perf(geometry): skip the type-only full decode of plain IfcFace records in surface models

Merged
LO
21d ago
+211-14

Self-driven work from the native instruction-count hillclimb (campaign 2), so no issue; labelled unqueued.

What was unnecessary

On advanced_model, surface_model_plan::lower is 42.5% of geometry instructions. Its 13 IfcFaceBasedSurfaceModel records are lowered 839 times through styled mapped occurrences, which means 555,535 face reads. For every face, lower_entry ran a full decode_by_id, at about 3.5k instructions and several allocations each, 1.96G Ir in total. Its only use of the result was a check for IfcAdvancedFace. Then it re-read the face's bounds from the raw bytes with get_entity_ref_list_fast.

Change

EntityDecoder::is_plain_face(id) proves from the record bytes that the record is exactly #n=IFCFACE((#a,#b,...));: STEP whitespace only between tokens, upper-case keyword, every name a u32 express id. The tokenizer reads every such record as an IfcFace holding one reference list, and it cannot refuse one, since there is no literal to reject and no REAL for strict admission. So the decode is skipped and the face type is IfcFace.

The decode still runs, as before, in three cases:

  • anything else: a comment, lower case, another keyword, an empty list or a trailing attribute;
  • an id already held in the entity memo;
  • an IfcAdvancedFace.

The skipped decode would only have filled the entity memo, which is a pure memo. No unsafe code, no new cache or retained memory; there is actually less retained, because fewer memo inserts.

Independent review fix: alias index (BLOCKER, fixed in a14da9d747)

Finding. is_plain_face(id) checked only the memo slot of the requested id and discarded the express id the record declares. Canonical decode_by_id(id) parses the indexed record and then answers from the memo of the declared id (decode_at). Under a supplied index that maps id 9 onto a record declaring #2, with #2 memoised as an IfcAdvancedFace, the proof called it a plain face.

Reachability. Supplied indexes are installed in production by the wasm boundary's set_entity_index(ids, starts, lengths) (columns from the TypeScript scanner) and by the processor's installed spans (processor/entity_index.rs). The decoder does not require their ids to equal the declared ids. I did not reproduce the case with an ordinary source-built index, but it is reachable whenever the two scanners disagree on a record.

Fix. The proof now returns the declared id, and the fast path is taken only when that equals the requested id. Otherwise the face decodes canonically. When the ids are equal, the memo slot the decode would consult is the one already checked to be empty, so the two paths agree under every index the decoder accepts.

Failing-first. 6f1abc958e adds the review's unit reproducer (plain_face_proof_must_preserve_parsed_id_cache_precedence, inline with the proof) and a lowering regression (an_aliased_face_id_is_classified_by_the_canonical_decode: the aliased advanced face must not be meshed as a polyloop). Both fail on 6f1abc958e (fleet receipt 0ccd2ce6) and pass on a14da9d747.

After the fix, on a14da9d747, all on Pop:

  • Revert oracle (--ci): OBSERVED. 6/6 pass on the branch; with production reverted, 5 pass and 1 fails.
  • Qualification all pass: clippy -D warnings; core 576, geometry 3,156 (including the per-PR census on the 121-model corpus), processing 1,338 (including mesh_output_matches_pinned_manifest), export_golden 18; wasm32 check. The checkout was clean.
  • Instruction gate (alone against main) still passes, and fingerprints are identical on all five fixtures:
FixtureBefore the review fix (663d3feaae)With the fix (a14da9d747)
advanced_model-12.16%-11.79%
Holter (holdout)-2.98%-2.89%
schependomlaan-0.63%-0.61%
ISSUE_129-0.10%-0.10%
FZK-0.01%-0.01%

The declared-id check costs advanced_model 0.37 points (about 67M Ir).

Performance verdict

