perf(geometry): budget geometry workers by whole-tab memory (huge files, stage 1)

Closed
LO
18d ago
+1114-157

What

This is stage 1 of loading 1–2 GB IFC files in the browser without a server. The number of geometry workers is now chosen from a budget for the whole tab's memory, not from a per-worker cap. Refusing a load uses a separate bound that the model cannot get wrong.

Every wasm instance (the pre-pass worker and each geometry worker) holds a full copy of the source, and all of them live in the page's renderer process. So the tab's peak grows with workers × file size. On main, a 1.03 GB file peaked at about 9 × F of summed Chrome PSS with 4 workers, 97 % of it in one process. The old cap counted 1.5·F per worker and 2.5·F for the main thread, and had no pre-pass term, so it under-counted that by about half.

  • packages/geometry/src/tab-memory-budget.ts (new) holds two estimates:
    • Planning (picks the worker count): 300 MiB + 4.36·F + (N+1)·(170 MiB + 0.86·F). It is a fitted planning estimate with a measured margin, not a guaranteed upper bound. It sits at or above the 13 cold loads it was calibrated on, but one later run was 4.9 % above it (see Evidence). Refusal never depends on it.
    • Refusal, a strict lower bound: the full source copies of the path that will run. That is 3·F for the worker pool and 2·F for the serial fallback.
    • Plus the budget (75 % of deviceMemory, at least 1 GiB of headroom: the old headroom rule), deviceMemoryCeilingGB and GeometryMemoryBudgetError.
  • packages/geometry/src/worker-count.ts:
    • computeWorkerCount keeps the cores and thermal tiers, then lowers them to the largest N whose planning estimate fits. It never goes below one.
    • Up to 512 MiB it keeps two workers whenever they fit the whole device (≥ 4 GiB), because real mid-size models peak after geometry (see (b) below).
    • WorkerCountResult is unchanged. The plan fields are on WorkerCountPlan extends WorkerCountResult.
    • assertGeometryMemoryFits(byteLength) is the opt-in refusal, using parallelPoolAvailable(), the same check processAdaptive uses.
  • packages/geometry/src/geometry-parallel.ts: plans first, before the SAB copy and the wasm compile, and logs the plan. It never refuses.
  • Overrides.
    • A plain workerCountOverride number (and so a geomWorkers value remembered in localStorage) is still clipped by the memory budget, as on main.
    • Only ?geomWorkers=N in the current page URL becomes { count, beyondBudget: true }. That may spend the budget's headroom, but never more than the whole device.
  • Viewer (apps/viewer/src/hooks/ingest/geometryEventSource.ts):
    • The exact kernel refuses a .ifc before a byte is read, and an .ifczip after unwrapping. This is the same seam the retained kernel uses, so useIfcLoader.ts is untouched and doesn't collide with #9617.
    • No refusal when a geometry server is configured.
    • Retry tries the same file anyway.
  • Message in en/de/fr/it/ru: "This model needs at least {required} of memory to load in the browser, more than this device reports ({available} at most). Choose Retry to try anyway (the load may run out of memory), open it on a computer with more memory, or use the native CLI or server." It is distinct from #9617's 2 GiB message and from the generic out-of-memory text. Required rounds up and available rounds down.
  • Memory simulation. ?perf.geomMemoryGB=N / __IFC_LITE_GEOM_MEMORY_GB is a registered perf flag that only caps the planning memory. It can lower the worker count; it cannot refuse a load, raise the budget, or change what a refusal says. Values outside (0, 4096] GiB are ignored.

When it refuses

A load is refused only when 3·F (pool) or 2·F (serial) exceeds the most RAM the deviceMemory reading allows. Chrome reports the nearest power of two (desktop range 2–32, Android 1–8, older builds at most 8), so a reading v below the clamp allows at most 1.5 v. Readings of 8, 32 or more, and absent readings (Firefox, Safari), never refuse.

readingpool refuses aboveserial refuses above
0.25 / 0.5 / 1 GB128 / 256 / 512 MiB192 / 384 / 768 MiB
2 GB1,024 MiB1,536 MiB
4 GBnever below 2 GiBnever
8, 16, 32, absentnevernever

After #9900 (merged main at a5ec5d179a)

