Skip to content

The native runtime for three.js: three.js, down to the metal. 45× smaller than Electron, on a Sony PS3, and path-traced at 60 fps on ray-tracing GPUs.

The voxel benches count the commit's build with an edit's cost (#169)

Yujia6748d79174fc
diff --git a/tools/bench-destruction.ts b/tools/bench-destruction.ts
index a562d44..01ebd57 100644
--- a/tools/bench-destruction.ts
+++ b/tools/bench-destruction.ts
@@ -5,7 +5,8 @@
// 10 s); since step 6 also topple (explosions at tree trunks: each cuts a crown loose), crater (4 m on open ground) and
// fell (a saw cut that drops buildings: the limit). Per explosion, from the game's log, the renderer's carve / rebuild / pieces ms and counts; per explosion frame,
// from the frame log, the worse of its two frames (the tick that fires it, the commit that applies it), interval and
-// CPU, the in-place edit's CPU (`edit`) or the rebuild it fell back to (`rebuild`, `fallback`); over the destruction,
+// CPU, the in-place edit's CPU (`edit` and the commit's `build`, which is also given alone) or the rebuild it fell
+// back to (`rebuild`, `fallback`); over the destruction,
// frames over 50 ms, frame start p95 and the trace pass's GPU timestamps. One more run per route with
// THREERT_PROFILE_BLAS=1 times the edits' BLAS / TLAS builds on the GPU. THREERT_EDIT_RESERVE=4 (room for pieces; the
// memory it costs is reported). The GPU lock is held; other processes' GPU share is recorded per run.
@@ -18,7 +19,8 @@
// dust: batched, the debris as stateful GPU particles colliding with the ground and the voxels (ROADMAP § 6 step 5a).
// smoke: batched, every explosion a puff of simulated smoke on a density grid (ROADMAP § 6 step 5b).
// The debris route also reports the debris in flight (most at once), what the explosions threw, and the in-place
-// instance updates' CPU per frame (frame log `instanced`).
+// instance updates' CPU per frame (frame log `instanced`; the build they leave is the frame's `build`: between
+// explosions, `between_build_p50`).
//
// Result: out/bench/destruction/<time>.json and a table on stdout.
import { execFile, execFileSync } from "node:child_process";
@@ -26,7 +28,7 @@ import { mkdirSync, readFileSync, writeFileSync } from "node:fs";
import { join } from "node:path";
import { promisify } from "node:util";
import { generate } from "./voxel-level/generate.ts";
-import { acquireGpuLock, frameStats, rendererBin, repoRoot } from "./lib/bench.ts";
+import { acquireGpuLock, editCost, frameStats, rendererBin, repoRoot } from "./lib/bench.ts";
import { branchBuild } from "./lib/build-at.ts";
import { startLive } from "./lib/live.ts";
@@ -67,7 +69,7 @@ for (const r of routes) {
}
type Room = { tris: [number, number]; records: [number, number]; objects: [number, number]; holes: number; grows: number; buffers_mb: number; blas_mb: number };
-type Frame = { interval: number; start?: number; cpu: number; edit: number; edit_split?: number[]; rebuild: number; blas: number; trace: number; fallback: string | null; voxel?: [number, number]; instanced?: number; room?: Room | null };
+type Frame = { interval: number; start?: number; cpu: number; edit: number; build?: number; edit_split?: number[]; rebuild: number; blas: number; trace: number; fallback: string | null; voxel?: [number, number]; instanced?: number; room?: Room | null };
type Explosion = { k: number; r: number; carve_ms: number; rebuild_ms: number; pieces_ms: number; piece_mesh_ms: number; piece_world_ms: number; voxels: number; touched: number; pieces: number; dropped: number; triangles: number; world_objects: number; thrown?: number; debris?: number };
type BatchFrame = { batches: number; rebuilt_tris?: number; rebuild_ms?: number; settled?: number; settle_ms?: number };
@@ -108,7 +110,7 @@ function figures(x: Awaited<ReturnType<typeof once>>) {
const cost = (i: number) => {
const fs = span(i), e = fs.find((f) => f.edit > 0) ?? fs[0];
const sp = e.edit_split ?? [0, 0, 0, 0, 0, 0];
- return { interval: r2(Math.max(...fs.map((f) => f.interval))), cpu: r2(Math.max(...fs.map((f) => f.cpu))), edit: r2(e.edit), tris: r2(sp[0]), upload: r2(sp[1]), blas_cpu: r2(sp[2]), added: sp[3], deformed: sp[4], blas: r2(Math.max(...fs.map((f) => f.blas ?? 0))), fallback: fs.map((f) => f.fallback).find(Boolean) ?? null };
+ return { interval: r2(Math.max(...fs.map((f) => f.interval))), cpu: r2(Math.max(...fs.map((f) => f.cpu))), edit: r2(editCost(e)), build: r2(e.build ?? 0), tris: r2(sp[0]), upload: r2(sp[1]), blas_cpu: r2(sp[2]), added: sp[3], deformed: sp[4], blas: r2(Math.max(...fs.map((f) => f.blas ?? 0))), fallback: fs.map((f) => f.fallback).find(Boolean) ?? null };
};
const ei = at(0), si = at(1);
const exp = ei.map(cost), settle = si.map(cost);
@@ -126,13 +128,13 @@ function figures(x: Awaited<ReturnType<typeof once>>) {
const ks = e.map((v, k) => ({ v, k })).filter(({ v }) => v.r === r);
const fr = one ? ks.map(({ k }) => exp[k]) : [];
const pm = (f: (v: Explosion) => number) => med(ks.map(({ v }) => f(v)));
- return { r, n: ks.length, voxels: pm((v) => v.voxels), touched: pm((v) => v.touched), pieces: pm((v) => v.pieces), carve_ms: pm((v) => v.carve_ms), rebuild_ms: pm((v) => v.rebuild_ms), pieces_ms: pm((v) => v.pieces_ms), piece_mesh_ms: pm((v) => v.piece_mesh_ms ?? 0), piece_world_ms: pm((v) => v.piece_world_ms ?? 0), frame_ms: med(fr.map((f) => f.interval)), cpu_ms: med(fr.map((f) => f.cpu)), edit_ms: med(fr.map((f) => f.edit)), edit_tris_ms: med(fr.map((f) => f.tris)), edit_upload_ms: med(fr.map((f) => f.upload)), edit_blas_cpu_ms: med(fr.map((f) => f.blas_cpu)), meshes_added: med(fr.map((f) => f.added)), gpu_blas_ms: med(fr.map((f) => f.blas).filter((v) => v > 0)) };
+ return { r, n: ks.length, voxels: pm((v) => v.voxels), touched: pm((v) => v.touched), pieces: pm((v) => v.pieces), carve_ms: pm((v) => v.carve_ms), rebuild_ms: pm((v) => v.rebuild_ms), pieces_ms: pm((v) => v.pieces_ms), piece_mesh_ms: pm((v) => v.piece_mesh_ms ?? 0), piece_world_ms: pm((v) => v.piece_world_ms ?? 0), frame_ms: med(fr.map((f) => f.interval)), cpu_ms: med(fr.map((f) => f.cpu)), edit_ms: med(fr.map((f) => f.edit)), edit_build_ms: med(fr.map((f) => f.build)), edit_tris_ms: med(fr.map((f) => f.tris)), edit_upload_ms: med(fr.map((f) => f.upload)), edit_blas_cpu_ms: med(fr.map((f) => f.blas_cpu)), meshes_added: med(fr.map((f) => f.added)), gpu_blas_ms: med(fr.map((f) => f.blas).filter((v) => v > 0)) };
}),
interval_p50: med(exp.map((f) => f.interval)), interval_max: max(exp.map((f) => f.interval)),
settle_p50: med(settle.map((f) => f.interval)), settle_max: max(settle.map((f) => f.interval)),
- between_p50: med(between.map((f) => f.interval)), between_edit_p50: med(between.map((f) => f.edit)),
+ between_p50: med(between.map((f) => f.interval)), between_edit_p50: med(between.map((f) => f.edit)), between_build_p50: med(between.map((f) => f.build ?? 0)),
batch_rebuild_ms_p50: med(x.batches.map((b) => b.rebuild_ms ?? 0).filter((v) => v > 0)), batch_tris_p50: med(x.batches.map((b) => b.rebuilt_tris ?? 0).filter((v) => v > 0)), batch_live_max: max(x.batches.map((b) => b.batches)),
- edit_p50: med(exp.map((f) => f.edit)),
+ edit_p50: med(exp.map((f) => f.edit)), build_p50: med(exp.map((f) => f.build)),
renderer_p50: med(e.map((v) => v.carve_ms + v.rebuild_ms + v.pieces_ms)),
window_over_50: window.filter((f) => f.interval > 50).length,
window_frames: window.length,
@@ -170,7 +172,7 @@ for (let round = 1; round <= rounds; round++) {
const f = figures(await once(route, scenario, {}));
const row = { round, scenario, route, tag, ...f };
rows.push(row);
- console.log(JSON.stringify({ round, scenario, route, tag, explosions: f.explosions, interval_p50: f.interval_p50, interval_max: f.interval_max, settle_p50: f.settle_p50, settle_max: f.settle_max, between_p50: f.between_p50, renderer_p50: f.renderer_p50, edit_p50: f.edit_p50, over_50: f.window_over_50, start_p95: f.window_start_p95, trace_p50: f.window_trace_p50, fallbacks: f.fallbacks.length, objects_end: f.objects_end, others: f.others_pct, ...(route === "dust" ? { thrown: f.thrown } : {}), ...(route === "debris" ? { thrown: f.thrown, debris_max: f.debris_max, instanced_p50: f.instanced_p50, instanced_max: f.instanced_max } : {}) }));
+ console.log(JSON.stringify({ round, scenario, route, tag, explosions: f.explosions, interval_p50: f.interval_p50, interval_max: f.interval_max, settle_p50: f.settle_p50, settle_max: f.settle_max, between_p50: f.between_p50, renderer_p50: f.renderer_p50, edit_p50: f.edit_p50, build_p50: f.build_p50, over_50: f.window_over_50, start_p95: f.window_start_p95, trace_p50: f.window_trace_p50, fallbacks: f.fallbacks.length, objects_end: f.objects_end, others: f.others_pct, ...(route === "dust" ? { thrown: f.thrown } : {}), ...(route === "debris" ? { thrown: f.thrown, debris_max: f.debris_max, instanced_p50: f.instanced_p50, instanced_max: f.instanced_max, between_build_p50: f.between_build_p50 } : {}) }));
}
}
// The edits' GPU builds (drained around: not frame times), once per route and scenario.
diff --git a/tools/bench-voxel-light.ts b/tools/bench-voxel-light.ts
index 9c1187a..282d72d 100644
--- a/tools/bench-voxel-light.ts
+++ b/tools/bench-voxel-light.ts
@@ -32,7 +32,7 @@ import sharp from "sharp";
import { blastLamp, type Night, SHOTS } from "../games/voxel-level/level-night.game.ts";
import { cameraPath } from "../games/voxel-level/plan.ts";
import { bundleGame } from "./bundle-game.ts";
-import { frameStats, rendererBin, repoRoot, requireGpuLock } from "./lib/bench.ts";
+import { editCost, frameStats, rendererBin, repoRoot, requireGpuLock } from "./lib/bench.ts";
import { startLive } from "./lib/live.ts";
import { generate } from "./voxel-level/generate.ts";
@@ -200,7 +200,7 @@ if (only.includes("subtree")) {
if (only.includes("blast")) {
const b = await bundle("blast-live-0", { blast: "lamp", when: 300 });
const runs: Record<string, unknown>[] = [];
- type Frame = { interval: number; start?: number; cpu: number; edit: number; rebuild: number; fallback: string | null; voxel?: [number, number] };
+ type Frame = { interval: number; start?: number; cpu: number; edit: number; build?: number; rebuild: number; fallback: string | null; voxel?: [number, number] };
for (let round = 1; round <= rounds; round++) {
const log = join(dir, "blast-frames.jsonl");
const live = startLive({ bin: rendererBin, assets: repoRoot, presets: join(repoRoot, "presets"), bundles: dir, label: "blast" }, b, { frameLog: log });
@@ -215,7 +215,7 @@ if (only.includes("blast")) {
const starts = after.map((f) => f.start ?? f.interval);
const row = {
round, frames: F.length, explosion_frame_ms: r2(Math.max(F[ex].interval, F[ex + 1]?.interval ?? 0)), explosion_cpu_ms: r2(Math.max(F[ex].cpu, F[ex + 1]?.cpu ?? 0)), settle_frame_ms: st >= 0 ? r2(Math.max(F[st].interval, F[st + 1]?.interval ?? 0)) : null,
- flight_edit_p50: r2(frameStats(F.slice(ex + 2, st > 0 ? st : undefined).map((f) => f.edit)).p50), over_50: after.filter((f) => f.interval > 50).length, start_p95: r2(frameStats(starts).p95), start_max: r2(Math.max(...starts)),
+ flight_edit_p50: r2(frameStats(F.slice(ex + 2, st > 0 ? st : undefined).map(editCost)).p50), over_50: after.filter((f) => f.interval > 50).length, start_p95: r2(frameStats(starts).p95), start_max: r2(Math.max(...starts)),
fallbacks: F.filter((f) => f.fallback).map((f) => f.fallback), rebuilds: F.filter((f) => f.rebuild > 0).length,
};
runs.push(row);
diff --git a/tools/bench-voxel.ts b/tools/bench-voxel.ts
index 6ec8a6c..f3a21d7 100644
--- a/tools/bench-voxel.ts
+++ b/tools/bench-voxel.ts
@@ -21,7 +21,7 @@ import { promisify } from "node:util";
import { BISTRO } from "../scenes/bistro.ts";
import { type Variant, cameraPath, pathAt } from "../games/voxel-level/plan.ts";
import { generate } from "./voxel-level/generate.ts";
-import { acquireGpuLock, frameStats, rendererBin, repoRoot } from "./lib/bench.ts";
+import { acquireGpuLock, editCost, frameStats, rendererBin, repoRoot } from "./lib/bench.ts";
import { branchBuild } from "./lib/build-at.ts";
import { type Drive, bistroDrive, district, startLive } from "./lib/live.ts";
@@ -114,7 +114,8 @@ async function measure(configs: Config[], what: string) {
if (args.includes("--edits")) {
// One object's geometry replaced every half second (tools/perf/voxel-level*-edits.game.ts), in place (live/edits.rs
// `edit_geometry`) against a scene rebuild (THREERT_EDIT_REBUILD=1), alternated; camera still at the path's start.
- // Per edit frame from the frame log: the edit's CPU ms (in place) or the rebuild's, and that frame's interval.
+ // Per edit frame from the frame log: the edit's CPU ms (in place: `edit` and the commit's `build`, which is also given
+ // alone) or the rebuild's, and that frame's interval.
const b = branchBuild(rendererBin);
const keys = cameraPath(1), tan = Math.tan((55 * Math.PI) / 360);
const still: Drive = { seconds: 12, at: () => ({ pos: keys[0].pos, target: keys[0].target, tan }) };
@@ -131,11 +132,11 @@ if (args.includes("--edits")) {
const env: Record<string, string> = mode === "rebuild" ? { THREERT_EDIT_REBUILD: "1" } : mode === "in place, profiled" ? { THREERT_PROFILE_BLAS: "1" } : {};
if (r === 1) { const l = startLive(b, bundle, { env }); await l.firstFrame; await l.quit(); }
const d = await district(b, bundle, log, still, { env });
- type F = { edit: number; rebuild: number; interval: number; blas: number; fallback: string | null };
+ type F = { edit: number; build?: number; rebuild: number; interval: number; blas: number; fallback: string | null };
const frames = readFileSync(log, "utf8").trim().split("\n").slice(-d.frames).map((l) => JSON.parse(l) as F);
const hit = frames.filter((f) => f.edit > 0 || f.rebuild > 0);
const ms = (xs: number[]) => (xs.length ? { p50: +frameStats(xs).p50.toFixed(2), max: +Math.max(...xs).toFixed(2) } : null);
- const row = { round: r, scene: name, mode, edits: hit.length, cost_ms: ms(hit.map((f) => (mode === "rebuild" ? f.rebuild : f.edit))), interval_ms: ms(hit.map((f) => f.interval)), ...(mode === "in place, profiled" ? { gpu_build_ms: ms(hit.map((f) => f.blas)) } : {}), fallbacks: [...new Set(hit.map((f) => f.fallback).filter((x) => x && !x.startsWith("THREERT_EDIT_REBUILD")))] };
+ const row = { round: r, scene: name, mode, edits: hit.length, cost_ms: ms(hit.map((f) => (mode === "rebuild" ? f.rebuild : editCost(f)))), ...(mode === "rebuild" ? {} : { build_ms: ms(hit.map((f) => f.build ?? 0)) }), interval_ms: ms(hit.map((f) => f.interval)), ...(mode === "in place, profiled" ? { gpu_build_ms: ms(hit.map((f) => f.blas)) } : {}), fallbacks: [...new Set(hit.map((f) => f.fallback).filter((x) => x && !x.startsWith("THREERT_EDIT_REBUILD")))] };
rows.push(row);
console.log(JSON.stringify(row));
}
diff --git a/tools/lib/bench.ts b/tools/lib/bench.ts
index 4cf91ae..2238253 100644
--- a/tools/lib/bench.ts
+++ b/tools/lib/bench.ts
@@ -197,6 +197,13 @@ export function frameStats(ms: number[]) {
return { n: s.length, p50, p95: q(0.95), p99: q(0.99), max: s.at(-1) ?? NaN, spikes: ms.filter((v) => v > 2 * p50).length };
}
+/** What a frame's in-place edits cost its CPU, ms: writing them (frame log `edit`) and the one build above the
+ * triangles the commit ends with (`build`; a log from before it has none, and its `edit` held that share). The build
+ * is the frame's whole: whatever else moved in that frame was built in the same call. */
+export function editCost(f: { edit: number; build?: number }): number {
+ return f.edit + (f.build ?? 0);
+}
+
/**
* Runs `fn` repeatedly for about `budgetMs` after dropping `warmup` runs; returns per-run times (ms). Throughput
* benchmarks size their work this way instead of a fixed iteration count.