perf(geometry): read each shared surface-model point once per lowering
Self-driven work from the native instruction-count hillclimb (campaign 2), so no issue; labelled unqueued.
Stacked on #9632: #9632 has landed, and this PR now targets main (see below). The head is now 0d4e46f593, rebased onto #9632's fixed head a14da9d747.
Landing on main (retargeted, main merged in at ba09df093e)
#9632 has landed. This PR now targets main, and current main is merged in (no rebase). The only conflict was the memo call in surface_model_plan.rs, kept as is. The scripts/perf/README.md hunk is dropped; this lever's ledger entry is in #9673. The diff against main is exactly this lever's 5 files.
Instruction gate against current main 27ce7fc010 (which contains #9632, #9635 and #9636). Each side built its own binary fresh. Fingerprints are identical to main on all five fixtures:
| Fixture | main Ir | This PR Ir | Ī |
|---|---|---|---|
| FZK | 425,573,753 | 425,621,790 | +0.01% |
| advanced_model | 14,855,225,427 | 13,074,760,492 | -11.99% |
| schependomlaan | 3,565,420,137 | 3,537,033,429 | -0.80% |
| ISSUE_129 | 25,724,738,525 | 25,693,097,448 | -0.12% |
| Holter (holdout) | 27,193,408,687 | 26,715,209,039 | -1.76% |
FZK's +0.01% is within the 0.1% allowance; it has no surface models.
Queue-equivalent qualification on Pop, by receipt, on ba09df093e, with the full CI fixture set (all 191 files of main's manifest, fetched and verified). All pass:
- Clippy
-D warningsfor core, geometry and processing. cargo test --workspace.- Watertightness census: 0 regressed, 0 coverage loss, 0 added; void hosts 1,645 = 1,645.
- Every
Rust tests (feature builds)step: parquet-bos test and clippy, retained-browser test and clippy,rust/python, csg_manifold_gate test and clippy, all-features clippy, csg_capture, and the three opening-perf-trace steps. - wasm32 check.
- Revert oracle (
--ci, base = main): OBSERVED, 40 pass and 1 fails with production reverted.
The checkout was clean before and after.
What was unnecessary
A face-based surface model's faces share their CartesianPoints. Each surface_model_plan::lower call read every point once per face using it, through point3: about 1.78M point reads (each one an index lookup, a record walk and three number reads) for advanced_model's 13 surface models, which are lowered 839 times.
Change
EntityDecoder::point3_fast(id)ispoint3's byte fast path alone:get_raw_bytesplus the existingfast_leafrule. It returnsSomeexactly whenpoint3would return that value without decoding. It is a pure function of the decoder's immutable source and index, with no side effect.surface_model_plan::lowerowns aPointMemo(FxHashMap<u32, [f64; 3]>) for the duration of the call and passes it down toread_loop_points_memo. A memo hit returns the stored value. On a miss,point3_fastis called; aSomeis stored and returned. ANonefalls through topoint3(id)itself, every time, in the original order, so the full decode, the refusal and its counting behave exactly as before.- The memo is scoped to one lowering over one decoder and dropped when
lowerreturns. It is not a cross-job or per-worker cache; transiently it holds at most that model's distinct points, which the plan holds anyway.
No unsafe code. The other read_loop_points_by_id callers (the faceted-brep fallback and advanced-face bounds) keep their behaviour; they go through a fresh memo, so nothing changes for them.
Independent review fix: memo bound (MAJOR, fixed in 0d4e46f593; also inherits #9632's alias fix)
Finding. Points read for a loop that a later point rejects stayed in the memo, and the memo had no bound. 100 rejected loops left 10,000 entries for a collection that meshes nothing.
Fix. PointMemo now records the ids each loop adds and removes them again if the loop is rejected. It stops inserting at POINT_MEMO_CAP = 65,536 distinct points. Past the cap, new points are read uncached and never refused, and coordinates and refusals are unchanged. read_loop_points_by_id (the faceted-brep fallback and advanced-face bounds callers) is back to its original uncached loop.
Memory. For a face set with millions of distinct points, the memo holds at most 65,536 entries (roughly 2-4 MB of table) during one lowering and is freed when the lowering returns. The excess points cost what they did before this PR. One remaining case stays bounded by the cap: points of fully read loops of a face that is later refused stay in the memo.
Failing-first. 2c809d108b adds rejected_loops_leave_nothing_in_the_point_memo (the review's 100x100 shape) and the_point_memo_is_bounded_and_overflow_reads_stay_exact (65,836 distinct points; must stay at or under the cap and read exactly what fresh reads do). On 2c809d108b both fail (memo held 10,000 entries where 0 were expected), and #9632's alias regression passes there. All pass on 0d4e46f593.
After the fix, on 0d4e46f593 (stacked on #9632's fixed head), all on Pop:
- Revert oracle (
--ci, base = #9632's head): OBSERVED. 41/41 pass on the branch; with production reverted, 40 pass and 1 fails. - Qualification all pass: clippy; core 576, geometry 3,160 (including the corpus census), processing 1,338 (including
mesh_determinism),export_golden18; wasm32. The checkout was clean. - Instruction gate (this lever alone on main,
c6c89da625) still passes, and fingerprints are identical on all five fixtures:
| Fixture | Before the review fix | With the fix |
|---|---|---|
| advanced_model | -14.13% | -14.11% |
| Holter (holdout) | -2.37% | -2.32% |
| schependomlaan | -1.19% | -1.22% |
| ISSUE_129 | -0.11% | -0.13% |
| FZK | +0.01% | -0.00% |
Performance verdict
Measured alone against main: commit 0938bff520, main plus this lever without #9632, with the trivial conflict in lower_entry resolved to keep main's decode. Cold, single-thread Callgrind via scripts/perf/instructions.sh (the --cold probe), against the main baseline. These are instruction counts only: no wall-time or browser-speed claim is made.
Source: c2/round-5/results.json (branch measured alone against the main baseline).
| Fixture | Phase | Before Ir | After Ir | Ī |
|---|---|---|---|---|
| FZK | Parse | 90,464,330 | 90,432,420 | -0.04% |
| FZK | Scan/prepass | 55,377,490 | 55,350,864 | -0.05% |
| FZK | Lookup | 11,766,550 | 11,766,825 | +0.00% |
| FZK | Preprocess | 23,232,378 | 23,226,819 | -0.02% |
| FZK | Geometry | 342,404,967 | 342,464,306 | +0.02% |
| FZK | Pipeline | 432,874,522 | 432,901,951 | +0.01% |
| advanced_model | Parse | 979,435,806 | 979,003,578 | -0.04% |
| advanced_model | Scan/prepass | 973,349,199 | 972,914,488 | -0.04% |
| advanced_model | Lookup | 229,983 | 229,983 | +0.00% |
| advanced_model | Preprocess | 5,716,130 | 5,718,638 | +0.04% |
| advanced_model | Geometry | 17,165,943,981 | 14,601,969,742 | -14.94% |
| advanced_model | Pipeline | 18,145,404,666 | 15,580,998,208 | -14.13% |
| schependomlaan | Parse | 1,418,372,532 | 1,418,179,567 | -0.01% |
| schependomlaan | Scan/prepass | 951,772,820 | 951,572,033 | -0.02% |
| schependomlaan | Lookup | 454,306,360 | 454,315,502 | +0.00% |
| schependomlaan | Preprocess | 8,760,859 | 9,073,453 | +3.57% |
| schependomlaan | Geometry | 2,294,618,638 | 2,250,616,789 | -1.92% |
| schependomlaan | Pipeline | 3,713,013,248 | 3,668,818,449 | -1.19% |
| ISSUE_129 | Parse | 257,065,408 | 256,916,689 | -0.06% |
| ISSUE_129 | Scan/prepass | 251,436,897 | 251,290,227 | -0.06% |
| ISSUE_129 | Lookup | 21,453 | 21,453 | +0.00% |
| ISSUE_129 | Preprocess | 5,476,728 | 5,474,679 | -0.04% |
| ISSUE_129 | Geometry | 25,630,362,629 | 25,602,252,048 | -0.11% |
| ISSUE_129 | Pipeline | 25,887,460,470 | 25,859,201,170 | -0.11% |
| Holter (holdout) | Parse | 4,897,361,125 | 4,896,579,633 | -0.02% |
| Holter (holdout) | Scan/prepass | 4,207,203,960 | 4,206,418,206 | -0.02% |
| Holter (holdout) | Lookup | 677,871,380 | 677,872,994 | +0.00% |
| Holter (holdout) | Preprocess | 5,197,694 | 5,201,422 | +0.07% |
| Holter (holdout) | Geometry | 25,000,452,946 | 24,293,292,645 | -2.83% |
| Holter (holdout) | Pipeline | 29,897,921,942 | 29,189,980,707 | -2.37% |
| Fixture | Meshes / vertices / triangles | Fingerprint base | Fingerprint branch |
|---|---|---|---|
| FZK | 285 / 35,940 / 20,322 (unchanged) | 7377db33331a3523 | 7377db33331a3523 |
| advanced_model | 11,471 / 2,795,258 / 1,360,732 (unchanged) | c6585cf25dbb3907 | c6585cf25dbb3907 |
| schependomlaan | 6,057 / 424,334 / 260,964 (unchanged) | c41dfe123a8cda83 | c41dfe123a8cda83 |
| ISSUE_129 | 1,402 / 218,929 / 136,241 (unchanged) | 954ce9d1850bbde1 | 954ce9d1850bbde1 |
| Holter | 109,514 / 4,515,161 / 2,895,362 (unchanged) | 500e89efcdd091fa | 500e89efcdd091fa |
FZK's +0.01% is within the 0.1% allowance. It has no surface models, so this lever does not run there.
Combined with the sibling hillclimb levers
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"). This was checked before retargeting, with #9632's head as base. The memo equivalence test named new helpers, so it moved inline into surface_model.rs and reverts with the code. processors/tests.rs::shared_surface_model_corners_are_read_once_per_lowering observes the work through a test-only read probe that the loop reader bumps under cfg(test): two quads sharing an edge read their 6 distinct corners exactly once each. The local --ci oracle on Pop gives OBSERVED: 41/41 pass on the branch, and with production reverted 40 pass and 1 fails. Branch rebased onto #9632's new head; new head 80e155bede.
- New
shared_point_memo_matches_fresh_reads_and_refusals. Reading faces that share points through one memo gives the same bounds, the same face refusals and the samerefused_records()count as fresh reads, including for a shared point the fast path cannot read (1.E400, a wrong type). An unreadable point is never memoized. The existing #7779 polyloop-refusal tests and all 16 surface-model/polyloop tests pass, as do the coreleaftests (9). perf_probefingerprints are identical on all five fixtures (table above).- Qualification on Pop, on the earlier head
75757b9fba(the same code; since then only tests moved and acfg(test)probe was added) (stacked on #9632; 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,155 passed, 0 failed. This ran with the 121-model corpus linked in, sowatertightness_census_and_triangulator_invarianceactually measured. The two heavy-fixture tests stayed#[ignore]d as usual.cargo test -p ifc-lite-processing: 1,338 passed, 0 failed, includingmesh_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.
Ledger entry added under "Shipped wins". ci:native-adapter is requested because this is a geometry change.
š¤ Generated with Claude Code