perf(codegen): emit schema registries compactly and decode them on first use

Merged
LO
20d ago
+1112-369480

Part of the parser worker start-up work (#9838 is the first piece). The three generated schema registries were 55% of the parser worker's 4.6 MB bundle, and the main thread's eager JS carries them too.

What changes

  • @ifc-lite/codegen emits each schema-registry.ts as one compact string plus an inline decoder, instead of one object literal. The encoding names every string once and every distinct attribute once, and records an entity's allAttributes and inheritanceChain as "the parent's, then its own" where that is exactly what the full lists say (true for all 776 / 876 / 653 entities; anything else would be listed explicitly). packages/codegen/src/schema-registry-codec.ts has the encoder, which decodes its own output and refuses an encoding that differs from its input.
  • getSchemaRegistry() decodes on first call and returns the same object afterwards. SCHEMA_REGISTRY stays an exported synchronous constant (/* @__PURE__ */ getSchemaRegistry(), so a bundle that never reads it drops it), and every generated helper (getEntityMetadata, …) is unchanged in signature and answer.
  • The parser's getSchemaRegistryForVersion holds getters instead of registries, and its non-entity name set is built on first use.
  • Codegen tests that asserted on the emitted literal's text now run the emitted module and assert on the registry it decodes to. A new test decodes each committed generated file through the emitted decoder and compares it with the registry built from its .exp.

Identity

Each regenerated registry, decoded, against main's committed object literal: same keys in the same order (a root entity still has no parent key; arrayBounds only where it was), same values, fresh objects per attribute occurrence. getEntityMetadata / getAllAttributesForEntity / getInheritanceChainForEntity / isKnownEntity give identical answers on IfcWall, IFCWALL, IFCRELAGGREGATES, constructor, __proto__ and an unknown name. Browser data-store digests on the scoreboard fixtures, IFC4X3 and .ifczip are running (with the follow-up data-table PR stacked on this).

Size

filemainthis PR
IFC4 schema-registry.ts1,637,965 B160,141 B
IFC4X31,860,725 B187,550 B
IFC2X31,300,681 B140,960 B

In the parser worker bundle the three registries go from 2,473 KB to 442 KB of minified code (standalone build of the worker entry: 4,618,245 → 2,538,305 B). Node module load of that bundle, untimed: ~126 ms → ~77 ms.

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.

Verification plan (in progress; results will replace this section)

  • Timed: one exclusive A/B for all four parser-worker start-up PRs as a unit, #9838 + #9839 + #9840 + #9841 on main 6636d36476, against the same base. FZK, advanced_model, Holter, BWK; cold, first and second reopen; boot lane; 5 rounds; memory pass. All four share that verdict (gain on a cell at 2x the floor, no regressing cell, boot included) and land in order: #9838, then #9839, #9840, #9841, each retargeted to main as the one below merges. Each PR's own identity proof and package validation stand alone.
  • Tests that run before labelling (fleet, pinned to Pop): package validation on this PR's head: build packages, typecheck @ifc-lite/data and its dependents except the viewer, @ifc-lite/data / @ifc-lite/parser / @ifc-lite/codegen tests, check-codegen-sync, check-api-surface, check-server-browser-type-parity, perf-ratchet tests, pre-handoff. Also the 24 viewer test files that import the schema tables (TEST_PATTERN).
  • Identity: browser data-store digests (7 scoreboard fixtures + IFC4X3 + .ifczip), and Node consumers (CLI info / query / export / validate / convert and the MCP loader's store) on one file per schema, main vs stack.
  • Decode location: an instrumented (local-only) build traces where every first-use decode lands (boot vs inside the timed open, main thread vs worker) on FZK and Holter.
  • Not run locally: the full four-shard viewer suite. While Pop is the only working host the merge queue runs it before anything lands.

Known remaining cost in this PR alone: HIERARCHY_REL_TYPES walks all three registries when the parser loads; #9841 makes it lazy.

🤖 Generated with Claude Code