perf(cache,viewer): decode a cache hit's geometry chunks on the workers, alongside the metadata decode (stacked on #9782, #9785)

Closed
LO
18d ago
+885-159

Untraced expectation: about 75–110 ms off Holter's reopens, about 80–100 ms off BWK's. The 150–200 ms below came from a traced profile and is superseded. The parts, all measured:

  • Holter has 39 chunks and BWK 32; the chunk stage is ~193–196 ms wall on both (untraced).
  • In that stage the main thread spends ~130 ms (Holter) / ~143 ms (BWK) decoding untraced; the rest is waiting for inflate replies.
  • Receiving a worker's decoded chunk costs main ~44% of decoding it in place (Node, 1,458-mesh chunk: 2.9 vs 6.6 ms). So main saves ~73 / ~80 ms of work.
  • The prefetch hides the ~35 ms (Holter) / ~20 ms (BWK) of main idling on inflate replies.
  • The low end of the range allows for Chrome's structured-clone deserialisation being relatively slower than Node's.

It is timed together with #9932 (lazy relationship Maps, ~20 ms) as one arm: about 95–130 ms on Holter's reopens between them.

Stacked on #9782 (the chunk inflate pool) and #9785 (the mesh-array arena). This PR's own change is the top commit, 29ddbc9cd2. Until #9782 and #9785 land, the diff against main also shows theirs.

Why

On a Holter snapshot reopen, first pixels → metadata ready is 572–584 ms with #9782 + #9785. Measured on a sourcemapped build of main 52c82a099d + #9782 + #9785, slot C', untraced spans, 2 reps; the main thread is busy 93% of it, and the stages run one after another on it:

stagestartms
cache.read48445
cache.decode53094
cache.entityIndex62422
cache.spatial64654
chunk decode701 → ~913~210 wall
snapshot hydrate913103

The chunk decode is the mesh-record reads and their array copies (traced: readMeshRecord 125 ms, of which buffer copies 69). #9782 had already moved the inflate itself onto workers. This step moves the record decode too, and starts it while the metadata decodes.

Change

  • Worker op decode: the cache worker inflates (when compressed) and runs the same decodeChunkRecords the main thread runs. It returns the chunk's meshes in one message, transferring every distinct arena buffer (decoded-chunk-transfer.ts). Nothing is copied, and the worker keeps nothing.
  • Shared appearance-source pool: the section's pool is shared by every chunk on the main thread. Pooled sources travel as pool indices and re-link to the main thread's own pool arrays, so identity across chunks is exactly what a main-thread decode gives. Each worker is sent the pool once, not per chunk.
  • Prefetch: cacheChunkInflater starts decoding when the entry's header is read (prepare), with two chunks per worker in flight. The hit's in-order readChunk(i) takes each prefetched result. A chunk the caller reaches first is decoded on demand and never twice.
  • Failure rules, as for inflate:
    • a worker failure (crash, stall, bad reply) decodes that chunk and the rest on the calling thread, exactly as before;
    • a chunk that fails on a worker and again here is CorruptChunkError, and so is every later chunk;
    • no pool (the chunk did not load, or workers cannot start) means no prefetch and today's main-thread decode.
  • Lifetime: a load closed mid-decode (superseded, or its model removed or replaced) starts no further decode. Its pending decodes are dropped without unhandled rejections, and its workers stop.
  • API: ChunkInflatePool.decode, the decode option of openGeometryChunksV13, and the type ChunkDecode. The changeset marks @ifc-lite/cache minor.

Tests (local)

  • packages/cache: 30 files, 263 passed, 1 skipped. New in chunk-inflate-pool.test.ts:

    • worker decode is byte-identical to a main-thread decode, with pooled sources re-linked to the section pool across chunks;
    • the worker's buffers are detached after each reply;
    • a worker crash mid-decode decodes the rest here, unchanged;
    • a corrupt chunk is CorruptChunkError from the worker path, and later chunks reject too;
    • without workers, chunks decode as without the pool.
  • Viewer: the cache-hit files pass, 58 tests. In cacheChunkInflater.test.ts:

    • raw and compressed entries decode every chunk on the pool, ahead of the caller and once each;
    • at most two chunks per worker are in flight;
    • close mid-decode starts nothing more and leaves no unhandled rejection;
    • the fallback when the pool cannot load gives identical meshes.

    In useIfcCache.inflateOffMain.test.tsx: every chunk is decoded on a worker, with no separate inflate round trip. A load superseded mid-decode publishes no geometry and stops every worker.

Still to show before this is labelled

  • Reopen output identity with the widened signature (#9863), on all cached fixtures, plus the altered-snapshot control.
  • Peak memory on Holter and BWK reopens: workers now hold decoded arrays until they are transferred, released per chunk.
  • Timed, together with the hydrate step, in one slot against #9782 + #9785 (+ #9855 if on main by then): Holter, BWK and advanced_model reopens, with cold as the regression check.

🤖 Generated with Claude Code