perf(viewer): a large file's streamed read yields to the event loop
Result: on BWK (327 MB) the file read's 187-254 ms main-thread task is gone, and the long-task total before interactive drops from about 1,250 ms to about 1,030 ms (1,240-1,302 -> 1,009-1,047 ms, traced cold loads). This change doesn't move a timed cell; the timed A/B below is evidence that nothing regresses.
What
A file of 256 MB or more is read by streaming it into a SharedArrayBuffer (acquireFileBuffer). Its chunks are already buffered, so every reader.read() resolved as a microtask and the whole copy ran as one main-thread task. The loop now returns to the event loop through a MessageChannel task (no setTimeout clamping) after each chunk that brings the bytes copied since the last yield to 8 MiB or more. It yields between chunks, never inside one: a stream that delivered the whole file as one chunk would still be copied in one task. Chromium's file streams deliver many small chunks, and the BWK traces below show the long task gone. The bytes and the short-read check are unchanged. Files under 256 MB use file.arrayBuffer() and do not take this path.
Split out of #9827 (pre-pixel lane): it has nothing to do with the cache key and it helps cold loads as much as reopens.
Why (untimed, Pop real GPU, quiet-gated: every open at load < 6 and CPU pressure < 5; 5 rotated repetitions)
BWK (327 MB) right after the interface reset:
- Cold: 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 read. With the change 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.
- Reopens: the renderer's device response waited behind that task (device ready at about 400 ms). Measured with the cache-key change (#9827) in the same arm, the device was ready at 262-289 ms and the first frame came 50-80 ms sooner. This PR's own number comes from its timed A/B below.
- First frame and interactive on BWK cold: no change seen (1379 -> 1395 and 3868 -> 3873 ms, medians).
No-regression evidence (timed A/B: exclusive benchmark admission on Pop, main@1880ba1d6b vs this head 3d67fea18b, 5 rounds)
Interactive ms, median base -> branch (gain; 2x noise floor). Every cell is "no significant difference":
| fixture | cold | first reopen | warm reopen |
|---|---|---|---|
| BWK (327 MB, takes the path) | 3,393 -> 3,335 (+1.7%; 5.6%) | 1,506 -> 1,489 (+1.1%; 6.4%) | 1,501 -> 1,509 (-0.5%; 4.1%) |
| Holter (169 MB, control) | 3,754 -> 3,875 (-3.2%; 10.9%) | 1,746 -> 1,773 (-1.6%; 6.2%) | 1,726 -> 1,756 (-1.7%; 6.6%) |
| FZK (2.4 MB, control) | 563 -> 579 (-2.8%; 3.3%) | 558 -> 562 (-0.7%; 2.1%) | 551 -> 567 (-2.8%; 6.2%) |
First geometry and UI quiet show no significant difference either, except one gain: Holter first-reopen UI quiet, +3.0%. Element geometry hashes are IDENTICAL on cold BWK and Holter and on all three FZK lanes (10/10 samples each). The BWK and Holter reopens can't be hash-checked, because main has no reopen tap. No sample overlaps a fleet-watchdog pass: each per-fixture unit started 17-18 s after a pass ended (BWK 00:18:23, Holter 00:28:08, FZK 01:58:25 CET).
Tests
apps/viewer/src/utils/acquireFileBuffer.test.ts: a fully buffered 20 MiB stream lets a task queued before the read run before the read ends. It fails without the change. The existing byte-identity, short-read, error and fallback tests pass (7/7).
š¤ Generated with Claude Code