perf(renderer): scan triangle topology once per mesh and frame in appendToBatches

Merged
PE
1mo ago
+217-10

Root cause

Scene.appendToBatches validated every mesh with topologySafeBatchOrigin, and resolvePrecisionBucket then asked the same (mesh, frame) question again when the mesh opened a new bucket. Each ask is an O(triangles) f32 scan of the same placed mesh.

Fix

appendToBatches creates a TopologyMemo ((mesh, frame) -> verdict) and threads it through validation and resolvePrecisionBucket. It is created per append and dropped afterwards, so nothing leaks across appends or frames. Without a memo the functions behave exactly as before. Frames, buckets and thrown errors are unchanged because the memo only returns what the scan would have returned.

placeMesh was checked as well. It is a shallow record spread that shares the vertex arrays and the placed record must stay distinct from the source, so it is left alone and a test pins that.

A synthetic micro-benchmark (3000 meshes of 600 vertices, one colour, streaming append, best of 5 warm runs) went from 86.1 ms to 70.8 ms. It is a synthetic number from one machine, not an end-to-end measurement.

Not covered: createBatchedMesh picks the bucket frame over all of the bucket's meshes at upload, which is a separate scan and is left as is.

Tests

  • scene-topology-memo.test.ts: routing after validation adds zero scans (scan and hit counters); routing and frame choice are identical with and without the memo, including a mesh whose tiny triangles fail the far frame and force a second bucket; placeMesh shares vertex arrays.
  • scene-topology-scans.test.ts (new): runs the real Scene.appendToBatches and counts the scan's Math.fround calls. One mesh costs at most two scans (validation+routing share one, plus the upload's); three before the change.
  • Green: pnpm --filter @ifc-lite/renderer test 2201 pass, 0 fail; tsc --noEmit clean.
  • Red without: disabling the memo cache fails the unit test; removing the memo at the validation call site, the routing call site, or the resolveActiveBucket wiring in scene.ts each fail the Scene test (54000 fround calls vs the 36000 limit).

Closes #8201