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.

feat(probe): the renderer already knew where its arrival went — nobody was reading it

esenginec1912124c2c4
diff --git a/bench/residency/heavy.mjs b/bench/residency/heavy.mjs
index ef757d071..c7e351faf 100755
--- a/bench/residency/heavy.mjs
+++ b/bench/residency/heavy.mjs
@@ -372,6 +372,8 @@ function report(arm, frames, delivery) {
console.log(` ${domain.padEnd(16)}${ms.toFixed(2).padStart(7)} ms`);
}
+ realization(window, frames[arrivalAt]);
+
console.log('');
check(window.length > 0 && systemExcess >= wallExcess * 0.95,
'and what the arrival ADDS to those frames is explained by the systems that ran',
@@ -506,6 +508,45 @@ const median = (values) => {
return sorted[Math.floor(sorted.length / 2)];
};
+/**
+ * Inside the render domain, on the frame the arrival landed on.
+ *
+ * Systems say WHICH subsystem paid; only the scopes say what it was doing. The
+ * `cpp.*` rows nest inside the JS scope that called them, so the two are printed
+ * as lists and never summed into one total.
+ */
+function realization(window, arrival) {
+ const rows = [];
+ for (const f of window) {
+ for (const s of f.scopes ?? []) rows.push({ where: 'js', ...s, frame: f });
+ for (const [name, ms] of Object.entries(f.native ?? {})) {
+ rows.push({ where: 'cpp', name, ms, system: '', frame: f });
+ }
+ }
+ if (rows.length === 0) {
+ console.log('\n (no scopes: the frames were too cheap to carry a breakdown)');
+ return;
+ }
+ console.log('\n inside the arrival frames, by scope:');
+ const total = new Map();
+ for (const r of rows) {
+ const key = `${r.where} ${r.name}`;
+ total.set(key, (total.get(key) ?? 0) + r.ms);
+ }
+ for (const [name, ms] of [...total].sort((a, b) => b[1] - a[1])) {
+ if (ms < 0.05) continue;
+ console.log(` ${name.padEnd(30)}${ms.toFixed(2).padStart(7)} ms`);
+ }
+ const renderSystem = (arrival?.costs ?? []).find((c) => c.name === 'RenderSystem');
+ if (renderSystem) {
+ const js = rows.filter((r) => r.where === 'js' && r.system === 'RenderSystem'
+ && r.frame === arrival).reduce((a, r) => a + r.ms, 0);
+ console.log(`\n RenderSystem on the arrival frame ${renderSystem.ms.toFixed(2)} ms,`
+ + ` of which ${js.toFixed(2)} ms is named by JS scopes`
+ + ` (${((js / renderSystem.ms) * 100).toFixed(0)}%)`);
+ }
+}
+
/** What one frame cost, by the domain that owns each system. */
function byDomain(frame) {
return new Map(Object.entries(frame.domains ?? {}));
diff --git a/pipeline/src/runtime/gameHost.ts b/pipeline/src/runtime/gameHost.ts
index 8ad2c7713..a73c008be 100644
--- a/pipeline/src/runtime/gameHost.ts
+++ b/pipeline/src/runtime/gameHost.ts
@@ -485,20 +485,44 @@ async function boot(): Promise<void> {
* shows on no system timer, while registering what it spawned happens
* inside systems on the frames after. Engaging the stats fills this.
*/
- costs(): { on: boolean; systems: Array<{ name: string; ms: number; domain: string }> } {
- // Engaged once. Re-enabling every call swaps the maps the frame just
- // filled, which reads as an engine that costs nothing.
- if (!statsOn) { app.enableStats(); statsOn = true; }
+ costs(): {
+ on: boolean;
+ systems: Array<{ name: string; ms: number; domain: string }>;
+ scopes: Array<{ name: string; ms: number; system: string; remainder: string }>;
+ native: Record<string, number>;
+ } {
+ // Engaged once, BOTH halves. Re-enabling swaps the maps the frame just
+ // filled; and the C++ scopes are behind their own switch, so engaging
+ // only the JS side shows a renderer that costs nothing inside itself.
+ if (!statsOn) {
+ app.enableStats();
+ (app.wasmModule as { engine_setCpuProfiling?: (on: boolean) => void } | null)
+ ?.engine_setCpuProfiling?.(true);
+ statsOn = true;
+ }
const costs = app.getFrameCosts();
- if (!costs) return { on: false, systems: [] };
+ if (!costs) return { on: false, systems: [], scopes: [], native: {} };
+ let native: Record<string, number> = {};
+ try {
+ const raw = (app.wasmModule as { engine_getCpuScopes?: () => string } | null)
+ ?.engine_getCpuScopes?.();
+ if (raw) native = JSON.parse(raw) as Record<string, number>;
+ } catch { /* a frame with no scopes reports none */ }
// EVERY system, not the dearest few: what a caller wants to know about
// an arrival frame is how much of it the breakdown explains, and a
// truncated list can only ever be a lower bound on its own total.
+ //
+ // Systems are mutually exclusive and scopes nest INSIDE them, so the two
+ // lists are never added together — that is how a breakdown reaches 130%.
return {
on: true,
systems: [...costs.systems]
.map((s) => ({ name: s.name, ms: s.ms, domain: s.domain }))
.sort((a, b) => b.ms - a.ms),
+ scopes: [...costs.scopes]
+ .map((s) => ({ name: s.name, ms: s.ms, system: s.system, remainder: s.remainder }))
+ .sort((a, b) => b.ms - a.ms),
+ native,
};
},
/** Drive a hot update against a served (CDN) manifest: fetch + diff + apply.
diff --git a/tools/launchers/residency-run.mjs b/tools/launchers/residency-run.mjs
index 3c1ad56d5..5b8337070 100644
--- a/tools/launchers/residency-run.mjs
+++ b/tools/launchers/residency-run.mjs
@@ -274,8 +274,15 @@ async function main() {
for (const c of costs.systems) row.domains[c.domain] = (row.domains[c.domain] ?? 0) + c.ms;
for (const d of Object.keys(row.domains)) row.domains[d] = Math.round(row.domains[d] * 1000) / 1000;
// The per-SYSTEM detail only where it can matter: which system inside a
- // domain is a question worth asking of an expensive frame alone.
- if (ms > ${step.costsAbove ?? 1.5}) row.costs = costs.systems.filter((c) => c.ms > 0.01);
+ // domain is a question worth asking of an expensive frame alone. The
+ // scopes ride with it — they nest INSIDE systems, so a reader adds one
+ // list or the other and never both.
+ if (ms > ${step.costsAbove ?? 1.5}) {
+ row.costs = costs.systems.filter((c) => c.ms > 0.01);
+ row.scopes = costs.scopes.filter((c) => c.ms > 0.01);
+ row.native = Object.fromEntries(
+ Object.entries(costs.native).filter(([, v]) => v > 0.01));
+ }
out.push(row);
// A macrotask turn between frames. Awaiting a step only drains
// MICROtasks, and a cell arrives over the network — so a loop without