perf(cache): decode a cached chunk's mesh arrays into shared arena blocks

Merged
LO
18d ago
+306-33

Peak memory on reopen (Holter, 170 MB), on top of #9782: 1,730 → 1,631 MiB on the first reopen (−5.7%) and 1,712 → 1,644 MiB on the snapshot reopen (−4.0%). Timed memory pass, slot D' on Pop: exclusive, 5 interleaved rounds, 0 of 32 samples under load. Base is #9782@f976a623a6 and branch #9782 + #9785, both built with the scoreboard identity commit. Cold peak memory shows no significant difference. Both verdicts hold without the 9 samples that overlapped fleet-watchdog passes (1,731 → 1,644 and 1,702 → 1,644 MiB). Together with #9782: 2,196 → 1,631 MiB on Holter's first reopen.

Timing on top of #9782 (slot C', 5 rounds):

  • BWK: 1,440 → 1,372 ms (+4.7%) and 1,443 → 1,345 ms (+6.8%), PASS, and both hold without watchdog-overlapped samples.
  • advanced_model: no significant difference.
  • Holter: +6.6% / +6.4%, but those cells are not evidence: round 1 caught a foreign rustc process and rounds 0 and 4 overlapped watchdog passes. A clean Holter rerun is slot 40.
  • Cold: no significant difference on all three.
  • Reopen output: identity matches (geometry-bytes signature).

Summary

Reopen lane, chunk-decode follow-up.

A cache hit's chunk decode allocated one ArrayBuffer per mesh array: three per mesh, about 170k on Holter. On a Holter first reopen (traced, exclusive slot), readBytes cost 91 ms of self time, plus garbage collection.

The cache records leave positions, normals and indices unaligned (they follow a variable-length type name and a one-byte field), so meshes can't be views into the chunk without a format change. Instead the decoder copies them into aligned ~1 MiB blocks (ArrayArena, opt-in on BufferReader); arrays over 256 KiB get their own buffer.

Direction (Holter-shaped data, 56,874 meshes in 39 chunks, untimed):

  • headless Chrome: 151 → 73 ms, median of 8 alternating rounds;
  • Node: no difference. The cost is Blink's per-ArrayBuffer allocation, not the copy.

Commits:

  1. fix(viewer): zone split pieces are posted as their own copies. The zone split client posts the live scene's mesh arrays without a transfer list, by design. A structured clone of a typed-array view copies its whole underlying buffer, so with arena blocks every split job would clone each block it touches. This commit is needed first.
  2. perf(cache): the arena itself.
  3. chore: export ArrayArena beside BufferReader and add it to the API surface.

Audit: everything that sends or reinterprets a mesh's typed arrays

  • Cloned without transfer: only zones/split-worker-client.ts, fixed in commit 1. No other main-thread path posts scene mesh arrays.
  • Source bytes, not mesh arrays:
    • remesh-client transfers the IFC source buffer, and only when it spans a whole buffer.
    • rhino-export-client and retained-client send source bytes, cloned on purpose.
    • The appearance planner sends source bytes plus plain ScanPoint arrays.
  • Owned arrays, unaffected:
    • scan outline and scan detector use the scan cache's own point arrays;
    • the LandXML and zone-split workers transfer arrays they just built back to the main thread.
  • Worker → main only: geometry-parallel, hung-job-recovery, native-bridge.
  • Serialisation into their own bytes: the snapshot writer and the cache writer. The renderer copies on upload (writeBuffer).
  • Reinterpreting a mesh array's whole .buffer: none. cesium-glb uses fresh arrays, and every other .buffer use on a mesh array passes byteOffset or is a worker transferring its own array.
  • Retention: a block stays alive while any of its meshes is alive. Peak and retained memory must be checked in the timed A/B, including the cold-storage eviction path.

Tests

  • split-worker-client.test.ts: posts pieces that view a 1 MiB block and checks the worker receives exactly each piece's bytes. Fails without commit 1 ("positions cloned alone, not their 1 MiB block").
  • geometry-chunks.arena.test.ts:
    • same values as the copy-per-array decode, 4-byte aligned;
    • fewer than 30 buffers instead of 900 (fails on the old reader by construction);
    • each mesh's arrays independent of the others in its block;
    • a plain reader still copies each array.
  • Cache suite 250 pass; viewer zones + cache-hit suites 272 pass; cache and viewer tsc clean; module-size check clean.

Output identity (scoreboard harness 597b33d034, Pop, 2026-11-12 23:33-00:01 CET)

