perf(geometry): quantise weld and orient keys without a libm round call

Merged
LO
20d ago
+151-6

Self-driven work from the native instruction-count hillclimb (campaign 2, round 6), so no issue; labelled unqueued. Its ledger entry is in the consolidated ledger PR #9673, not here.

What was unnecessary

  • Weld: mesh_weld's vertex key rounds five f32 components per vertex. Two of them are the constant 0.0 UV of an untextured mesh.
  • Orient: the weld grid in mesh_orient rounds three f64 coordinates per unique vertex.
  • Cost: on x86_64's baseline target and on wasm32, every .round() is a call into libm. On main before this campaign's orient/weld change that was about 579M Ir on advanced_model and about 960M on Holter.

Change

grid::round_i32 and grid::round_i64 compute x.round() as i32 / as i64 inline: round half away from zero, saturating, NaN to 0. Below 2^23 (f32) or 2^52 (f64), x - trunc(x) is exact, so comparing it with ±0.5 decides the same rounding. At or above those magnitudes every value is already an integer, and the saturating cast alone is the answer.

They replace exactly two call sites: the weld vkey and the orient grid key, now a small grid_key helper so orient_mesh_outward_verdict stays within its function-length ratchet. No unsafe code, no new state.

Identity and tests

  • grid::tests compares both helpers bit for bit against libm: every 251st f32 bit pattern (about 17M values, every exponent, NaN and infinities), 5M pseudo-random f64 values, and the edge cases (±0.5, ±2.5, 0.49999997, 2^23 − 0.5, 2^52 − 0.5, 2^63, MAX).
  • Revert-oracle observers in the existing mesh_weld_tests.rs and mesh_orient_tests.rs. Test-only probes, bumped from the key code under cfg(test), show the work:
    • a 4-vertex weld performs 20 inline roundings;
    • a 4-vertex tetrahedron's orient grid performs 12.
  • perf_probe fingerprints are identical to main on all five fixtures (table below).

Qualification on Pop, by receipt, on this head b963faf50c merged with main 27ce7fc010. It covers what the merge queue runs, with the full CI fixture set: all 191 files of main's tests/models/manifest.json, fetched and verified.

  • Clippy -D warnings for core, geometry and processing: clean.
  • cargo test --workspace: pass. An earlier run on 91556072f9 failed only function_length_ratchet (190 lines against a budget of 185). That is fixed by extracting the helper, not by raising the budget.
  • Watertightness census (--features triangulation-alt --test triangulation_invariance): pass. 0 regressed, 0 coverage loss, 0 added; void hosts 1,645 = 1,645.
  • Every step of Rust tests (feature builds) passes: parquet-bos test and clippy, retained-browser test and clippy, rust/python, csg_manifold_gate test and clippy, geometry all-features clippy, csg_capture, and the three opening-perf-trace steps.
  • cargo check --target wasm32-unknown-unknown -p ifc-lite-wasm: pass.
  • Revert oracle (--ci, base = main): OBSERVED. With production reverted, orient fails 1 of 26 and weld fails 1 of 15.

Performance verdict

Cold, single-thread Callgrind via scripts/perf/instructions.sh (the --cold probe), measured against current main 27ce7fc010, which already contains #9632, #9635 and #9636. Each side built its own binary fresh. These are instruction counts only: no wall-time or browser-speed claim is made.

FixturePhasemain 27ce7fc010 IrThis PR IrΔ
FZKParse90,348,37090,354,718+0.01%
FZKScan/prepass55,351,24455,355,445+0.01%
FZKLookup11,739,67211,739,954+0.00%
FZKPreprocess23,169,74023,171,605+0.01%
FZKGeometry335,220,120331,077,932-1.24%
FZKPipeline425,573,753421,437,901-0.97%
advanced_modelParse979,084,606979,084,192-0.00%
advanced_modelScan/prepass973,035,585973,035,048-0.00%
advanced_modelLookup229,983229,983+0.00%
advanced_modelPreprocess5,678,7335,678,856+0.00%
advanced_modelGeometry13,876,115,90313,552,262,955-2.33%
advanced_modelPipeline14,855,225,42714,531,372,053-2.18%
schependomlaanParse1,417,728,3761,417,801,386+0.01%
schependomlaanScan/prepass951,583,347951,642,336+0.01%
schependomlaanLookup453,892,252453,904,724+0.00%
schependomlaanPreprocess9,034,3969,035,945+0.02%
schependomlaanGeometry2,147,669,7042,099,986,887-2.22%
schependomlaanPipeline3,565,420,1373,517,810,318-1.34%
ISSUE_129Parse256,881,396256,883,275+0.00%
ISSUE_129Scan/prepass251,271,424251,273,017+0.00%
ISSUE_129Lookup21,45321,453+0.00%
ISSUE_129Preprocess5,458,3535,458,639+0.01%
ISSUE_129Geometry25,467,824,55025,430,818,682-0.15%
ISSUE_129Pipeline25,724,738,52525,687,734,521-0.14%
Holter (holdout)Parse4,896,910,6564,897,129,610+0.00%
Holter (holdout)Scan/prepass4,206,780,5834,206,997,078+0.01%
Holter (holdout)Lookup677,870,414677,870,976+0.00%
Holter (holdout)Preprocess5,172,8525,174,749+0.04%
Holter (holdout)Geometry22,296,388,57821,746,525,438-2.47%
Holter (holdout)Pipeline27,193,408,68726,643,764,501-2.02%
FixtureMeshes / vertices / trianglesFingerprint mainFingerprint this PR
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

ISSUE_129's −0.14% is below the 1% gate on its own, but other fixtures clear it: advanced_model, Holter and schependomlaan by more than 1%, FZK by just under. No fixture rises.

This touches mesh_weld.rs and mesh_orient.rs, which #9635 also touched (merged). The hunks are disjoint, and main was merged in without conflicts. ci:native-adapter is requested because this is a geometry change.

🤖 Generated with Claude Code