perf(geometry): read each shared surface-model point once per lowering

Merged
LO
20d ago
+238-5

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:

Fixturemain IrThis PR IrĪ”
FZK425,573,753425,621,790+0.01%
advanced_model14,855,225,42713,074,760,492-11.99%
schependomlaan3,565,420,1373,537,033,429-0.80%
ISSUE_12925,724,738,52525,693,097,448-0.12%
Holter (holdout)27,193,408,68726,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 warnings for 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) is point3's byte fast path alone: get_raw_bytes plus the existing fast_leaf rule. It returns Some exactly when point3 would 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::lower owns a PointMemo (FxHashMap<u32, [f64; 3]>) for the duration of the call and passes it down to read_loop_points_memo. A memo hit returns the stored value. On a miss, point3_fast is called; a Some is stored and returned. A None falls through to point3(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 lower returns. 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_golden 18; wasm32. The checkout was clean.
  • Instruction gate (this lever alone on main, c6c89da625) still passes, and fingerprints are identical on all five fixtures:
FixtureBefore the review fixWith 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).

FixturePhaseBefore IrAfter IrĪ”
FZKParse90,464,33090,432,420-0.04%
FZKScan/prepass55,377,49055,350,864-0.05%
FZKLookup11,766,55011,766,825+0.00%
FZKPreprocess23,232,37823,226,819-0.02%
FZKGeometry342,404,967342,464,306+0.02%
FZKPipeline432,874,522432,901,951+0.01%
advanced_modelParse979,435,806979,003,578-0.04%
advanced_modelScan/prepass973,349,199972,914,488-0.04%
advanced_modelLookup229,983229,983+0.00%
advanced_modelPreprocess5,716,1305,718,638+0.04%
advanced_modelGeometry17,165,943,98114,601,969,742-14.94%
advanced_modelPipeline18,145,404,66615,580,998,208-14.13%
schependomlaanParse1,418,372,5321,418,179,567-0.01%
schependomlaanScan/prepass951,772,820951,572,033-0.02%
schependomlaanLookup454,306,360454,315,502+0.00%
schependomlaanPreprocess8,760,8599,073,453+3.57%
schependomlaanGeometry2,294,618,6382,250,616,789-1.92%
schependomlaanPipeline3,713,013,2483,668,818,449-1.19%
ISSUE_129Parse257,065,408256,916,689-0.06%
ISSUE_129Scan/prepass251,436,897251,290,227-0.06%
ISSUE_129Lookup21,45321,453+0.00%
ISSUE_129Preprocess5,476,7285,474,679-0.04%
ISSUE_129Geometry25,630,362,62925,602,252,048-0.11%
ISSUE_129Pipeline25,887,460,47025,859,201,170-0.11%
Holter (holdout)Parse4,897,361,1254,896,579,633-0.02%
Holter (holdout)Scan/prepass4,207,203,9604,206,418,206-0.02%
Holter (holdout)Lookup677,871,380677,872,994+0.00%
Holter (holdout)Preprocess5,197,6945,201,422+0.07%
Holter (holdout)Geometry25,000,452,94624,293,292,645-2.83%
Holter (holdout)Pipeline29,897,921,94229,189,980,707-2.37%
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

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 same refused_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 core leaf tests (9).
  • perf_probe fingerprints 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 a cfg(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, so watertightness_census_and_triangulator_invariance actually measured. The two heavy-fixture tests stayed #[ignore]d as usual.
    • cargo test -p ifc-lite-processing: 1,338 passed, 0 failed, including 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.

Ledger entry added under "Shipped wins". ci:native-adapter is requested because this is a geometry change.

šŸ¤– Generated with Claude Code