Skip to content

A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.

perf(editor): faster Play — stamped host build, idle prewarm, V8 code cache

yhhec463baae6fd
diff --git a/desktop/electron/buildPlayRealm.ts b/desktop/electron/buildPlayRealm.ts
index 11e914ddc..691702a21 100644
--- a/desktop/electron/buildPlayRealm.ts
+++ b/desktop/electron/buildPlayRealm.ts
@@ -113,32 +113,46 @@ export async function buildPlayRealm(opts: {
await mkdir(out, { recursive: true });
// 1. Host module — esengine EXTERNAL (resolved by the realm's import map).
- try {
- const res = await build({
- entryPoints: [opts.playHostEntry],
- bundle: true,
- format: 'esm',
- platform: 'browser',
- target: 'es2020',
- external: ['esengine', 'esengine/*'],
- outfile: path.join(out, 'host.js'),
- sourcemap: false,
- write: true,
- logLevel: 'silent',
- });
- errors.push(...res.errors.map((e) => e.text));
- } catch (err) {
- const e = err as { errors?: { text: string }[]; message?: string };
- errors.push(...(e.errors?.map((x) => x.text) ?? [String(e.message ?? err)]));
- return { ok: false, hostPath: '', errors };
+ // Stamped on the entry's stat like the dir syncs below, so a repeat Play
+ // (the hot path — prewarmed on project open) skips esbuild entirely.
+ const hostStamp = path.join(out, '.host-stamp');
+ const hostOut = path.join(out, 'host.js');
+ const entryStat = await stat(opts.playHostEntry);
+ const hostSig = `${opts.playHostEntry}:${entryStat.size}:${Math.round(entryStat.mtimeMs)}`;
+ const hostFresh =
+ existsSync(hostOut) && existsSync(hostStamp) && (await readFile(hostStamp, 'utf8')) === hostSig;
+ if (!hostFresh) {
+ try {
+ const res = await build({
+ entryPoints: [opts.playHostEntry],
+ bundle: true,
+ format: 'esm',
+ platform: 'browser',
+ target: 'es2020',
+ external: ['esengine', 'esengine/*'],
+ outfile: hostOut,
+ sourcemap: false,
+ write: true,
+ logLevel: 'silent',
+ });
+ errors.push(...res.errors.map((e) => e.text));
+ if (errors.length === 0) await writeFile(hostStamp, hostSig);
+ } catch (err) {
+ const e = err as { errors?: { text: string }[]; message?: string };
+ errors.push(...(e.errors?.map((x) => x.text) ?? [String(e.message ?? err)]));
+ return { ok: false, hostPath: '', errors };
+ }
}
// 2. SDK + wasm copies (gated on a full dir signature, so an added file re-syncs).
await syncDir(opts.sdkDistDir, path.join(out, 'sdk'), path.join(out, '.sdk-stamp'));
await syncDir(opts.wasmDir, path.join(out, 'wasm'), path.join(out, '.wasm-stamp'));
- // 3. Host page.
- await writeFile(path.join(out, 'play.html'), PLAY_HTML);
+ // 3. Host page (only when its content actually changed — keeps mtimes stable).
+ const htmlPath = path.join(out, 'play.html');
+ if (!existsSync(htmlPath) || (await readFile(htmlPath, 'utf8')) !== PLAY_HTML) {
+ await writeFile(htmlPath, PLAY_HTML);
+ }
return { ok: errors.length === 0, hostPath: `${PLAY_DIR}/play.html`, errors };
}
diff --git a/desktop/electron/main.ts b/desktop/electron/main.ts
index 12a6d6106..eb353781b 100644
--- a/desktop/electron/main.ts
+++ b/desktop/electron/main.ts
@@ -47,11 +47,13 @@ import type { WorkspaceState } from '../src/project/format';
protocol.registerSchemesAsPrivileged([
{
scheme: 'estella',
- privileges: { standard: true, secure: true, supportFetchAPI: true, corsEnabled: true, stream: true },
+ // codeCache: V8 caches compiled bytecode for scripts served over this scheme
+ // (the play realm's SDK bundle + host), cutting repeat-Play parse time.
+ privileges: { standard: true, secure: true, supportFetchAPI: true, corsEnabled: true, stream: true, codeCache: true },
},
{
scheme: 'app',
- privileges: { standard: true, secure: true, supportFetchAPI: true, corsEnabled: true, stream: true },
+ privileges: { standard: true, secure: true, supportFetchAPI: true, corsEnabled: true, stream: true, codeCache: true },
},
]);
diff --git a/desktop/src/App.tsx b/desktop/src/App.tsx
index 604ad37e7..48d6ccbc3 100644
--- a/desktop/src/App.tsx
+++ b/desktop/src/App.tsx
@@ -78,6 +78,10 @@ export function App() {
return unsub;
}, []);
+ // Schedule work for browser idle time (fallback for environments without rIC).
+ const idle = (fn: () => void) =>
+ typeof window.requestIdleCallback === 'function' ? window.requestIdleCallback(fn) : setTimeout(fn, 500);
+
// Play runs in an ISOLATED realm (the Game panel's iframe = the shipping
// runtime), NOT by flipping the main edit World — so gameplay can never dirty
// the scene and the Viewport stays a live Scene view.
@@ -103,6 +107,8 @@ export function App() {
PlayInspect.stop();
useEditorStore.getState().setInspectWorld('editor');
dockApi.closeGame();
+ // Re-stage the realm in idle time so the next Play skips the build wait.
+ idle(() => PlayRealm.prewarm());
}
}, [isPlaying]);
useEffect(() => {
@@ -112,6 +118,12 @@ export function App() {
// The editor opens on the launcher (project browser); the shell + engine mount
// only once a project is opened. (Logic wiring lands with the recents IPC.)
const showLauncher = useEditorStore((s) => s.showLauncher);
+ // Stage the play realm right after a project opens, so the first Play doesn't
+ // pay the esbuild + sdk/wasm sync wait.
+ useEffect(() => {
+ if (!showLauncher) idle(() => PlayRealm.prewarm());
+ // eslint-disable-next-line react-hooks/exhaustive-deps
+ }, [showLauncher]);
const buildOpen = useEditorStore((s) => s.buildOpen);
const settingsOpen = useEditorStore((s) => s.settingsOpen);
if (showLauncher) return <Launcher />;
diff --git a/desktop/src/engine/PlayRealm.ts b/desktop/src/engine/PlayRealm.ts
index 2eb1039b0..e5fb77aa4 100644
--- a/desktop/src/engine/PlayRealm.ts
+++ b/desktop/src/engine/PlayRealm.ts
@@ -75,6 +75,17 @@ class PlayRealmImpl {
* esengine instance (custom components/systems run) and all assets are
* same-origin. Init is posted on the realm's `hello`.
*/
+ /** Stage the realm ahead of time (idle after project open / play stop), so
+ * Play's own preparePlayRealm hits warm stamps and returns in milliseconds.
+ * Fire-and-forget; the build is stamped + idempotent. */
+ prewarm(): void {
+ try {
+ void window.estella.project.preparePlayRealm();
+ } catch {
+ /* prewarm is best-effort; Play reports real errors */
+ }
+ }
+
async start(payload: PlayPayload): Promise<void> {
this.payload = payload;
this.set({ playing: true, ready: false, error: null });