perf(viewer): look a STEP file's cache entry up while the file is read (W1)

Closed
LO
19d ago
+449-55

What (W1)

The cache key is a spread-sampled fingerprint (head, tail, 8 interior windows, exact length). For a STEP file outside any archive, the bytes it samples are the file's own, so the same value comes from Blob slices. The loader used to wait for the whole-file read before it could even look the entry up.

A primary STEP open now computes that key from slices and looks the entry up. For a hit the hit gate serves, it starts adopting the stored scene while the read runs (apps/viewer/src/hooks/earlyCacheHit.ts).

Correctness: what is served is unchanged

  • Key: the loader still derives the key from the bytes it read. It uses the early lookup only under that key. A different key (bytes or key settings changed in between) abandons the early adoption and the load continues as before.
  • Gate: the same decideCacheHit (mtime guard; an entry with no mtime and no stored full-source identity is never served).
  • Content: the served hit is still revalidated in the background against the load's full-source identity, then purged and reloaded on mismatch (#4269).
  • Ownership: every exit that does not serve the early hit abandons it: key mismatch, miss, failed read, superseded load, the loader's finally. So the viewport never keeps a scene its load did not serve.
  • The key settings (merge layers, geometry mode, tier) are now read just before the file read instead of just after it. One derivation (geometryCacheKey, planCache) keys both lookups.

Why (pre-pixel itemisation, untimed, Pop real GPU, n=5, main 803ea549ef)

BWK snapshot reopen: the streamed whole-file read ran 162-500 ms on the main thread. The lookup ended at 628 ms, after the device was ready (536 ms), so on BWK it was on the first-pixel critical path. On Holter and advanced_model the lookup finished before the device; it becomes critical there once the device request moves to the pick (separate PR).

Tests (adversarial first)

apps/viewer/src/hooks/earlyCacheHit.test.ts (9, real renderer Scene on a stub device):

  • the slice key equals the full-read key, so an entry written before this change is found and adopted;
  • a same-size file differing inside a sampled window keys another entry: nothing is found or drawn;
  • a same-size edit between sample windows keys the same entry, and a changed mtime refuses it before anything is drawn;
  • the same edit with its mtime preserved is drawn as any hit is today. The stored identity is what the revalidation compares; the existing test "still purges and reloads when the stored hash disagrees" covers the reload, and it now goes through this path;
  • no mtime and no identity: never drawn early;
  • abandon() withdraws an adopted scene and stops one still waiting for the renderer (no upload); a superseded load adopts nothing; unreadable slices leave the lookup to the loader.

Mutations: removing the gate fails 2 tests; removing abandon-as-stale fails 1.

Loader level (useIfcLoader.sourceHashOnce.test.tsx, 2; both fail on main): the entry is looked up while the whole-file read is still held, and served once the bytes agree; a read that fails after the early lookup ends the load in error, serves nothing and keeps the entry.

The yielding whole-file read moved to #9914

That change (the >= 256 MB streamed read returning to the event loop) is split out into #9914: it is independent of the cache key, and it helps cold loads too. The measurements below were taken with it in the arm.

What this pair (#9826 + #9827) buys, and what it does not

Measured untimed: Pop real GPU, quiet-gated (every open at load < 6 and CPU pressure < 5), arms rotated over 5 repetitions. Base is 70ecd5a09c plus the identity hasher; the arm is base + #9826 + #9827. Medians in ms from the file pick, base -> arm:

celldevice readylookup endsfirst frameinteractive
BWK first reopen404 -> 286457 -> 253619 -> 5391595 -> 1582
BWK snapshot reopen397 -> 262447 -> 260602 -> 5521571 -> 1582
advanced_model first reopen272 -> 263159 -> 167435 -> 401868 -> 839
advanced_model snapshot reopen235 -> 233160 -> 164404 -> 369848 -> 814
Holter first reopen277 -> 286219 -> 196522 -> 5121833 -> 1859
Holter snapshot reopen266 -> 273216 -> 222513 -> 4841829 -> 1792
  • It is a first-pixels gain on files of 256 MB and above. On BWK the stored scene is on screen 50-80 ms sooner. Interactive does not move, so this is not a gain on the interactive goals.
  • The timed verdict cell is BWK first geometry on reopens, with no regressing cell anywhere, cold included. The timed A/B is pending.
  • Cold BWK, a second benefit of the yielding read: base has one main-thread long task of 187-254 ms starting 114-131 ms after the pick (reps 1-4; rep 0's early task is under 108 ms); this is the whole-file read. With the yielding read there is no early long task, and the long-task total before interactive drops from 1,240-1,302 ms to 1,009-1,047 ms. First frame and interactive on BWK cold are unchanged (1379 -> 1390, 3868 -> 3868 ms).
  • One untimed Holter cold sample read 8,031 ms. It started at 18:46:35 UTC, inside the fleet watchdog pass of 18:45:29 UTC, which ran the heavy build-tree sweep and checkout reclaim. Excluding it, Holter cold is 4611 (4561-4657) on base against 4611-5147 on this arm. The timed slot starts right after a watchdog pass and lists any sample that overlaps one.
  • Output: a reopen identity check against base reads match on all six cached fixtures, both sides and both reopens, on the 294df5e40a signature. With one stored snapshot byte flipped, it reads RESTORE MISMATCH everywhere. The check is being repeated with the widened #9863 signature (5e80d69c71) before this is labelled.

Status

The untimed itemisation shows a cold cost on Holter (+260 ms to interactive) and advanced_model (+106 ms), both under 256 MB, so it is not the yielding read. It is being attributed (#9826 alone vs #9827 alone, traced) before any timed slot.
This PR lands together with #9826. They are timed as one arm and share the verdict. mergify-trial is held until the widened identity check, the altered-snapshot control and the timed A/B pass.

šŸ¤– Generated with Claude Code