perf(viewer): a primary load's reset runs each store subscriber once

Closed
LO
17d ago
+347-9

Status: the untimed proof shows ~5 ms, below every capped floor. No timed slot will be spent; recommended for closing (decision with the orchestrator). Do not queue.

Why

On today's main, the task that runs at a primary load's pick (from the pick to file.read) takes 120–132 ms traced on a Holter reopen (sourcemapped build of 65083544b0, untimed, 4 opens). It splits like this:

work in the pick tasktraced ms
React render/commit (shell mount, loading state)66–75
zustand subscriber fan-out (vanilla listener loop 15–16, shallow 5–6, react binding 3)~40
AnnotationLayer forced layout (getBoundingClientRect)9–16
loader5–6

A store-update log of the same pick shows 32 store writes in one synchronous task: resetViewerState, clearAllModels, clearLayerStack, the loading flags and the placeholder model. 14 of the 32 change no key. visibilityRevision is bumped 6 times. Every write re-runs every subscriber, and each useViewerStore(selector) re-evaluates its selector.

Change

  • store/notification-batch.ts: outermost store middleware. It wraps only subscribe.
    • holdViewerStoreNotifications() opens a hold. While a hold is open, writes apply at once, so get() sees them, and each subscriber's callback is deferred.
    • On the last release, each subscriber that was due runs once with (final state, the state before its first deferred write), in subscription order.
    • A subscriber that unsubscribed meanwhile is skipped. A write made during the flush notifies normally.
    • release is idempotent, and holds nest.
  • useIfcLoader: a primary load opens the hold just before its reset. It releases it after the placeholder model is written, before the first await, and again (idempotently) at the top of catch and in finally. Federated adds are untouched.
  • withStoreChurnCounters (#6957) sits inside the new middleware, so it counts the callbacks that actually run.
  • Module-size budget: useIfcLoader.ts stays at its 2,170-line allowlist budget.

Expected

About 25–35 ms untraced off every primary load's pick task, cold loads included: the fan-out minus one pass. That is near the capped 2x floors (Holter snapshot reopen 3.3%, about 40 ms; FZK cold 6.6%, about 33 ms). The timed A/B therefore runs Holter cold / first reopen / snapshot reopen plus FZK cold, each with its own A/A in the same admission. If none of them clears its floor, this PR is closed.

Validation (filled in as results land)

  • Untimed proof: the gain is ~5 ms, not 25–35. Sourcemapped builds of main 65083544b0 and of this branch 5ce5d13c63, Holter, untimed, under ifc-gpu-check.
    • Traced pick task [pick, file.read]: 120–132 ms on main vs 111–115 here (one outlier at 159).
    • The "store / zustand" category went from 38–41 to 28–33 ms, but zustand's listener-loop self time barely moved (15–16 to 12–15 ms).
    • Most of what I attributed to notification fan-out is selector evaluation inside the shell's own render and mount, which the hold does not touch.
    • Untraced file.read start (main vs branch, 3 opens each): cold 111/113/114 vs 110/114/121; first reopen 104/105/138 vs 100/102/119; snapshot reopen 110/119/192 vs 98/99/118.
    • That is roughly 0–10 ms, about 5 ms typical. The smallest capped 2x floor (FZK cold, 6.6%) is about 33 ms, so a timed A/B cannot show this.
  • Capped A/B + A/A: not run (arms built: main 6a109eb861 vs 290c38d990); the expected gain is below the floors.
  • Every viewer test covering load start, reset, cancel/supersede and error paths, run explicitly (files + counts listed here).
  • Hold-lifetime proofs: a throw inside the reset, a re-entrant load, a subscriber that writes during the flush.

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Improvements
    • Viewer store updates during primary loads are now delivered together, reducing intermediate refreshes while load state changes remain immediate.
    • Federated loads retain their existing notification behavior.
  • Tests
    • Added coverage for batched notifications, nested holds, subscriber ordering, and primary-load outcomes. Some primary-load notification-hold tests are skipped.