perf(data-model): narrow the entities table before building the metadata
What and why
Closes #9582.
extract_data_model built an EntityMetadata row — six owned Strings — for every STEP instance, and DataModelEntities::Rooted then threw most of them away. On a large architectural model that is about five rows in six. The result fits; the peak is what a caller with a memory ceiling cannot afford.
extract_data_model_with(content, DataModelOptions) applies the entities variant before the metadata pass. extract_data_model delegates with the defaults, so the default path is unchanged. The three parse routes that already read ?data_model_entities= off the query now pass it into the extraction instead of narrowing the finished table, so the optimization actually reaches users.
Per the triage comment: one options-based implementation behind the existing compatible wrapper, property/quantity selection explicit and default-on, no association switches, and no All caller or WASM default silently switched.
Design
Ordering is the correctness condition. The filter runs before the entities table exists, so it decides from the type name plus the ids the other tables name — which means every other table has to be built first. The association pass was not, and it is the one that matters: a materials row points at an IfcMaterial and an IfcMaterialLayerSet by definition_id / material_id, rows that carry no GlobalId and that nothing else references. They were dropped before anything could record that they were referenced — 24 of them on ara3d/AC20-FZK-Haus.ifc, 88 on ara3d/ISSUE_068_ARK_NUS_skolebygg.ifc — so the entities table stopped describing ids its own materials table pointed at. extract_associations needs only the relationships and the buckets, so it now runs first and feeds the filter.
One reference-collection policy, not two. collect_table_refs is the single implementation; referenced_ids (post-filter) calls it and adds the spatial ids, metadata_jobs (pre-filter) calls it alone. Two copies existed for the length of one draft and had already diverged by the time the tests caught it.
The spatial references are covered, and here is why. referenced_ids also includes spatial_hierarchy, which the pre-filter cannot consult — the hierarchy is built from the entities table it is deciding. It does not need to:
project_id, and every id in the builder'sspatial_entity_ids, is an entity whose type is inspatial.rs's spatial-type list. Every one of those is anIfcRootsubtype, so it is kept by type; the IFC4X3-only ones the IFC4 registry does not list are kept as unknown types.- Every other id the builder touches — a node's
parent_id,children_ids,element_ids, and both halves of everyelement_to_*pair — is read fromIfcRelAggregates/IfcRelContainedInSpatialStructurerows, so it is a relationship endpoint, whichcollect_table_refskeeps.
So the spatial build also sees the same answers from the narrowed table: every id it looks up in its entity_map is a relationship endpoint or a spatial-typed row. That is an argument, so it is also a test — see below.
The All path is protected by construction, not just measured. metadata_jobs returns Cow::Borrowed for All, so there is no full-job copy. And because the job list is known up front for All, its metadata pass stays in the same rayon::join beside properties, quantities and relationships; only a narrowing variant defers it. The All join tree is line-for-line what it was.
DataModel.total_entities. entities.len() stops being the instance count once the table is narrowed, and DataModelStats.entity_count describes the model, not the variant served, so the one metadata header both variants share can never disagree with either (#6034). The routes used to read entities.len() before applying the variant; with the narrowing moved ahead of extraction there is no "before". It is not part of any payload: DataModel is not Serialize, and serialize_data_model_to_parquet names its columns explicitly, so the cached and wire bytes are unchanged. The one compatibility cost is construction — a struct literal of DataModel must now set it, and apps/server's three parquet-test literals are updated here.
Evidence
Two harnesses, both in the diff now, because the lever and what a caller sees are different questions and a win on the first does not imply the second.
1. Extraction only — rust/processing/examples/dm_peak.rs
This measures extract_data_model_with and nothing else. No end-to-end, geometry or full-server claim follows from this table alone; section 2 is that claim.
upstream/main @ 5790205e3 vs branch, server-release, 11-core arm64 / 36 GB, otherwise idle, interleaved repetitions of the same two binaries, peak RSS as the median of /usr/bin/time -l:
| model | mode | n | base peak | branch peak | Δ | base ms | branch ms | Δ | digest |
|---|---|---|---|---|---|---|---|---|---|
ara3d/AC20-FZK-Haus (2 MB) | all | 2 | 104 M | 104 M | +0.0% | 26 | 23 | −10% | SAME |
| rooted | 2 | 67 M | 24 M | −64% | 22 | 9 | −58% | SAME | |
ara3d/schependomlaan (47 MB) | all | 2 | 1907 M | 1906 M | −0.1% | 366 | 352 | −4% | SAME |
| rooted | 2 | 1337 M | 654 M | −51% | 388 | 144 | −63% | SAME | |
ara3d/ISSUE_068_ARK_NUS_skolebygg (54 MB) | all | 2 | 2288 M | 2289 M | +0.0% | 468 | 406 | −13% | SAME |
| rooted | 2 | 1589 M | 746 M | −53% | 478 | 190 | −60% | SAME | |
ara3d/ISSUE_098_…_MO_7000 (68 MB) | all | 2 | 2624 M | 2624 M | +0.0% | 568 | 548 | −3% | SAME |
| rooted | 2 | 1806 M | 808 M | −55% | 572 | 212 | −63% | SAME | |
ara3d/ISSUE_053_…Holter_Tower_10 (169 MB) | all | 7 | 6743 M | 6747 M | +0.1% | 1380 | 1416 | +3% | SAME |
| rooted | 2 | 4577 M | 1984 M | −57% | 1569 | 578 | −63% | SAME | |
various/O-S1-BWK-BIM… (327 MB) | all | 7 | 12545 M | 12672 M | +1.0% | 2532 | 2579 | +2% | SAME |
| rooted | 2 | 9016 M | 4672 M | −48% | 3062 | 1254 | −59% | SAME |
The All path is flat on all six models, +1.0% being the largest and sitting inside the 327 MB model's own 4% run-to-run spread (base 12403–12698 MiB over 7 runs, branch 12223–12704). That matches the structural argument above: for All the job list is borrowed, not copied, and the metadata pass stays in the same rayon::join it was in.
Measurement note, since it changed an earlier claim. An initial sweep put that model's All spread at 43% and I wrote it up as unresolvable on this host. It was not the model — it was my own cargo test running beside the benchmark, which AGENTS.md already warns against. Re-run idle, it repeats to 4% and resolves cleanly. The numbers above are all from the idle re-run, against the post-rebase base; #9636 (perf(core): value STEP reals in the grammar walk) landed in between and moved the rooted wall-time win from −55…−73% to a tighter −58…−63%.
Reproducing it. The harness is no longer a throwaway — rust/processing/examples/dm_peak.rs is in the diff, and its header carries the commands for both time(1) dialects:
cargo build --profile server-release -p ifc-lite-processing --example dm_peak
# macOS: peak RSS in bytes on the "maximum resident set size" line.
/usr/bin/time -l ./target/server-release/examples/dm_peak \
tests/models/ara3d/schependomlaan.ifc --entities rooted
# the pre-#9582 path, in the same binary — see below
/usr/bin/time -l ./target/server-release/examples/dm_peak \
tests/models/ara3d/schependomlaan.ifc --entities rooted-postfilter
# one JSON object per extraction, for a scripted sweep
./target/server-release/examples/dm_peak model.ifc --entities rooted --json
--entities rooted-postfilter is the base side, and it is the same binary. It runs extract_data_model then DataModelEntities::apply — exactly what every caller did before this PR. Neither function changed here (extract_data_model delegates with the defaults; apply is untouched), so the base algorithm is still reachable, and comparing it against rooted in one build holds codegen, allocator and host fixed. It is also the only way to measure it from this file: a base checkout has no extract_data_model_with, so build-at-ref.sh --inject cannot build this example at 5790205e3. The genuine two-binary comparison is section 2.
Re-run on the committed harness, five interleaved repetitions per side, median — ara3d/schependomlaan.ifc 959 → 273 MiB (−72%, base spread ±0%) and 379 → 141 ms (−63%, ±3%); ara3d/AC20-FZK-Haus.ifc 59 → 15 MiB (−74%, ±1%) and 22 → 9 ms (−59%, ±9%). One digest per model across all ten runs, so the two paths agree — the correctness half of the same run.
Those absolutes sit below the six-model table above (FZK 67 → 24 M there, 59 → 15 M here) because the committed harness holds less beside the extraction than the throwaway did and drops the model before the process exits. The direction and the magnitude reproduce; where the two disagree, believe the committed one, because it is the one you can run.
The digest, explicitly. A 64-bit FNV-1a (ifc_lite_core::hash) over every table of the resulting DataModel, in a fixed order: the table's name, then its row count, then each row's serde_json encoding — so a row that moves between two tables cannot cancel out, and struct field order is the encoding. total_entities is folded in first and refused last; spatial_hierarchy goes in whole, and every field of it is a Vec, so no map iteration order can reach the digest. It reads more fields than the Parquet encoding carries, so it is the stricter of the two. digest() and fold_rows() in dm_peak.rs are the whole construction, 20 lines.
2. End-to-end server parse — scripts/perf/dm-e2e-ab.mjs
The question section 1 cannot answer. This builds the server from both sides and drives the real POST /api/v1/parse/parquet for both variants.
What it controls for:
- One fresh server process per (side, variant, round), under
/usr/bin/time, so the reported peak belongs to exactly one parse of one variant. A long-lived server measured across both would report one high-water mark for both and could attribute neither. - A fresh
CACHE_DIRper process. A cache hit skips the parse entirely and would read as a 100% win. - Which side runs first is balanced and randomized per round by the existing
ab-order.mjs(#4221); both sides see the same variant order. - Output identity is byte identity: a SHA-256 of the geometry Parquet body and of the data-model Parquet the route cached (fetched from
/api/v1/parse/data-model/{cache_key}), per run. A digest that moves between rounds on one side is reported as non-determinism, which is a different defect from the two sides disagreeing. - The base side's own spread is the noise floor. A delta inside it is printed as noise, never as a win, and a base
TOTALspread over 15% refuses the timing verdict outright.
node scripts/perf/dm-e2e-ab.mjs tests/models/ara3d/schependomlaan.ifc --iters 7 --json /tmp/dm-e2e.json
Seven interleaved rounds per side on ara3d/schependomlaan.ifc (49 MB), base 5790205e3 vs branch a4efd4aa9, medians: (a4efd4aa9 is the pushed fa3b486cd before a docs-only amend that wrote these numbers into the ledger; no compiled file differs.)
| variant | metric | base | branch | Δ | base spread | verdict |
|---|---|---|---|---|---|---|
all | peak RSS | 1432 MiB | 1429 MiB | −0.2% | ±2% | noise |
| parse | 73 ms | 71 ms | −2.7% | ±12% | noise | |
| geometry | 109 ms | 112 ms | +2.8% | ±30% | noise | |
| TOTAL | 187 ms | 185 ms | −1.1% | ±15% | noise | |
rooted | peak RSS | 1203 MiB | 556 MiB | −53.8% | ±2% | ✅ real |
| parse | 71 ms | 71 ms | +0.0% | ±11% | noise | |
| geometry | 112 ms | 67 ms | −40.2% | ±21% | ✅ real | |
| TOTAL | 185 ms | 141 ms | −23.8% | ±10% | ✅ real |
VERDICT: output identical on both sides; real moves: rooted/peak RSS -53.8%, rooted/geometry -40.2%, rooted/TOTAL -23.8%.
Geometry Parquet byte-identical across sides in both variants (sha256 8964623985aa…, the same in all four cells — geometry does not depend on the variant). Data-model Parquet byte-identical across sides per variant (all b38035ea28dd… at 6,503,840 bytes; rooted eb9cd7c277bf… at 2,996,092 bytes). data_model_stats.entity_count is 714,485 in every run of both variants, which is what #6034 asks of it.
The gap between section 1 and section 2 is the point. The extraction's −72% becomes −53.8% of the server process, because a parse also holds the upload, the geometry pass and two Parquet buffers, and that floor is most of what is left. Reporting only the first number would have claimed a win a caller never sees — which is the correction this round of review asked for.
On the rooted geometry row. It drops 40% too, and that is not a geometry change: the geometry pass runs in a rayon::join beside the extraction, so a cheaper sibling returns cores to it. The byte-identical geometry Parquet above is what makes that reading safe rather than a guess; no geometry-output claim is being made either way.
Noise, stated plainly, because two earlier sweeps of this same fixture were refused. The harness declines a timing verdict when the base side's own TOTAL spread passes 15%, and it did exactly that twice while macOS re-indexed the build trees between runs. The table above is a sweep that passed its own guard; the refused ones are not quoted as if they had. Across all four sweeps the rooted peak reproduced within a point (−53.8% / −54.8% / −55.1% / −55.1%) while its TOTAL ranged −23.8% to −33.5%, because the geometry phase it shares cores with moves with whatever else the host is doing. The −23.8% reported here is the conservative end of that range, from the quiet run — not the best of four. Peak RSS is the number this PR is about and it is the stable one.
Smaller fixture, for completeness
Five rounds on ara3d/AC20-FZK-Haus.ifc (2.5 MB), the repo's default: rooted peak 99.8 → 61.2 MiB (−38.7%, ±5%), all peak −0.4%. Every digest identical. The harness refuses the timing verdict there and is right to: a 12 ms total on a 3 MB model is at the route's millisecond resolution, so its ±67% spread is quantisation, not the machine. The acceptance fixture is schependomlaan.
Tests
Six new tests in rooted_tests.rs, plus two synthetic fixtures.
assert_paths_agree compares the whole data model — not just the entities table — between extract_data_model + apply and extract_data_model_with(Rooted), and then asserts nothing the model names is missing from the table it built.
issue_9582_prefilter_matches_postfilter_on_edge_casesruns it overMIXED_IFC, a newSPATIAL_EDGE_IFC, a newNO_PROJECT_IFCand empty input.SPATIAL_EDGE_IFCcarries: an orphan storey noIfcRelAggregatesnames; a space aggregated under one storey and contained under another (#3973's cross-linked export); a containment-promoted space (#1075); anIfcSiteand an IFC4X3IfcFacilityPartthat no relationship names at all, which is the one spatial id that is not a relationship endpoint; a vendor type with a GlobalId and one without;IfcWallandIfcFurniturerows with null GlobalId; and the non-rooted material / classification / document endpoints.NO_PROJECT_IFChas noIfcProject, so the spatial walk never starts and every node is rescued as an orphan.issue_9582_prefilter_matches_postfilter_on_real_modelsruns it overara3d/AC20-FZK-Haus.ifc,duplex.ifcandexample.ifc, skipping each that is absent.issue_9582_prefilter_keeps_the_rows_the_exact_rule_keepsnames those rows individually, so a regression says which rule moved.issue_9582_prefiltered_rows_are_a_subsequence_in_file_orderpins file order and row identity.issue_9582_all_is_unchangedandissue_9582_optional_tables_drop_only_themselvespin the default path and the optional tables.
They fail against the unfixed code. Checked two ways, by patching the branch:
- pass empty association tables to the filter (i.e. restore the old pass ordering) →
..._on_edge_cases,..._on_real_modelsand..._keeps_the_rows...fail, withMIXED_IFC: the two paths disagreeand the same on FZK; - make the unknown-type rule answer
falseinstead of over-keeping →..._on_edge_casesfails onSPATIAL_EDGE_IFC, and..._keeps_the_rows...on the vendor row.
How it was verified
-
cargo test --workspace— 1974 passing. Three pre-existing failures, not from this branch:ifc-lite-export'sglb,glb_from_meshesandusdgolden pins reportMOVEDonAC20-FZK-Haus. I ran the same test onorigin/mainin a clean worktree and got the same three failures with byte-identical hashes (glbpinned228979185104e472, gotecdef44771816cde, on both). They only became visible because I downloaded the fixture; nothing here touches geometry or export. Not re-pinned — that is a separate call. -
cargo clippy --workspace --all-targets -- -D warnings— clean, exceptifc-lite-geometry-runtime's threeunused variable: piderrors intests/cleanup.rsandtests/retained_sweep.rs, which also reproduce onorigin/main. -
cargo test -p ifc-lite-processing --test function_length_ratchet— green with no new allowlist row.extract_data_modeldrops off it entirely (159 → 6 lines). The three route functions are lower than their recorded budgets and their rows are ratcheted down (255→254, 182→181, 340→329);parse_parquet_stream's background data-model fill is extracted intofill_data_model_cacherather than its budget being raised. -
cargo test -p ifc-lite-processing --test module_size_ratchet— green, after a fix the second commit carries. The first commit failed it, and I had missed that: it pusheddata_model/mod.rsto 406 lines androutes/parse/parquet_stream.rsto 411, both over the 400-line bar with no allowlist row, so CI would have gone red on the first run the maintainer approved. Both are split rather than allowlisted —fill_data_model_cachemoves intostream_data_model.rs, which already exists for the streamed parse's background fill, and the instance scan plus its debug census move out ofdata_model/mod.rsinto a newscan.rs. Pure moves;cargo test -p ifc-lite-processing -p ifc-lite-serveris green either side of them (806 + 398 passing), as iscargo clippy --all-targets -D warningson both crates. -
node --test scripts/perf/dm-e2e-report.test.mjs— 12 passing. The A/B harness's reporting half is separated from its driver so it can be tested without starting a server, and the tests pin the things a measurement harness gets quietly wrong: a move inside the base side's own spread is reported as noise and not as a win; a digest that differs across sides invalidates the run; a digest that moves between rounds on one side is reported as non-determinism instead, which is a different defect; unpaired sides are refused rather than averaged; and anentity_countthat moves with the served variant is flagged, which is the #6034 invariant. - Perf verdict recorded in the
scripts/perf/README.mdlever ledger, per AGENTS.md. -
docs/guide/server.mdgains a short paragraph under the rooted section: the wire contract is unchanged, but asking forrootedis now the cheaper way to parse a large model.
Not run: pnpm test / the TS lanes — this branch touches no TS and no published packages/*, so no changeset. No WASM, CLI, SDK or PyO3 consumer reaches extract_data_model, so pkg/ifc-lite.d.ts is unchanged.
Open
DataModelOptions.properties/.quantitieshave no caller yet in this PR. You asked for them to be kept explicit and default-on, so they are, and a test pins that switching them off drops exactly those two tables; the issue's third column (the cache writer that serves properties from elsewhere) is the consumer, and that is #9584. Say the word if you would rather they land with it instead.- Only the entities variant is selectable over the wire, so
DataModelOptions::for_entitiesis what all three routes use. If you would rather the routes name the full struct, that is a one-line change each. scripts/perf/dm-e2e-ab.mjsis a measurement harness, not a CI gate, and it is deliberately not wired into a workflow: it builds two servers and starts one process per measurement, which is minutes of wall time and the wrong shape for per-PR CI.build-at-ref.shgains--package,--binand--profileso its base side can be a binary of another crate;ab.sh's existing call keeps the old defaults, and onlyprofilingis still back-filled into older refs. Say the word if you would rather the harness live somewhere other thanscripts/perf/.
Checklist
- A test asserts the new behavior through a fixture or a stated invariant, and that test FAILS against the unfixed code (two patches, above)
- Published
packages/*touched: no - Geometry-output change: none — and now asserted rather than asserted-by-inspection: the geometry Parquet body is SHA-256-identical between base and branch on every run of both variants (
8964623985aa…) - House rules: no
as any/@ts-ignore, no silentcatch {}, no module past ~400 lines (two were, in the first commit; the second splits them — see "How it was verified"), no repo-widecargo fmt - No client or project identifiers in code, tests, commit messages, or this PR
🤖 Generated with Claude Code
Summary by CodeRabbit
- Performance
- Rooted-mode parsing now narrows entity data before building metadata, reducing peak memory use and parse time while preserving the resulting data model.
- Bug Fixes
- Parse statistics report the total number of scanned entities, even when the returned entity table is narrowed.
- Documentation
- Added performance comparisons and guidance for measuring rooted-mode extraction, including result consistency, measurement limitations, and benchmark instructions.