perf(parser): build the hierarchy relationship gate on first use

Merged
LO
19d ago
+79-14

Base main (#9840 merged 2026-11-13 as 6cbd92c; rebased off its commits, patch-identical). Fourth piece of the parser worker start-up work (#9838, #9839, #9840).

What changes

  • HIERARCHY_REL_TYPES was a module-level Set built from getAllConcreteRelationshipTypes(), which walks all three schema registries. With #9839 the registries decode on first use, and this was the one thing still decoding all three whenever the parser loaded. The browser parser worker paid for that before it could accept a parse, whatever schema the file declared. The set is now returned by buildHierarchyRelTypes() through a memoised getHierarchyRelTypes(). The categoriser reads it once per preparation, and getCategory already caches per type. It was not a public export.
  • scripts/lib/server-browser-type-extractors.mjs reads this declaration's source text to know what the TS gate admits. It now accepts the builder form (function build<Name>() { return new Set([...]) }) for each recognised set, with the same schema-derived marker check. Its sibling guard also runs over builders, so a 4th *_REL_TYPES set written lazily is caught just like a 4th constant. The existing under-read test is re-anchored on the builder. Two new mutation tests, a lazy sibling builder and a renamed hierarchy builder, both turn the gate red.

Evidence

  • Same gate contents: on main's source, main's extractor reads 55 relationship types; on this source, this extractor reads the identical 55.
  • check-server-browser-type-parity.test.mjs locally: 34 pass, 0 fail, 13 skipped (those need a built parser dist; the fleet run covers them).
  • Node load of the parser worker bundle (standalone build of the worker entry, 10 interleaved runs, untimed, host load ~22 so absolute values are inflated): #9839+#9840 85 ms median → with this 46 ms. Main is 287 ms, and a bundle with no tables at all is 31 ms.

Where the saved work goes (decode-location trace, untimed)

An instrumented build of #9839 + #9840 (without this PR), cold loads under host load ~17, performance.measure around every decode, by thread and phase:

  • Parser worker: the hierarchy walk plus its three registry decodes is the largest single item, 32-64 ms. The worker is spawned at the open, so it runs inside the timed open either way. This PR moves it from worker load to the first categorisation. That shortens the worker's start-up, but the parse still pays it. It is a wall-time gain only where the worker would otherwise wait for the entity index.
  • Where it saves work outright: the main thread's boot (13-21 ms for the walk, plus the registry decodes it forces if nothing else reads them at boot), and the overlay and source-compression workers. Those workers computed the set (25-33 ms each) without ever categorising a file.
  • The shared four-PR timed slot (boot lane included) decides whether this shows up end to end.

Scope of "decoded on first use"

The laziness described here holds in bundles, such as the viewer or any tree-shaking build: there, an exported table constant that nothing reads is dropped, and the getter decodes on first call. An unbundled Node import still evaluates each exported constant (ENTITIES_*, IFC_DATA_TYPES, SCHEMA_REGISTRY, …) when its module loads, as it evaluated the object literal before. /* @__PURE__ */ lets a bundler drop an unused constant; it does not defer one. In Node the decode is still cheaper than the literal it replaces. Module load, untimed, built dist, 15 interleaved fresh processes: @ifc-lite/data 56 → 42 ms, @ifc-lite/parser 228 → 162 ms (#9839 + #9840).

Landing grounds (shared by #9838, #9839, #9840, #9841)

  • No regressing cell in any lane of the shared timed slot (below).
  • The goal cell did not reach significance: FZK cold interactive, +11.8% against a 21.7% bar. This stack is not a load-time gain on the goal and is not claimed as one.
  • What it does deliver, measured:
    • Bundle (production build, base 6636d36476 vs all four):
      • main-thread eager JS: 17,550,991 → 12,613,295 raw bytes;
      • parser worker: 4,621,251 → 1,447,760 raw bytes (brotli 233,720 → 205,394).
      • The eager JS's brotli size is flat (2,963,206 → 2,982,953, +0.7%): this is less JS to parse and compile, not less to download.
    • Idle JS heap: 60 → 49 MiB (boot lane, no file opened).
    • Boot app-shell ready: 590 → 473 ms. That is above its own floor, but the boot cell has no A/A floor.
    • BWK first-reopen peak memory: 1,779 → 1,720 MiB (passes at 2x its floor).
  • Landing order: #9838, then #9839, #9840, #9841. Each is retargeted to main as the one below merges, and labelled only when its own shipped head has: package validation green; the 25-file focused viewer run green; the BulkPropertyEditor isolation runs explained (#9840); 0 unresolved review threads; CI green.

Timed verdict, shared by #9838 + #9839 + #9840 + #9841

One exclusive slot on Pop (cd8b3235, Balanced profile, maximum 5.3 GHz, no sample flagged for load): main 6636d36476 against all four PRs on the same base (b355265c3b). FZK, Holter, BWK and advanced_model; cold, first and second reopen; 5 rounds; boot lane; memory pass.

  • Regressions: none.
  • Significant gain (at least 2x the noise floor): BWK first-reopen peak memory, 1,779 → 1,720 MiB (+3.3%, 2x floor 3.0%).
  • Goal cell, FZK cold interactive: 598 → 528 ms (+11.8%), inside its 21.7% 2x floor, so no significant difference. FZK cold peak memory 902 → 828 MiB (+8.2%; 2x floor 8.6%, just short).
  • Boot lane (no file opened): app-shell ready 590 → 473 ms (+19.7%; own floor 7.4%). Idle JS heap 60 → 49 MiB; idle agent memory 62 → 50 MiB. Inconclusive by rule: the boot cell has no A/A floor.
  • Output identity: cold lanes identical on all four fixtures. Reopens are unverifiable by design (the cache path carries no element hashes).
  • FZK cold, where the (non-significant) difference went: the metadata path settles about 80 ms earlier (store settled 576 → 495 ms median). Geometry finalisation now finishes about 84 ms later (442 → 526 ms), so FZK ends on geometry instead of metadata. The earlier parser result appears to contend with geometry finalisation on the main thread; this run does not establish that.

Validation on the shipped head

Everything below ran on the shipped head b7c8b361be, which is this PR rebased onto main 88606e59b2 after #9840 merged:

  • Package validation (fleet): ALL PASSED. Build packages; typecheck; data, parser and codegen tests; check-codegen-sync, check-api-surface, check-server-browser-type-parity, the perf-ratchet tests and pre-handoff, based on 88606e59b2.
  • Focused viewer run (fleet): 25/25 files, 259 tests, 255 passed, 0 failed, 4 skipped.
  • CI run 43366702527: succeeded, including the "Changed tests observe production" gate.

Validated tree versus shipped head: git diff --stat b7c8b361be b7c8b361be is empty, because the validated commit is the shipped head.

Timed: the shared four-PR slot (see "Timed verdict").

🤖 Generated with Claude Code