Cold, single-thread Callgrind via scripts/perf/instructions.sh (the --cold probe), base main 004934fa88 (no rust/ change from there to this branch's base 63aeb6a82f), Valgrind 3.26.0, Pop x86_64. These are instruction counts only: no wall-time or browser-speed claim is made.

Tuning fixtures: FZK, advanced_model, schependomlaan, ISSUE_129. Holter is the holdout, checked only here at acceptance.

FixturePhaseBefore IrAfter IrĪ”
FZKParse90,464,33090,439,395-0.03%
FZKScan/prepass55,377,49055,360,124-0.03%
FZKLookup11,766,55011,767,199+0.01%
FZKPreprocess23,232,37823,224,154-0.04%
FZKGeometry342,404,967342,405,871+0.00%
FZKPipeline432,874,522432,850,491-0.01%
advanced_modelParse979,435,806979,139,940-0.03%
advanced_modelScan/prepass973,349,199973,054,400-0.03%
advanced_modelLookup229,983229,983+0.00%
advanced_modelPreprocess5,716,1305,715,088-0.02%
advanced_modelGeometry17,165,943,98114,959,694,473-12.85%
advanced_modelPipeline18,145,404,66615,938,859,325-12.16%
schependomlaanParse1,418,372,5321,418,285,938-0.01%
schependomlaanScan/prepass951,772,820951,672,007-0.01%
schependomlaanLookup454,306,360454,324,980+0.00%
schependomlaanPreprocess8,760,8599,070,366+3.53%
schependomlaanGeometry2,294,618,6382,271,463,901-1.01%
schependomlaanPipeline3,713,013,2483,689,771,926-0.63%
ISSUE_129Parse257,065,408256,951,623-0.04%
ISSUE_129Scan/prepass251,436,897251,325,829-0.04%
ISSUE_129Lookup21,45321,453+0.00%
ISSUE_129Preprocess5,476,7285,474,011-0.05%
ISSUE_129Geometry25,630,362,62925,605,514,940-0.10%
ISSUE_129Pipeline25,887,460,47025,862,498,996-0.10%
Holter (holdout)Parse4,897,361,1254,897,070,917-0.01%
Holter (holdout)Scan/prepass4,207,203,9604,206,853,792-0.01%
Holter (holdout)Lookup677,871,380677,932,984+0.01%
Holter (holdout)Preprocess5,197,6945,197,136-0.01%
Holter (holdout)Geometry25,000,452,94624,109,119,578-3.57%
Holter (holdout)Pipeline29,897,921,94229,006,300,504-2.98%
FixtureMeshes / vertices / trianglesFingerprint baseFingerprint branch
FZK285 / 35,940 / 20,322 (unchanged)7377db33331a35237377db33331a3523
advanced_model11,471 / 2,795,258 / 1,360,732 (unchanged)c6585cf25dbb3907c6585cf25dbb3907
schependomlaan6,057 / 424,334 / 260,964 (unchanged)c41dfe123a8cda83c41dfe123a8cda83
ISSUE_1291,402 / 218,929 / 136,241 (unchanged)954ce9d1850bbde1954ce9d1850bbde1
Holter109,514 / 4,515,161 / 2,895,362 (unchanged)500e89efcdd091fa500e89efcdd091fa

The fingerprints are perf_probe --fingerprint, i.e. ordered mesh payloads: positions, normals, indices (so winding too), colours, origins, bounds and transforms. They exclude text, materials, UVs, textures and instancing, so they are necessary evidence of identity, not sufficient on their own. csgFailures, degenerateDropped and point-cache hit/miss counts are also equal on all five fixtures.

Combined with the sibling hillclimb levers

These are separate branches off main. The three accepted levers stacked (#9632 + #9636 + #9635, cherry-picked onto main, 193e1818e0), cold single-thread Callgrind pipeline instructions against the main baseline:

FixtureStackedSum of the three measured alone
FZK-1.68%-1.70%
advanced_model-18.46%-18.54%
schependomlaan-3.99%-3.90%
ISSUE_129-0.63%-0.94%
Holter (holdout)-9.15%-9.13%

Fingerprints are identical on all five fixtures when stacked. The code changes touch disjoint files, and the ledger entries are anchored at different bullets, so a sequential trial merge onto main is clean.

ISSUE_129 recovers 0.31 points less stacked than alone. Pair builds isolate this: #9635 with #9632 gives +51M Ir, #9635 with #9636 gives +30M. The per-function diff puts most of it in unexported glibc malloc internals. #9635 swaps outsized tables for fresh ones and the other two remove allocations, so the heap ends up in a different state, and that changes the allocator's internal path costs. It is not a logic interaction, and the output is identical. #9643 (stacked on #9632, measured alone against main) adds advanced_model -14.1% and Holter -2.4% on top; its combined figure will be added once it lands.

All four accepted levers stacked (#9632 + #9636 + #9635 + #9643, cherry-picked onto main, cfd66cd1d5): FZK -1.67%, advanced_model -28.27%, schependomlaan -4.76%, ISSUE_129 -0.77%, Holter -10.79% pipeline instructions, with fingerprints identical on all five fixtures. advanced_model falls short of the sum of its parts (-32.7%) by design: #9636 makes each point read cheaper, and #9643 removes most of those reads.

Identity and tests

Revert oracle ("Changed tests observe production"). The first head was INCONCLUSIVE: its test called the new is_plain_face, so it could not compile against reverted production. Fixed in 0b616967dd, and the test now observes the work instead. surface_model_plan_tests.rs::plain_ifcface_records_are_lowered_without_a_full_decode checks main's own entity memo: after lowering, a plain IFCFACE is not in it (main's decode_by_id would have put it there), and a face the byte proof refuses still is. The byte-proof test moved inline into fast_buffers.rs, so it reverts together with is_plain_face. Running node scripts/check-test-revert-oracle.mjs --base <merge-base> --ci locally on Pop gives OBSERVED: 5/5 pass on the branch, and with production reverted 4 pass and 1 fails (the observer).

  • New unit test plain_face_proof_accepts_only_records_the_decode_reads_as_ifc_face. Every record the proof accepts decodes, uncached, to IfcFace, and 16 near-miss spellings are refused; once an id is memoized, the decode answers instead. Passes.
  • All 14 ifc-lite-geometry surface-model unit tests pass, including the mixed refusal-order test.
  • Qualification on Pop, commit 663d3feaae (all pass):
    • cargo clippy -p ifc-lite-core -p ifc-lite-geometry -p ifc-lite-processing --all-targets -- -D warnings: clean.
    • cargo test -p ifc-lite-core: 575 passed, 0 failed.
    • cargo test -p ifc-lite-geometry: 3,154 passed, 0 failed. This ran with the 121-model corpus linked in, so watertightness_census_and_triangulator_invariance (the per-PR census against its golden) actually measured. The two heavy-fixture census tests (ISSUE_053, ISSUE_068) stayed #[ignore]d, as they are by default; that lane is .github/workflows/geometry-census-heavy.yml.
    • cargo test -p ifc-lite-processing: 1,338 passed, 0 failed, including mesh_determinism (mesh_output_matches_pinned_manifest).
    • cargo test -p ifc-lite-export --test export_golden: 18 passed.
    • cargo check --target wasm32-unknown-unknown -p ifc-lite-wasm: compiles.
    • The checkout was clean before and after; fixtures were linked in as gitignored files and removed afterwards.

Ledger entry added under "Shipped wins". This PR also carries the dead-end entry for the withdrawn incircle lever, closed PR #9637 (c9511caf09, documentation only). ci:native-adapter is requested because this is a geometry change.

šŸ¤– Generated with Claude Code