perf(data): emit the schema tables compactly and decode them on first use

Merged
LO
19d ago
+558-22555

Base main (#9839 merged 2026-11-12; rebased off its commits, patch-identical). Part of the parser worker start-up work (#9838, #9839).

What changes

  • @ifc-lite/data's generated tables (attributes.ts, entities-*, psets-*, data-types.ts) are emitted as one compact string each instead of object literals. src/ifc-schema/table-codec.ts is a small exact codec: a string table, an object-shape (key list) table and tagged containers, so key order and keys whose value is undefined (a root entity's parent: undefined) survive. The encoder decodes its own output and refuses an encoding that differs. entity-names.ts stays a literal (encoding it would be larger).
  • Each table module exports a getter that decodes on first call (getEntitiesIfc4x3(), …) next to the unchanged constant (ENTITIES_IFC4X3, /* @__PURE__ */). @ifc-lite/data exports the four getters the parser needs: getEntitiesIfc2x3, getEntitiesIfc4Express, getEntitiesIfc4x3, getIfcDataTypes.
  • What used to run when a module loaded now runs on first use: ENTITIES_IFC4_EXPRESS's filter, the per-version maps behind getEntities / getAttributes / getPropertySets / getAttributeXsdTypes, and the parser's cross-schema entity map.
  • Scripts that read the tables as source text (found by CI after the first push): scripts/lib/entity-table.mjs parseEntityTable matched the old object-literal rows, so its four callers (coverage ledger, IFC4 EXPRESS corrections, structural coverage catalogue, modelling-parity census) read zero entities. It now decodes the encoded literal with a plain-JS copy of the runtime decoder: the ledger generator must run without a TypeScript loader (#8019). A test checks the copy against table-codec.ts for every value kind. Zero parsed entities is now an error naming the caller, never an empty table, and the callers' own zero checks are removed. Parsed tables are identical to main's literal tables: IFC2X3 771, IFC4 932 and IFC4X3 1,008 entities, deep-equal including order.
  • Sweep of every other reader of the encoded files (entities-*, attributes, data-types, psets-*, the three schema-registry modules):
    • the enum reconciliation, the IFC2X3 and IFC4 required-slots generators and the step-log tables generator import them as modules; their --check passes;
    • codegen's sync check compares bytes;
    • no Rust or build script reads them;
    • nothing exports an encoded literal.

Identity

Every regenerated table against main's committed literal: identical values, key order and key presence (entity-names.ts and partof-relations.ts byte-identical). Browser data-store digests: running.

Size (production viewer build, base 6636d36476 vs this stack on the same base)

metricmain#9839 + this
parser worker chunk4,621,251 B1,577,580 B
eager JS (blocking ratchet)17,550,991 B12,613,211 B
entry JS1,971,965 B1,971,937 B

Generated sources: attributes.ts 1,016,746 → 344,909 B; entity tables 725,007 → 328,362 B; property sets 902,024 → 399,570 B; data types 38,754 → 19,600 B.

A load-sensitive viewer test this PR made more sensitive

A loaded four-shard viewer run on Pop failed once on this stack: BulkPropertyEditor.undo.test.tsx, "#5898: a chunked Query write does not rediscover rule suggestions per chunk", null !== 'CHUNKED'.

  • The path does read lazily decoded tables. configureMutationView → getInheritanceChainAcrossSchemas builds the cross-schema entity map on first use. That decodes the three entity tables, the IFC4 EXPRESS filter and the IFC4 pin inside the run's first write chunk; on main this happened at import.

  • The test allowed a fixed 150 ms of real time (advance uses a real setTimeout) for the query plus three 500-target chunks. test/render.tsx itself warns that this pattern fails under load (#5977).

  • Not deterministic: a concurrent run of identical viewer, parser and data code (a7f6859c83, which differs only in a codegen test file) passed it.

  • Fix: the test now waits on the written value (waitFor) and keeps the trailing 150 ms only to observe stray rediscoveries. The file is in the focused selection (now 25 files). Isolated runs of the file alone, 3x each, before the fix:

    • this PR's head without the fix (5e3cba42ac, Pop): 3/3 runs pass, 12/12 tests each;
    • main (5a61bf3f2a, PC): 3/3 runs pass, 12/12 tests each.

    So the failure needs load and is not deterministic on either side: a load-sensitive test. This PR made it more sensitive by moving the first table decode into the timed window, and the waitFor fix removes that dependence. With the fix, the 25-file focused run passes (255 pass, 0 fail) on this head.

Also fixed here: generate-ifc-schema.test.ts and check-codegen-sync.mjs copy the table codec into their temporary generator trees. 9 generator tests had failed with "Cannot find module ../src/ifc-schema/table-codec.js". The table emitter now lives in packages/data/scripts/schema-table-module.ts. That keeps generate-ifc-schema.ts within its module-size budget: 1,089 lines, budget lowered from 1,106. It also keeps check-codegen-sync.mjs at 400 lines. Both temporary trees copy the new file, and the regenerated tables are byte-identical to the committed ones.

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

Fleet runs on c42e6cf03f (PC, carried scripts):

  • Package validation: ALL PASSED. Build packages; typecheck @ifc-lite/data and its dependents except the viewer; 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 main 03da13dd84.
  • Focused viewer run: 25/25 files, 259 tests, 255 passed, 0 failed, 4 skipped.

Validated tree versus shipped head: git diff --stat c42e6cf03f 9a7f4e56f3. The diff holds only the script fix above, its test, the sweep's changes and the revert-oracle evidence (test-only):

 scripts/generate-coverage-ledger.mjs               |  2 +-
 scripts/generate-coverage-ledger.test.mjs          | 25 ++++++++
 scripts/generate-ifc4-express-corrections.mjs      |  6 +-
 scripts/lib/entity-table.mjs                       | 67 +++++++++++++++++++++-
 .../9840-schema-table-codec/behaviour.oracle.patch | 26 +++++++++
 tests/evidence/9840-schema-table-codec/oracle.json |  6 ++
 tests/modelling-parity/output/artifacts.mjs        |  2 +-
 tests/structural-coverage/catalogue.mjs            |  3 +-
 8 files changed, 126 insertions(+), 11 deletions(-)

Revert oracle: CI's "Changed tests observe production" reverts whole files. That left generate-ifc-schema.test.ts unable to load, because it copies schema-table-module.ts, which this PR adds, so no assertion ran. tests/evidence/9840-schema-table-codec/ supplies a surgical mutation: reverse-applied, it decodes the undefined tag as null in table-codec.ts and in the scripts' copy, and keeps every export. Run locally with CI's arguments, the gate reports OBSERVED: the generator test and the decoder round-trip test each fail an assertion, and the baselines are green.

Run locally on the shipped tree:

  • generate-coverage-ledger --check, with --no-experimental-strip-types;
  • the --check of generate-ifc4-express-corrections, generate-structural-coverage-ledger, generate-enum-reconciliation, generate-ifc2x3-required-slots, generate-ifc4-required-slots and generate-step-log-tables;
  • check-source-text-assertions;
  • the modelling-parity census;
  • the script tests (coverage ledger, structural follow-up, structural ledger, modelling-parity ledger, license header): 105/105.

tests/structural-coverage/score.mjs --check needs the fixture corpus, so CI runs it.

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

🤖 Generated with Claude Code