perf(geometry): a flat batch's usual mesh metadata travels in typed columns; the pool rebuilds the same objects

Closed
LO
17d ago
+545-4

Stacked on #10175 (base perf/packed-flat-batch); retarget to main once #10175 lands. Untimed evidence pending; timed verdict pending (slot 90).

What

After #10175 the vertex streams travel packed, but each mesh is still about a dozen small heap objects on the wire:

  • the mesh object itself;
  • color;
  • origin;
  • localBounds and its two arrays;
  • the 16-number localToWorld;
  • the fingerprint box and its two arrays;
  • strings and numbers.

The main thread deserializes all of them inside the pool's onmessage.

This PR changes that:

  • Which meshes: a mesh whose fields are exactly the worker's usual ones, in the worker's key order.
  • How it travels: as a slot (posted as 0) in a few typed columns: expressId u32, an ifcType string table, f64 values for colour, origin, bounds, transform, class, ids, box and volume, and a u64 hash.
  • On arrival: unpackFlatMeta rebuilds the same object, with the same keys, order and values (-0 and large u32 ids included), before the vertex views are filled in.
  • Everything else travels whole, in its place: any mesh with another field (shadingColor, material, UVs, textures, entityIds, …), another key order, or a value the columns cannot hold exactly.
  • Files:
    • New module: packages/geometry/src/flat-batch-meta.ts.
    • flat-batch-pack.ts calls it.
    • No change to geometry.worker.ts or geometry-parallel.ts.

Why this lever (ranked by main-thread ms removed per worker ms added)

Untimed traces on top of #10175 (Holter cold, 3 runs) still show 167 ms of self time in geometry-parallel.ts.

In the Holter-shaped microbenchmark:

  • vertex streams only (#10175): 76 ms;
  • everything packed: 45 ms (another −41%);
  • worker packing cost is about equal in both cases.

Expected: roughly −70 to −90 ms of main-thread time on Holter (including GC), with about zero added worker time.

The alternatives were ranked lower:

  • LOD1 on idle workers: about −60 ms, and finalize would have to wait for the result.
  • M2 encoded in Rust: about −250 ms on the main thread, but the JS encoder measured 500–800 ns/vertex (0.75–1.2 s on Holter). Even 2–3× faster in Rust, that adds 250–400 ms of worker time on the critical path, and the change is large.

Lessons from #9948 and M2 applied: only removed main-thread work counts, and worker CPU is on the critical path. The expected gain is about 2–3% of Holter cold, which may not clear a timed 2× floor on its own.

Evidence so far

  • flat-batch-meta.test.ts:
    • every field combination round-trips with the same keys, order and values, and those meshes are posted as 0;
    • meshes with other fields or another key order stay objects;
    • meshes with values the columns cannot hold exactly (no ifcType, a non-integer id) stay objects.
  • flat-batch-pack.test.ts 6/6. Geometry typecheck (92 test files) and tsc are clean.
  • Revert-oracle mutation for the new module (tests/evidence/10175-flat-batch-meta): red 2/3, green 3/3. The #10175 mutation patch is refreshed for the stacked file: red 5/6, green 6/6.
  • Pending:
    • browser per-element fingerprint against #10175 on the 7 corpus fixtures;
    • re-traced pool onmessage self time;
    • capped A/B with A/A in the same admission (Holter, BWK, skolebygg).

🤖 Generated with Claude Code