perf(geometry): budget geometry workers by whole-tab memory (huge files, stage 1)
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),deviceMemoryCeilingGBandGeometryMemoryBudgetError.
- Planning (picks the worker count):
packages/geometry/src/worker-count.ts:computeWorkerCountkeeps 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).
WorkerCountResultis unchanged. The plan fields are onWorkerCountPlan extends WorkerCountResult.assertGeometryMemoryFits(byteLength)is the opt-in refusal, usingparallelPoolAvailable(), the same checkprocessAdaptiveuses.
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
workerCountOverridenumber (and so ageomWorkersvalue remembered in localStorage) is still clipped by the memory budget, as on main. - Only
?geomWorkers=Nin the current page URL becomes{ count, beyondBudget: true }. That may spend the budget's headroom, but never more than the whole device.
- A plain
- Viewer (
apps/viewer/src/hooks/ingest/geometryEventSource.ts):- The exact kernel refuses a
.ifcbefore a byte is read, and an.ifczipafter unwrapping. This is the same seam the retained kernel uses, souseIfcLoader.tsis untouched and doesn't collide with #9617. - No refusal when a geometry server is configured.
- Retry tries the same file anyway.
- The exact kernel refuses a
- 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_GBis 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.
| reading | pool refuses above | serial refuses above |
|---|---|---|
| 0.25 / 0.5 / 1 GB | 128 / 256 / 512 MiB | 192 / 384 / 768 MiB |
| 2 GB | 1,024 MiB | 1,536 MiB |
| 4 GB | never below 2 GiB | never |
| 8, 16, 32, absent | never | never |
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
.ifcpicks 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 sharedprocessAdaptiveoptions now come fromadaptiveGeometryOptions.ts. The loader still passes the URL-awaregetGeomWorkerOverrideRequest(). 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: viewerfastPathRuntime.test.ts+pickStart.test.ts37/37, including the new IFCZIP spare-release regression; geometryworker-count.test.ts+worker-count.tab-budget.test.ts59/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.
| load | F | workers | measured | plan | residual |
|---|---|---|---|---|---|
| 1.03 GB synthetic replica | 985.7 | 4 / 1 | 9,219 / 6,523 | 9,686 / 6,633 | +5.1 / +1.7 % |
| 1.74 GB synthetic replica | 1,655.2 | 4 / 2 | 15,040 / 12,045 | 15,484 / 12,297 | +3.0 / +2.1 % |
| 2.09 GB synthetic replica | 1,990.0 | 4 | 18,136 | 18,383 | +1.4 % |
| real 343 MB (stage 0 / today) | 326.8 | 8 | 5,189 / 5,120 | 5,784 | +11.5 / +13.0 % |
| real 343 MB | 326.8 | 4 | 3,363 | 3,980 | +18.3 % |
| real 177 MB (max of 4 runs) | 169.2 | 8 | 3,834 | 3,878 | +1.1 % |
| real 2.4 / 11 / 34 / 47 / 54 MB (stage 0) | – | 8 | 884 / 1,041 / 1,374 / 1,493 / 1,679 | 1,859 / 1,969 / 2,237 / 2,399 / 2,480 | +110 / +89 / +63 / +61 / +48 % |
| real 343 MB, post-geometry | 326.8 | 1 / 2 | 3,237 / 3,015 | 2,627 / 3,078 | −18.9 / +2.1 % |
| real 177 MB, post-geometry | 169.2 | 1 / 2 / 4 | 2,931 / 3,081 / 3,338 | 1,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 @2 | 6,456 / 10,212 / 3,429 / 3,228 | 6,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.
| host | 5 MB | 20 | 54 | 100 | 200 | 343 | 400 | 512 | 722 | 986 | 1200 | 1655 | 1990 MB |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 4c / 2 GB | 2 | 2 | 2→1 | 2→1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 (refused) | 1 (refused) | 1 (refused) |
| 10c / 2 GB | 4 | 8→7 | 4 | 4 | 4→1 | 4→1 | 4→1 | 4→1 | 3→1 | 3→1 | 3→1 (refused) | 3→1 (refused) | 2→1 (refused) |
| 4c / 4 GB | 2 | 2 | 2 | 2 | 2 | 2 | 2 | 2→1 | 1 | 1 | 1 | 1 | 1 |
| 8c / 8 GB | 3 | 3 | 3 | 3 | 3 | 3 | 3 | 3 | 2 | 2→1 | 1 | 1 | 1 |
| 10c / 16 GB | 4 | 8 | 4 | 4 | 4 | 4 | 4 | 4 | 3 | 3 | 3 | 3→1 | 2→1 |
| 16c / 16 GB | 8 | 8 | 8 | 8 | 8 | 8 | 8 | 8 | 4 | 4 | 4 | 3→1 | 2→1 |
| 16c / 32 GB, 32c / 32 GB | 8 | 8 | 8 | 8 | 8 | 8 | 8 | 8 | 4 | 4 | 4 | 4 | 4 |
| 8c / unknown (Firefox, Safari) | 3 | 3 | 3 | 3 | 3 | 3 | 3 | 3 | 2 | 2→1 | 1 | 1 | 1 |
| 10c / unknown | 4 | 8 | 4 | 4 | 4 | 4 | 4 | 4 | 3 | 3 | 3 | 3→1 | 2→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, sha25683b20d36…). - Before vs after. Pop reports
deviceMemory32, where the policy keeps main's worker counts, so the default runs are the "before" side. The?perf.geomMemoryGB=Nruns 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 themain@004934fa88build's signature for the same file (earlier runs).
| file | run | workers (bound) | plan / budget MiB | peak PSS MiB | renderer HWM | wasm Σ MB | interactive | elements · signature |
|---|---|---|---|---|---|---|---|---|
| Holter 177 MB (real) | default | 8 (cores) = main | 3,878 / 24,576 | 3,855 | 3,585 | 2,174 | 27.0 s | 60,912 · 453e6906b5dbba5a |
| sim. 4 GB | 5 (memory) | 2,931 / 3,072 | 3,739 | 3,342 | 1,431 | 49.4 s | identical | |
| BWK 343 MB (real) | default | 8 (cores) = main | 5,784 / 24,576 | 4,934 | 4,647 | 3,275 | 18.9 s | 16,756 · f361b4ab5580edd9 |
| sim. 4 GB | 2 (two-worker floor) | 3,078 / 3,072 | 3,045 | 2,575 | 892 | 22.3 s | identical | |
| x3 1.03 GB (synthetic) | default | 4 (cores) = main | 9,686 / 24,576 | 9,192 | 8,840 | 5,577 | 60.4 s | 50,268 · 94706c8da4e1596b |
| sim. 8 GB | 1 (memory) | 6,633 / 6,144 | 6,600 | 6,208 | 1,390 | 81.0 s | identical | |
| x5 1.74 GB (synthetic) | default | 4 (cores) = main | 15,484 / 24,576 | 14,956 | 14,596 | 9,111 | 93.9 s | 83,780 · d4540e03c2887c71 |
| sim. 16 GB | 1 (memory) | 10,704 / 12,288 | 10,413 | 10,019 | 2,170 | 106.8 s | identical | |
| sim. 0.001 GB | 1 (memory), not refused | 10,704 / – | 11,230 | 10,816 | 2,170 | 148.0 s | identical |
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.001loaded 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
1e8a6255against themain@004934fa88build) is +2,959 bytes eager, +54 entry chunk. CI'sPerf ratchet ceilingsmeasures 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;
WorkerCountResultstub 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;.ifczipis 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: viewerfastPathRuntime.test.ts+pickStart.test.ts, 37/37; geometryworker-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
Testrun 43593846134 passed;Build packages + WASMsucceeded andNode tests (shard 4/4)passed the@ifc-lite/geometry:testsuite (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, sourcec4709881), all four shards pass, 0 failures:shard tests pass fail skipped 0/4 5,751 5,649 0 102 1/4 5,778 5,672 0 106 2/4 5,035 4,922 0 113 3/4 6,201 6,183 0 18 -
Geometry and every package that drives the pool, forced build on Pop, served wasm
83b20d36…= source build, 70/70 Turbo tasks:package result geometry 717/717 cli 1,657 passed, 15 skipped export 2,045 passed, 60 skipped mcp 782 passed, 5 skipped sdk 512 passed, 7 skipped create-ifc-lite 134/134 viewer-embed 270/270 The only direct workspace importer of
worker-count/tab-memory-budget/perf-flagsis the viewer. -
CI on this head:
Testrun 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 typecheckpassed on the fleet at1e8a6255(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.tsstill charges concurrent loads its own 4 × F. This is unchanged from main; a follow-up should chargeestimateTabPeakMiB.
🤖 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.