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(bench): the render path gets a benchmark, and it is linear

esengine99b33b616ef7
diff --git a/sdk/benchmarks/render-path.bench.ts b/sdk/benchmarks/render-path.bench.ts
new file mode 100644
index 000000000..430b73d38
--- /dev/null
+++ b/sdk/benchmarks/render-path.bench.ts
@@ -0,0 +1,86 @@
+// SPDX-License-Identifier: Apache-2.0
+// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
+/**
+ * @file What the render path costs as a scene grows.
+ *
+ * Every other benchmark here measures the ECS, the event queue, UI layout or the
+ * wasm boundary. None measured the path a frame actually takes, so how this
+ * engine behaves at a thousand objects versus ten thousand was a thing nobody
+ * could answer — it had never been run.
+ *
+ * The transform pass is the half of that path reachable without a GPU: it walks
+ * every Transform in the world each frame and rebuilds the world fields the
+ * renderer, the physics sync and picking all read. These cases are the shapes a
+ * scene comes in — flat, deep, wide — at two sizes each, so the SHAPE of the
+ * curve is visible and not just one number: ten times the world costs ten times
+ * as much when the pass is linear in it, and says so here when it is not. The
+ * sizes are what they are because a ratio needs both sides above the ten
+ * microseconds under which a sample is mostly timer.
+ */
+import { describe, bench, beforeAll } from 'vitest';
+import path from 'path';
+import { Transform } from '../src/ecs/component';
+import { wasmData } from '../tests/helpers/wasmComponentData';
+import { WASM_DIR } from '../tests/helpers/loadWasm';
+
+let module: any;
+let Registry: any;
+/** The worlds, built once. A `beforeAll` inside a `describe` does not run before
+ * its benches do, and a bench that throws is SKIPPED while the run still exits
+ * 0 — so everything they need is set up here, where it is awaited. */
+const worlds: Record<string, any> = {};
+
+beforeAll(async () => {
+ const mod = await import(path.join(WASM_DIR, 'esengine.js'));
+ module = await mod.default({ locateFile: (p: string) => path.join(WASM_DIR, p) });
+ Registry = module.Registry;
+ worlds.flatSmall = flatWorld(5_000);
+ worlds.flatLarge = flatWorld(50_000);
+ // Same 10k transforms both times: the difference is how far the walk
+ // recurses, which is the cost a deep rig pays over a wide one.
+ worlds.shallow = chainedWorld(10_000, 2);
+ worlds.deep = chainedWorld(10_000, 20);
+});
+
+const TRANSFORM_DATA = wasmData(Transform);
+
+/** `count` transforms with no parent between them. */
+function flatWorld(count: number): any {
+ const reg = new Registry();
+ for (let i = 0; i < count; i++) {
+ reg.addTransform(reg.create(), TRANSFORM_DATA);
+ }
+ return reg;
+}
+
+/** `count` transforms in chains `depth` long — the same total, walked recursively. */
+function chainedWorld(count: number, depth: number): any {
+ const reg = new Registry();
+ for (let root = 0; root < count / depth; root++) {
+ let parent = reg.create();
+ reg.addTransform(parent, TRANSFORM_DATA);
+ for (let d = 1; d < depth; d++) {
+ const child = reg.create();
+ reg.addTransform(child, TRANSFORM_DATA);
+ reg.setParent(child, parent);
+ parent = child;
+ }
+ }
+ return reg;
+}
+
+/** One frame of the pass: begin clears the once-per-frame latch, then it runs. */
+function frame(reg: any): void {
+ module.renderer_beginFrame(0);
+ module.renderer_updateTransforms(reg);
+}
+
+describe('Render path - transform pass, flat world', () => {
+ bench('5k roots', () => frame(worlds.flatSmall));
+ bench('50k roots', () => frame(worlds.flatLarge));
+});
+
+describe('Render path - transform pass, hierarchy', () => {
+ bench('10k in chains of 2', () => frame(worlds.shallow));
+ bench('10k in chains of 20', () => frame(worlds.deep));
+});
diff --git a/sdk/etc/perf.snapshot.json b/sdk/etc/perf.snapshot.json
index 76b1a8ada..f0180800a 100644
--- a/sdk/etc/perf.snapshot.json
+++ b/sdk/etc/perf.snapshot.json
@@ -1,11 +1,13 @@
{
"note": "Generated by tools/perf-guard.mjs — run --update to accept changes.",
"tolerance": 0.5,
- "cases": 230,
+ "cases": 234,
"ratios": {
- "ui-layout: editing a field vs adding a node": 0.197,
- "ui-layout: editing a field vs an idle frame": 9.46,
- "query: materialising vs visiting": 2.73,
- "change-tracking: tracked vs untracked write-back": 1.03
+ "ui-layout: editing a field vs adding a node": 0.194,
+ "ui-layout: editing a field vs an idle frame": 8.73,
+ "query: materialising vs visiting": 2.54,
+ "change-tracking: tracked vs untracked write-back": 1.02,
+ "transform pass: ten times the world": 11,
+ "transform pass: deep hierarchy vs shallow": 1.08
}
}
diff --git a/tools/perf-guard.mjs b/tools/perf-guard.mjs
index f14a584e7..8dcd5b45f 100644
--- a/tools/perf-guard.mjs
+++ b/tools/perf-guard.mjs
@@ -86,6 +86,24 @@ const METRICS = [
of: 'Mut write-back, component tracked',
over: 'Mut write-back, component NOT tracked',
},
+ {
+ name: 'transform pass: ten times the world',
+ why: 'The pass walks every transform, so ten times as many costs ten times'
+ + ' as much. A number climbing well past 10 is a walk that grew a second'
+ + ' pass over the world — the shape no single measurement can show.',
+ group: 'Render path - transform pass, flat world',
+ of: '50k roots',
+ over: '5k roots',
+ },
+ {
+ name: 'transform pass: deep hierarchy vs shallow',
+ why: 'The same 10k transforms, reached through chains ten times as long.'
+ + ' Recursion depth is not work: this is near 1, and a number that is'
+ + ' not says the walk repeats itself down a chain.',
+ group: 'Render path - transform pass, hierarchy',
+ of: '10k in chains of 20',
+ over: '10k in chains of 2',
+ },
];
/**