#9900 added two idle spares: a parser worker and a fast-path geometry worker. How they fit the whole-tab budget:

  • The parser spare takes the place of the parser worker the load would spawn anyway, so the steady state is unchanged. It carries only the small leftover from one warm parse of a shipped sample. No change here.
  • The geometry spare is a single engine session that serves only .ifc picks below 10 MiB. It never joins the pool. On main a ready spare stayed alive through every larger load, as one more engine instance the budget does not count. Now a pick at or above 10 MiB, primary or federated, terminates it before the load starts its workers, and it is refilled at idle after the load as before. Ineligible picks below the bound keep it, as on main.
  • Rejected proposal: an alternative was to terminate "spares beyond the budget" once the size is known. It does not fit #9900: the spares are not pool workers, and a spare in use is by definition a small file, where the budget never binds.
  • Merge conflict: the memory refusal stays in geometryEventSource.ts. The shared processAdaptive options now come from adaptiveGeometryOptions.ts. The loader still passes the URL-aware getGeomWorkerOverrideRequest(). The fast path keeps posting a plain number, which is always budget-clipped and irrelevant below 10 MiB.
  • **Tests run directly on this head through root pnpm test/Turbo: viewer fastPathRuntime.test.ts + pickStart.test.ts 37/37, including the new IFCZIP spare-release regression; geometry worker-count.test.ts + worker-count.tab-budget.test.ts 59/59. The other 45 geometry tests require a built wasm package, unavailable locally; see the current-head CI job below.
  • Still pending: the private-corpus browser confirmation of the worker-count rule. It runs as capped units after #9989's timed run.

Model accuracy: every measured load

The planning estimate is a fitted estimate with a measured margin, not a guaranteed upper bound.

  • Calibration set. It sits at or above all 13 geometry-dominated calibration loads below, by +1.1 % to +13 % where budgets bind.
  • Observed miss. On the frozen head, x5 at 1 worker measured 11,230 MiB against 10,704 planned, 4.9 % under. A second identical run measured 10,413 MiB, so the spread between identical runs on a loaded host is about 8 %.
  • What an under-estimate can cost. At most a worker count one too high near a budget boundary.
  • Refusal never depends on this estimate. It uses the 3 × / 2 × source-copy floor.
  • Follow-up. A PR will raise the per-instance term from repeated quiet-host measurements, so that the estimate covers run-to-run spread too.

Peak summed Chrome PSS, Chrome 156, Pop. F is in MiB. The plan constants are the ones in the code. "Post-geometry" rows are real mid-size models at 1–2 workers, whose peak comes after geometry and does not depend on N; they are outside what the estimate covers.

loadFworkersmeasuredplanresidual
1.03 GB synthetic replica985.74 / 19,219 / 6,5239,686 / 6,633+5.1 / +1.7 %
1.74 GB synthetic replica1,655.24 / 215,040 / 12,04515,484 / 12,297+3.0 / +2.1 %
2.09 GB synthetic replica1,990.0418,13618,383+1.4 %
real 343 MB (stage 0 / today)326.885,189 / 5,1205,784+11.5 / +13.0 %
real 343 MB326.843,3633,980+18.3 %
real 177 MB (max of 4 runs)169.283,8343,878+1.1 %
real 2.4 / 11 / 34 / 47 / 54 MB (stage 0)–8884 / 1,041 / 1,374 / 1,493 / 1,6791,859 / 1,969 / 2,237 / 2,399 / 2,480+110 / +89 / +63 / +61 / +48 %
real 343 MB, post-geometry326.81 / 23,237 / 3,0152,627 / 3,078−18.9 / +2.1 %
real 177 MB, post-geometry169.21 / 2 / 42,931 / 3,081 / 3,3381,669 / 1,985 / 2,616−43.1 / −35.6 / −21.6 %
final-head runs (below): x3 @1, x5 @1, real 177 MB @5, real 343 MB @26,456 / 10,212 / 3,429 / 3,2286,633 / 10,704 / 2,931 / 3,078+2.7 / +4.8 / −14.5 / −4.6 %

Validity (stated in the code): files 2 MB – 2 GB, architectural content, Chrome. The estimate covers the geometry phase, the part that grows with N. A size-only estimate must over-predict sparse files to cover denser ones. This one does so by +1–5 % on the huge replicas and up to +110 % on a 2 MB model. That costs workers in places (below), never a refusal.

Before/after worker counts

Rows are cores / reported deviceMemory; cells are workers on main → this PR. Cells not marked are unchanged. Of the device-memory readings in this table, only 2 GB and 4 GB can refuse. The 2 GB rows below are the 4-core and 10-core 2 GB hosts.

host5 MB2054100200343400512722986120016551990 MB
4c / 2 GB222→12→11111111 (refused)1 (refused)1 (refused)
10c / 2 GB48→7444→14→14→14→13→13→13→1 (refused)3→1 (refused)2→1 (refused)
4c / 4 GB22222222→111111
8c / 8 GB3333333322→1111
10c / 16 GB484444443333→12→1
16c / 16 GB888888884443→12→1
16c / 32 GB, 32c / 32 GB8888888844444
8c / unknown (Firefox, Safari)3333333322→1111
10c / unknown484444443333→12→1
  • Property check. Over 179,456 cases (deviceMemory 0.5–32 or unknown × cores 1–32 × 0–2,100 MB) against main's function, the count never rises, and it changes only where main's count exceeds the new budget. The independent review's own 179,456-case sweep confirmed both properties.
  • Where the conservative estimate costs workers (measured, see Evidence): 1.74 GB on a 16 GB device now plans 1 worker; a measured 2 (12,045 MiB) would have fit. A 20 MB file on a 10-core 2 GB device goes 8 → 7.
  • 400 MB on a 4 GB device keeps main's 2 workers: the two-worker floor applies up to ≈ 473 MiB on 4 GB.

