perf(viewer): non-load work waits for the settled scene

Merged
LO
19d ago
+284-18

What

Step 1 of L1 (design: cold-finalize/L1-DESIGN.md, campaign scratch). On a Holter cold load the main thread is 96–100% busy from parser.complete to the end of the load. In that post-stream window, two pieces of work serve nothing that "interactive" needs.

Holter cold, main@cb641084ba, traced, ms of main-thread self time:

workHolteradvanced_modelwhat it serves
cache-write encode~375: buffer-utils 107, TextEncoder 127, appearance provenance 50, geometry chunks, strings, entity index~150the next open
placed spatial index: instanced materialisation + builder + BVH~290: materialisation 166, builder 61, BVH 63~100SDK and server spatial queries (not picking)

Both started at load.finalize, which comes before the renderer's drain and finalize, so they competed with those for the main thread.

The change makes both wait for one shared scene-settled signal. It is the predicate #9623 introduced for the georeference minimap, factored into apps/viewer/src/lib/scene-settled.ts: the store has left its loading state, nothing streams or waits to upload, and the renderer has no queued meshes, fragments or finalize in flight.

  • Placement index sync (spatial-index.ts): while the scene is unsettled, its debounce timer keeps the models pending and looks again. The existing generation guards still drop superseded builds.
  • Cache write, primary and federated (useIfcLoader.ts): it starts after settle. whenSceneSettled(isCurrent) resolves false when a newer load supersedes this one, or after 120 s if a failed finalize never settles; the write is then dropped. These are the conditions the rejected NEW-5/#9622 attempt asked for: a real readiness signal, no cloned geometry, and supersession dropping the work. Source compaction still runs after the write.
  • BinaryCacheWriter.write({ yieldBetweenSections }) (cache package, opt-in, written bytes unchanged): returns to the event loop after each section, so a write made while the page is interactive runs as short tasks.
  • The minimap gate (use-location-map-ready.ts) uses the shared predicate.

No geometry, GPU or output change.

Interaction with #9628 (merged). #9628 moved pendingInstancedShards out of the store. This branch has merged it: the predicate calls hasPendingInstancedShards() from @/store/instancedShardQueue, so shards still in that queue keep the scene unsettled.

Deterministic checks (fail on main)

  • spatial-index.test.ts: "builds no placed index while the scene is still draining or finalizing, then builds it once settled". The model is load-complete and its scene is finalizing. Across two debounce periods there is no index and no materialisation; when the finalize ends, the index publishes.
  • useIfcLoader.cacheAfterSettle.real-ifc.test.tsx: a real C20 (10.8 MB) cold load through the actual loader and WASM, with the renderer still finalizing. No cache entry appears for 10 s; once the finalize ends, it is written.
  • scene-settled.test.ts: every condition, supersession and the timeout.
  • cache.test.ts: with yieldBetweenSections, at least 6 macrotasks run during the write, and the bytes match the non-yielding write.

Timed verdict (exclusive A/B)

Base main + #9684 (3fb357d873), branch main + #9684 + this PR (000532b2cc), both on main@12fd328582, each built in its own checkout; harness = main's scripts/perf (#9607); 4 lanes × 5 interleaved rounds + memory pass; verdict = gain ≥ 2× the noise floor (max of the stage-0 A/A floor and the run's own).

Which run each verdict comes from. Slot s5 (benchmark job 6468479a, 2026-11-12 05:44–06:25 UTC) was contaminated in its first minutes: another lane ran two builds outside benchmark admission from 05:41 to 05:48 UTC. All 31 samples of round 0 of the first group (FZK, ISSUE_129, advanced_model, schependomlaan) fall in that window and carry the harness's underLoad flag. Those four fixtures' s5 cells are discarded; their verdict comes from a complete five-round rerun, slot s6 (job 890899e7, 06:51–07:08 UTC, 0 of 160 samples under load). Holter, skolebygg and BWK ran in s5 after 06:02 UTC with 0 samples under load; their verdict comes from s5.

fixturerunbrowser-cold interactive: base → thisgain2× floorverdict
Holters54,549 → 3,754 ms+17.5%6.3%PASS gain
BWKs53,622 → 3,328 ms+8.1%5.4%PASS gain
skolebyggs52,683 → 2,435+9.3%9.9%no significant difference
advanced_models61,753 → 1,515 ms+13.6%10.9%PASS gain
schependomlaans61,161 → 1,069+7.9%11.5%no significant difference
ISSUE_129s62,735 → 2,685+1.8%8.3%no significant difference
FZKs6559 → 580−3.8%21.7%no significant difference
  • Holter browser-cold UI quiet 5,124 → 4,399 ms (+14.1%, PASS).
  • No regression in any cell of either run: every fixture × lane (native, browser-cold, first reopen, snapshot reopen) × metric (interactive, first geometry, UI quiet, peak memory) is a gain or no significant difference.
  • Output identical: browser-cold element hashes (flat + instanced) identical in all 10 samples of every fixture. Reopen samples carry no element hashes in this harness (unverifiable there, as on base).
  • Reports: ~/.t3/scratch/2026-11-10-ifc-load-hillclimb/cold-finalize/l1/runs/s5-9747/report.md (Holter, skolebygg, BWK) and …/runs/s6-9747-g1/report.md (FZK, ISSUE_129, advanced_model, schependomlaan).

Status

  • focused fleet runs, branch and RED: see below; on d5259aea76 (job 1bdbbb8d) cache ok, viewer 263/263
  • Tests run on this exact head (d5259aea76): @ifc-lite/cache full suite 249/249 (28 files, run directly on Pop, no GPU); fleet job 1bdbbb8d on Pop with real fixtures and WASM built: the viewer selection covering everything this changes, 263/263 (scene-settled, model-placement/spatial-index, cacheAfterSettle, LocationMap, coordinateCache, sceneSnapshot*, coldScene*, useIfcCache, useIfcLoader), including both L1 tests and the real-C20 cache-after-settle test. CI Typecheck green on this head.
  • Full four-shard viewer suite on d5259aea76 (fleet job cd0d30b6, PC): shards 1–3 green; shard 0 had 1 failure, useModelRotationSync.test.ts #4890 (scene?.hasQueuedMeshes is not a function in isSceneSettled, from a scene test double). Fixed in 6beac84a86 (optional calls; production scenes implement all three). That test fails without the fix and passes with it; useModelRotationSync + scene-settled + model-placement/spatial-index 39/39 on 6beac84a86. The merge queue runs the full suite on the final head.
  • exclusive timed A/B (above)
  • review threads: 0 open

Focused tests on the fleet (real fixtures, wasm built)

headcacheviewer focused
82476f975a (= 848559595d but for a doc comment)30/3056/56, including the real-C20 cache-after-settle test and the placed-index L1 test
same tests with the fix reverted (throwaway copy)29/30 (the yield test fails)51/53 (both L1 tests fail)

🤖 Generated with Claude Code