Arms built with the identity commit: base main 5c7f195588, #9782 f976a623a6, #9782 + #9785 70aadf9b55. Fixtures Holter, BWK, advanced_model. Lanes: cold, then two reopens (warm1 = first reopen, warm). Both sides in every run.

  • Normal pass (base vs #9782; base vs #9782 + #9785): match on all 24 reopens ("pieces = cold, 1 adopted snapshot(s) = written").

  • Cold identical across sides and across all four runs:

    fixturepieceselementspiece siginstanced entitiessnapshot batches
    Holter109,64260,28578d24eee4666dd8a51,095277
    BWK42,34016,395bf5640358a9168c35,872228
    advanced_model11,2236,4017c4c1cb03b454a842,701100

    The written snapshot's own sig differs from run to run on the same base build (four values for main per fixture), so it is not an identity field. Reopens are judged against their own cold.

  • Altered-snapshot control (LOAD_SCOREBOARD_CORRUPT_SNAPSHOT=1, same arms): RESTORE MISMATCH vs cold on all 24 reopens, both sides. The check detects a one-byte change.

  • Untimed (identity only; not timing evidence). Raw: reopen-w2/id597-{normal,control}-{9782,9785}.

Head after the timed builds

3973b94726 (after 70aadf9b55, the head the timed arms and slot 40 build from) changes only .changeset/cache-chunk-arena.md. It states that decodeGeometryChunk / readChunk now return arena-backed views, and lists the in-repo .buffer users that were audited. No code changes, so the timing and identity evidence on 70aadf9b55 applies.

Arena opt-in (review decision (b)) and evidence on the shipped head

  • 2fb4e55aeb + ea30f6ee67:
    • decodeGeometryChunk, openGeometryChunksV13(...).readChunk and BinaryCacheReader.read return arrays that own their buffers by default, exactly as published.
    • { sharedArena: true } (new ChunkDecodeOptions, documented with the copy-before-transfer rule) turns the arena on.
    • The viewer passes it at its three decode call sites (cache hit, federated hit, cold-geometry provider). That is its whole viewer diff, so what the viewer runs is unchanged.
    • New tests: on the default path every array owns its buffer, and transferring one mesh's buffers leaves the others intact; BinaryCacheReader.read in both modes.
    • The API surface adds ChunkDecodeOptions.
  • Timed re-time on the shipped head 515536b0cd (arena opt-in + capacity hint). Base current main 53730fdab8 (with #9782), both arms + scoreboard identity 597b33d034, Pop, exclusive benchmark admission, 5 rounds.
    • Timing unit: 0 samples overlapped a watchdog pass, 0 under load.
      • Holter first reopen: interactive 1,583→1,481 ms (+6.4%, 2x floor 1.7%), UI quiet +4.9%.
      • Holter warm reopen: interactive 1,543→1,462 ms (+5.3%, 2x floor 3.5%), UI quiet +4.9%.
      • Holter cold: n.s.; first geometry n.s. on every lane.
      • ISSUE_129 (the closest small-chunk fixture we have: 19 chunks for a 12 MB IFC; none has hundreds of tiny chunks): first reopen +2.0% (gain); warm and cold n.s.
      • Identity: match on every reopen.
    • Memory unit (separate slot):
      • Holter reopen peaks 1,659→1,624 and 1,659→1,617 MiB (n.s. against the noise floor; below the earlier arm's 1,657 / 1,662); Holter cold n.s.
      • ISSUE_129 reopens 870→847 (gain) and 882→857 MiB (n.s.).
      • ISSUE_129 cold peak: 1,018→1,032 MiB (−1.4%, FLAG regression against its 1.3% 2x floor). It holds without the two branch rounds that overlapped a watchdog pass (branch median 1,032 of 1,032 / 1,047 / 1,025).
      • The cold trace runs none of this PR's code: a cache miss, no chunk decode, no zone split, and the cache writer is unchanged. The base rounds alone span 1,008-1,033 MiB.
    • Tie-breaker for the flagged cell (rule fixed before the run: label on n.s. or a gain; stay unlabelled on a repeat flag). ISSUE_129 cold peak memory, same two builds, 10 interleaved rounds, 08:37-08:47 CET:
      • 1,023→1,030 MiB (−0.6%, 2x floor 1.0%): no significant difference; cold output identical.
      • Without the 3 samples that overlapped a watchdog pass: 1,023→1,031 (+0.8%), still under the floor.
      • Base spread 1,017-1,045 MiB, branch 1,022-1,059.
    • Both runs, side by side: the 5-round memory unit flagged ISSUE_129 cold (1,018→1,032, −1.4% vs a 1.3% floor); the 10-round run reads n.s. No #9785 code runs on that cache-miss load.
  • Focused tests on ea30f6ee67 (pop-os, node v24.22.0): cache 29 files, 253 pass / 1 skip; viewer (cache hit and loader, snapshot load/write, chunk inflater, cold-geometry provider, zone split, search, overlays) 755 pass / 0 fail / 6 skip.
  • Identity normal pass on the opt-in (scoreboard harness 597b33d034, Pop, untimed, 03:25-03:38 CET).

🤖 Generated with Claude Code

Summary by CodeRabbit

  • New Features

    • Cache geometry decoding now supports an opt-in shared-memory mode. By default, decoded arrays continue to use separate buffers.
    • The cache package now exposes the shared-memory decoding option and supporting utilities.
  • Improvements

    • The viewer uses shared-memory decoding when loading cached geometry.
    • Zone-split mesh pieces are copied before being sent for processing, keeping the original scene arrays intact and avoiding unnecessary copying of larger backing buffers.
  • Tests

    • Added coverage for shared and default buffer behavior, and for sending zone-split pieces safely.