Evidence (frozen head c4709881, one build)

Untimed real loads via ~/build-workers/ifc-gpu-check on Pop (NVIDIA via Vulkan, Chrome 156.0.8078.4, fresh profile per run, cold, ?perfTrace=1&perfMem=1, whole browser capped at 32 GiB).

  • Build. Every run uses the forced build of the frozen head c4709881 (served wasm = its source build, sha256 83b20d36…).
  • Before vs after. Pop reports deviceMemory 32, where the policy keeps main's worker counts, so the default runs are the "before" side. The ?perf.geomMemoryGB=N runs are the reduced counts on the same build.
  • Times are diagnostic only. They ran beside the full viewer suite on the same host, which slowed everything 2–4×.
  • Per-element identity. An order-independent signature over every entity's RTC-invariant geometry hash (#924), from flat meshes or instancedGeometryHashes. Every signature below also equals the main@004934fa88 build's signature for the same file (earlier runs).
filerunworkers (bound)plan / budget MiBpeak PSS MiBrenderer HWMwasm Σ MBinteractiveelements · signature
Holter 177 MB (real)default8 (cores) = main3,878 / 24,5763,8553,5852,17427.0 s60,912 · 453e6906b5dbba5a
sim. 4 GB5 (memory)2,931 / 3,0723,7393,3421,43149.4 sidentical
BWK 343 MB (real)default8 (cores) = main5,784 / 24,5764,9344,6473,27518.9 s16,756 · f361b4ab5580edd9
sim. 4 GB2 (two-worker floor)3,078 / 3,0723,0452,57589222.3 sidentical
x3 1.03 GB (synthetic)default4 (cores) = main9,686 / 24,5769,1928,8405,57760.4 s50,268 · 94706c8da4e1596b
sim. 8 GB1 (memory)6,633 / 6,1446,6006,2081,39081.0 sidentical
x5 1.74 GB (synthetic)default4 (cores) = main15,484 / 24,57614,95614,5969,11193.9 s83,780 · d4540e03c2887c71
sim. 16 GB1 (memory)10,704 / 12,28810,41310,0192,170106.8 sidentical
sim. 0.001 GB1 (memory), not refused10,704 / –11,23010,8162,170148.0 sidentical

What it buys and costs:

  • Memory. On the same build, 4 → 1 worker cut peak PSS by 2.5 GiB (−28 %) at 1.03 GB, and by 4.4 GiB (−30 %) at 1.74 GB. There, main's count would need 14.6 GiB, which does not fit a 16 GB machine. Every load completed with identical per-element output.
  • Time. Fewer workers lengthen the geometry stream, but these times are not comparable (loaded host). In the earlier, less loaded runs one worker took 2–2.5× the interactive time at 1–1.74 GB. That trade happens only where the estimate says the faster count would not fit. The mid-size floor exists because one worker bought nothing on real mid-size models.
  • Simulation. ?perf.geomMemoryGB=0.001 loaded the 1.74 GB file with 1 worker and did not refuse. Refusal needs a real low reading, which Pop cannot produce; it is covered by tests (pool vs serial path, store, error card, try-anyway Retry).
  • The miss in numbers. The two x5 runs at 1 worker measured 10,413 and 11,230 MiB under the same plan of 10,704 MiB: an ≈ 8 % spread between identical configurations on a loaded host. The second is 4.9 % above the planning estimate. The estimate is therefore a fitted planning estimate with a measured margin, not an upper bound. The refusal is unaffected: it uses the source-copy floor, 3 × F ≈ 4,966 MiB here. The head is frozen, so this PR keeps the constants.
  • Eager viewer JS. This PR's own delta (measured at 1e8a6255 against the main@004934fa88 build) is +2,959 bytes eager, +54 entry chunk. CI's Perf ratchet ceilings measures the merge with current main within its allowance.

