perf(geometry): run incircle stages C and D without zeroing 18 KB per call

Closed
LO
21d ago
+282-18

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

What was unnecessary

#9016 runs stages A and B of the in-circle test with right-sized scratch. It hands whatever stage B cannot decide to geometry_predicates::incircle, on the assumption that this path is rare. It is not rare. On ISSUE_129, 43,331 calls reach incircleadapt, costing 1.03G Ir, and about 78% of that (806M) is a memset. incircleadapt zeroes about 20 KB of scratch on every call, 18 KB of it the two 1152-entry fin expansions, although every read of those stays inside the prefix the same call wrote. FZK spends 21.6M Ir (about 5% of its pipeline) on the same path. From the call graph, most of these calls go on into stage D: about 7.6 extra expansion scalings per call.

Change

stage_b now hands back its own state (the stage-B expansion, the differences and their tails, the three 2×2 minors) instead of the call being redone by the crate. The new cdt/incircle_tail.rs continues from that state through stage C and stage D. It is a port of incircleadapt that uses the crate's public expansion routines with the same operands in the same order, so it returns the same f64 bits.

The two fin buffers are this thread's reused scratch (fixed at 2 × 1152 f64, allocated once, and never re-zeroed). That is safe because they are only read below the length the same call wrote. Every other buffer is still zeroed per call, exactly as in the crate, because the crate reads past a written prefix in two places, and the port keeps both:

  • the bct/cat/abt defaults of length 1;
  • cxtab, whose length the crate shadows and therefore reads as 8.

There is no unsafe code. The per-thread scratch is fixed at 18 KB.

For review: licence provenance. incircle_tail.rs follows geometry-predicates 0.3.0's incircleadapt operation for operation. That crate is MIT-licensed (© 2017 Egor Larionov, a port of Shewchuk's public-domain predicates), so the file header carries its copyright line. Please confirm that MIT-derived code in this file is acceptable. The alternative would be upstreaming the fix to the crate.

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. Measured: commit 274692c5aa alone (main plus this one commit), against the main baseline. (An earlier revision of this body showed #9636's table by mistake; corrected.)

FixturePhaseBefore IrAfter IrΔ
FZKParse90,464,33090,448,252-0.02%
FZKScan/prepass55,377,49055,364,171-0.02%
FZKLookup11,766,55011,767,731+0.01%
FZKPreprocess23,232,37823,228,438-0.02%
FZKGeometry342,404,967323,771,226-5.44%
FZKPipeline432,874,522414,224,735-4.31%
advanced_modelParse979,435,806979,168,792-0.03%
advanced_modelScan/prepass973,349,199973,082,009-0.03%
advanced_modelLookup229,983229,983+0.00%
advanced_modelPreprocess5,716,1305,716,325+0.00%
advanced_modelGeometry17,165,943,98117,149,841,494-0.09%
advanced_modelPipeline18,145,404,66618,129,035,231-0.09%
schependomlaanParse1,418,372,5321,418,319,057-0.00%
schependomlaanScan/prepass951,772,820951,674,044-0.01%
schependomlaanLookup454,306,360454,354,136+0.01%
schependomlaanPreprocess8,760,8599,072,290+3.55%
schependomlaanGeometry2,294,618,6382,294,572,718-0.00%
schependomlaanPipeline3,713,013,2483,712,913,859-0.00%
ISSUE_129Parse257,065,408256,983,700-0.03%
ISSUE_129Scan/prepass251,436,897251,356,754-0.03%
ISSUE_129Lookup21,45321,453+0.00%
ISSUE_129Preprocess5,476,7285,475,153-0.03%
ISSUE_129Geometry25,630,362,62924,764,312,289-3.38%
ISSUE_129Pipeline25,887,460,47025,021,328,614-3.35%
Holter (holdout)Parse4,897,361,1254,897,354,193-0.00%
Holter (holdout)Scan/prepass4,207,203,9604,207,133,491-0.00%
Holter (holdout)Lookup677,871,380677,933,492+0.01%
Holter (holdout)Preprocess5,197,6945,200,199+0.05%
Holter (holdout)Geometry25,000,452,94624,880,435,109-0.48%
Holter (holdout)Pipeline29,897,921,94229,777,897,956-0.40%
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

schependomlaan's preprocess phase rises by about 0.3M instructions. #9632, #9635 and #9636, which share no code with this change, show the same rise, so it comes from the measurement run, not from this change.

Identity and tests

  • The existing incircle_matches_geometry_predicates_bit_for_bit_issue_9016 now exercises the port. It compares bits against geometry_predicates::incircle on every path: generic inputs, lattice-cocircular inputs, collinear constraints, more than 1,000 ulp-nudged stage-C/D cases, near-cocircular inputs in the stage-B error-bound band, extreme magnitudes, and non-finite inputs. Passes. All 15 cdt unit tests pass, as does cargo clippy -p ifc-lite-geometry --all-targets -- -D warnings.
  • perf_probe fingerprints are identical on all five fixtures (table above), and so is ISSUE_129's csgFailures count (9 = 9).
  • Still pending (queued on Pop, results will be added here): clippy -D warnings for core, geometry and processing; full cargo test of ifc-lite-core, ifc-lite-geometry and ifc-lite-processing on the 121-model corpus (census, golden, mesh_determinism); the export_golden test; cargo check --target wasm32-unknown-unknown -p ifc-lite-wasm.

This is independent of #9632, #9635 and #9636 (no shared code), and is a separate branch off main. Ledger entry added under "Shipped wins". ci:native-adapter is requested because this is a geometry change.

🤖 Generated with Claude Code