perf(geometry): reset pooled orient/weld hash maps in O(mesh), not O(largest mesh)
Self-driven work from the native instruction-count hillclimb (campaign 2), so no issue; labelled unqueued.
What was unnecessary
orient_mesh_outward_verdict (once per output mesh) and mesh_weld::weld_indexed keep their FxHashMaps in a per-worker thread-local scratch (#3988) and clear() them before every mesh. A non-empty HashMap::clear rewrites the control byte of every bucket, so after one large mesh, every later mesh on that worker paid to wipe the large table. On ISSUE_129 these were two write_bytes calls of about 31k instructions each, on meshes averaging 97 triangles. The baseline call graph attributes this memset to orient and weld:
| Fixture | memset Ir in orient + weld | Share of pipeline |
|---|---|---|
| Holter | 1.48G | 5.0% |
| schependomlaan | 94M | 2.5% |
| FZK | 7.4M | 1.7% |
| advanced_model | 240M | 1.3% |
| ISSUE_129 | 150M | 0.6% |
Change
scratch_map::reset_for(map, keys) keeps the pooled table while its capacity is within 4× of max(keys, 64) (clear, then reserve, as before). Otherwise it replaces the table with one sized for this pass, so a reset costs O(this mesh). It is applied to the orienter's vertex map, both edge-adjacency storages, and the weld key map.
Why output cannot change: these maps are only entry/get/insert-ed, never iterated, so bucket count and capacity are unobservable. That is the same argument that made pooling them byte-identical in #3988. No unsafe code. Memory: a worker no longer keeps an outsized hash table after one large mesh. The sibling scratch Vecs (links, vpos, corner, flip, visited; the weld's remap/first_vert) keep their peak capacity exactly as on main. Their reset was never the cost: Vec::clear is O(1), and resize writes only this mesh's length. The memset left in orient is about 25 Ir per call on Holter (from 987M Ir down to 15.3M). See the resolved review thread; the module doc was corrected in f0116f538d.
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.
| Fixture | Phase | Before Ir | After Ir | Δ |
|---|---|---|---|---|
| FZK | Parse | 90,464,330 | 90,425,865 | -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,220,264 | -0.05% |
| FZK | Geometry | 342,404,967 | 335,876,241 | -1.91% |
| FZK | Pipeline | 432,874,522 | 426,307,369 | -1.52% |
| advanced_model | Parse | 979,435,806 | 978,999,303 | -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,714,363 | -0.03% |
| advanced_model | Geometry | 17,165,943,981 | 16,984,393,896 | -1.06% |
| advanced_model | Pipeline | 18,145,404,666 | 17,963,418,117 | -1.00% |
| schependomlaan | Parse | 1,418,372,532 | 1,418,175,473 | -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,069,359 | +3.52% |
| schependomlaan | Geometry | 2,294,618,638 | 2,204,243,753 | -3.94% |
| schependomlaan | Pipeline | 3,713,013,248 | 3,622,441,283 | -2.44% |
| ISSUE_129 | Parse | 257,065,408 | 256,915,113 | -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,473,103 | -0.07% |
| ISSUE_129 | Geometry | 25,630,362,629 | 25,453,177,057 | -0.69% |
| ISSUE_129 | Pipeline | 25,887,460,470 | 25,710,124,488 | -0.69% |
| Holter (holdout) | Parse | 4,897,361,125 | 4,896,575,581 | -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,197,370 | -0.01% |
| Holter (holdout) | Geometry | 25,000,452,946 | 23,585,964,541 | -5.66% |
| Holter (holdout) | Pipeline | 29,897,921,942 | 28,482,648,613 | -4.73% |
| 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 |
schependomlaan's preprocess phase rises by about 309k instructions, which is 0.008% of pipeline. The surface-model PR #9632, which shares no code with this one, shows the same +309k on the same phase, so it comes from the measurement run, not from either change. The likely cause is that my driver passes the fixture by absolute path. It does not touch the pipeline gate.
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. csgFailures and degenerateDropped 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:
| Fixture | Stacked | Sum 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 UNOBSERVED: no separate test file was changed, because the scratch_map tests are in-file. Fixed in b1521f42ba. mesh_orient_tests.rs::a_small_mesh_after_a_large_one_resets_a_small_vertex_table and mesh_weld_tests.rs::a_small_weld_after_a_large_one_resets_a_small_key_table read main's own thread-local scratch: after a large mesh, a small one must leave a table of at most 1,024 entries, whereas main keeps the large table. The local --ci oracle on Pop gives OBSERVED: orient 24 pass and 1 fails, weld 13 pass and 1 fails, with production reverted.
ifc-lite-geometrylib tests filtered tomesh_orient,mesh_weldandscratch_map: 38 pass. Among them is the existingdense_adjacency_reuses_links_across_width_transitions_3988, which runs a retained scratch through big→small transitions (24000 → 2 triangles) against a fresh scratch, comparing verdict and indices. There is also a newscratch_mappolicy test.- Qualification on Pop, commit
325a7794f0(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: 574 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_invariance(the per-PR census) actually measured. The two heavy-fixture tests (ISSUE_053, ISSUE_068) stayed#[ignore]d as usual; they run ingeometry-census-heavy.yml.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.
- The current head adds only a doc-comment change in
scratch_map.rs(f0116f538d) and the ledger move (git diff 325a7794f0 HEADtouches onlyrust/geometry/src/scratch_map.rscomments andscripts/perf/README.md).
This shares no code with #9632; they are independent branches off main. Ledger entry added under "Shipped wins". ci:native-adapter is requested because this is a geometry change.
🤖 Generated with Claude Code