Raw runs and the driver are kept outside the repo (evidence-c4709881/*/{result.json,console.log,progress.ndjson}, evidence-frozen.sh, evidence-rung.{sh,mjs}). The earlier per-head evidence (evidence-0be13ea4/, evidence-1e8a6255/, real-calib/) was collected on superseded builds and is not cited as final.

Tests

  • packages/geometry/src/worker-count.tab-budget.test.ts:

    • the planning estimate is at or above each of the 13 calibration loads (this is a pinned property of that set, not a guarantee for other loads), and stays within +15 % where budgets bind;
    • the refusal floor is below every measured load;
    • the table above, plus the mid-size floor;
    • WorkerCountResult stub and exhaustive-reason compatibility;
    • remembered vs explicit overrides;
    • non-finite and overflowing inputs;
    • refusal only on the floor of the path that will run (pool vs serial vs one core);
    • the simulation cannot refuse or raise the budget.

    20 of these fail against bdfdedd980's modules (the head the independent review checked).

  • worker-count.test.ts: main's "override is still clamped by the memory budget (cannot OOM)" is restored; the 4 GB mid-size test is updated.

  • perf-flags.test.ts: the new binding is pinned, and its reader accepts only (0, 4096].

  • Viewer:

    • geometryEventSource.memory.test.ts: refusal before the read; Retry tries anyway; unknown or clamped readings never refuse; the simulation never refuses; .ifczip is checked after unwrap.
    • geomWorkerOverride.test.ts: a remembered override stays a plain number; a URL override is beyond-budget.
    • load-error-message.device-memory.test.ts: the message, its rounding, and all four shipped locales.

Current-head validation (merge head b647e524, by receipt)

  • Direct changed-module suites via root pnpm test/Turbo: viewer fastPathRuntime.test.ts + pickStart.test.ts, 37/37; geometry worker-count.test.ts + worker-count.tab-budget.test.ts, 59/59.
  • The other 45 geometry tests require a built WASM package and could not run locally. Current-head Test run 43593846134 passed; Build packages + WASM succeeded and Node tests (shard 4/4) passed the @ifc-lite/geometry:test suite (92 files, 729/729 tests), including those WASM-dependent tests.

Validation (frozen head c4709881, by receipt)

  • Full viewer suite, four-shard helper on Pop (job ffb77d68, source c4709881), all four shards pass, 0 failures:

    shardtestspassfailskipped
    0/45,7515,6490102
    1/45,7785,6720106
    2/45,0354,9220113
    3/46,2016,183018
  • Geometry and every package that drives the pool, forced build on Pop, served wasm 83b20d36… = source build, 70/70 Turbo tasks:

    packageresult
    geometry717/717
    cli1,657 passed, 15 skipped
    export2,045 passed, 60 skipped
    mcp782 passed, 5 skipped
    sdk512 passed, 7 skipped
    create-ifc-lite134/134
    viewer-embed270/270

    The only direct workspace importer of worker-count / tab-memory-budget / perf-flags is the viewer.

  • CI on this head: Test run 42959435361 passed every job (Typecheck, Lint, Node guards, Changed tests observe production, Perf ratchet ceilings, Build packages + WASM, docs, semver, aggregate); the later same-head run re-ran only the guards and aggregate.

  • Retained-free viewer check on the frozen-head dist: pass (1,152 files).

  • Root pnpm typecheck passed on the fleet at 1e8a6255 (all 4,797 test files in a typecheck program); the final-head typecheck runs in CI.

  • This PR no longer edits scripts/perf/README.md. Its ledger entry is on the shared ledger branch (#9673, 628a9c1064).

Wording fixed in the follow-up, not here (head frozen)

These files in this PR still describe the planning estimate as an "upper envelope", or say it "never under-predicts" / was "raised until no measured load is under-predicted". That is true only of the 13 calibration loads:

  • packages/geometry/src/tab-memory-budget.ts, comments at lines 9, 19 and 46;
  • worker-count.tab-budget.test.ts, test name at line 70;
  • docs/architecture/geometry-pipeline.md:661;
  • .changeset/geometry-tab-memory-budget.md.

The follow-up PR corrects them together with the constants, and adds the measured loads as a pinned regression table.

Not verified

  • No real file of 1 GB or more from an authoring tool. Above 343 MB the evidence is synthetic replicas of a sparse model, so dense files can still hit the per-instance 4 GiB wasm limit before this budget binds (the recorded 1.6–1.8 GB traps).
  • No Firefox, Safari, WebView or iframe-without-SAB run, and no real low-memory device; low memory is simulated. A real refusal is exercised only in tests.
  • federationLoadGate.ts still charges concurrent loads its own 4 × F. This is unchanged from main; a follow-up should charge estimateTabPeakMiB.

🤖 Generated with Claude Code

Summary by CodeRabbit

  • New Features
    • Geometry processing now adjusts worker counts to fit estimated browser-tab memory budgets.
    • Models that exceed available device memory can be refused before loading, with an option to retry.
    • Worker-count overrides can request counts beyond the planning budget, while remaining subject to device limits.
  • Documentation
    • Updated guidance explains memory-based worker planning, load checks, and override behavior.