perf(geometry): BVH for the host-side ray parity in booleans
What
In a boolean, the kernel classifies each fragment of the host as inside or outside the cutter with an exact ray-parity test. For the cutter's fragments against the host this already used a BVH (point_inside_bvh). For the host's fragments against the cutter it scanned every cutter triangle, twice per fragment (BComponents::inside → point_inside). So the cost grew with host fragments × cutter triangles.
BComponents now builds a per-component BVH the first time it is needed and tests only the triangles whose padded box the parity segment crosses. Nothing else in the kernel changes.
Found on real huge models (private, reported in aggregate): one opening cut through a wall (a 7,096-triangle closed opening body through a 26-triangle wall) took 50 s and held one geometry thread for half of the load's geometry phase.
Why the output is identical by construction
component_inside keeps everything that decides the verdict and changes only which triangles are tested:
- Same segment. The far endpoint is still
sound_far(p, ray_dir(), ext_k, inflated_aabb_k), the same boxpoint_insidereceived. (The existingpoint_inside_bvhuses the BVH's unpadded root box instead; that would change the endpoint, so it is not reused as is.) - Same exact test.
exact_seg_hits_triwith explicitorient3d. - Candidates are a superset of the exact hits.
- An exact crossing meets the triangle at a point inside its exact AABB.
- A BVH node is dropped only if its padded slab test fails. The pad is at least
2^-30 · reach, wherereach = 4 · (max |coord| of the component's box + ext_k)bounds every coordinate of the tree and of the query segment. operand_extentis at least twice the component's largest coordinate, andray_dir's smallest component is about 0.30, so every slab denominator is at leastreach / 40. The slab test's rounding therefore stays below about500 · ε · reach ≈ 1.1e-13 · reach, some 8,000 times under the pad.- A query whose segment reaches beyond
reachfalls back to the full scan.
- Same count. Each triangle is one leaf and a descent visits a leaf at most once. Parity is a count mod 2, so neither order nor duplicates can matter. There is no first-hit or ordering semantics on this path.
- Same budget. Explicit
orient3dcounts no escalation, so the #1109 budget trips at the same points. - Other linear scan left alone.
on_surface_normal, which is linear too, returns the first matching triangle, so its order matters. It is unchanged.
Bvh::build_with_min_pad raises the pad; a larger pad only adds candidates.
The query lives in kernel/arrangement/classify_ray_bvh.rs (moved unchanged out of classify.rs), so the revert oracle has an added production key. Its evidence (tests/evidence/geometry-host-bvh-9989/) reverse-applies a mutation that keeps every symbol the tests import but sends the parity back through the linear scan. Locally (as CI runs it) the oracle reports OBSERVED: the work test turns RED, while the verdict and identity tests stay green, as they must for an output-identical change.
Evidence
Tests (classify_bvh_tests.rs; test-only hooks behind cfg(test)):
- Verdict equivalence: BVH vs linear scan on more than 15,000 adversarial queries, all equal. Queries cover vertices, edge midpoints, face centroids, points one ulp off them, copies offset by 1e5 and −3.7e6, and a tiny component far from the origin.
- Byte identity: whole-boolean output (difference, union, intersection) equal to the forced linear path. Cases: cylinder cuts, a flush cutter, overlapping boxes, a host with many triangles.
- Work, not time:
- exact segment tests grow at most 3× per doubling of the cutter with the BVH;
- the linear baseline grows at least 3.5× (so the test discriminates);
- at 512 cutter triangles (the largest test case) the BVH does under 1/8 of the linear scan's work.
Suites on c9bd850c3d (Pop, fleet): new tests 3/3, geometry kernel tests 299/299 (incl. the pinned boolean determinism manifests), geometry lib tests 2,448/2,448, clippy -D warnings clean. 0549027b77 and dda7c02201 only shrink the new tests' sizes (same assertions); on dda7c02201 the three tests pass in 37.5 s and clippy is clean.
Mesh byte identity, whole public corpus, base 03da13dd84 (merge-base) vs branch c9bd850c3d; re-run on the head dbbc43a500 after the module move gives the same result (native 147/147, wasm 145/145 + 2/2):
- Native:
perf_probe --fingerprint(ordered per-mesh FNV-1a of positions, normals, indices, ids, colours, origins, bounds, transforms) on every.ifcintests/models/manifest.json. 147/147 identical, 0 different, 0 failed. A fixture that fails to run or lacks a fingerprint counts as a failure, never a pass. - Wasm: the digest and guards of
scripts/check-wasm-core-identity.mjs(everyMeshDataJsgetter; "failed on both" and "vacuous" guards), applied to a base and a branch bundle, both built fresh. Wasm sha256: base416a4bcc…, branchf7bb570e….- fixtures ≤ 80 MB: 145/145 identical, 136 with meshes, 0 failing on both;
- Holter and BWK, run separately: 2/2 identical.
- Same path in the browser: the change is shared kernel code, so the wasm build takes it too.
Private corpus (eight real models, 218 MB – 1.26 GB; aggregate only):
- Identity: all eight files have byte-identical per-mesh fingerprints and unchanged triangle counts.
- Same kernel work: on the three boolean-heavy files, every one of 4,656 recorded elements has the same interval / escalation / BigRational counts before and after.
- The worst element (the 7,096-triangle opening): 50.0 s → 30.9 s. That file's geometry span went 51.0 s → 36.3 s.
- Caveat: these runs were untimed and on a hot CPU (80–96 °C), so they are not timing evidence.
Synthetic reproduction (invented geometry, single thread, untimed):
- Box wall minus an N-segment cylinder: 22.4 s → 15.5 s at 7,096 cutter triangles, 1.56 s → 1.02 s at 2,048. Same output and the same kernel counters.
- The added test IFC (one wall, 7,096-triangle faceted-BRep cylinder opening) takes 13.9 s of geometry on main.
Perf ratchet (red on this PR; the cause is main, not this change):
native-instructions/fzk-haus.*: CI's own measure (scripts/perf/instructions.sh, callgrind, FZK) on currentorigin/mainvs this PR merged into it, same host. Every phase goes down: parse −0.037 %, entity scan −0.047 %, lookup −0.001 %, preprocess −0.032 %, geometry −0.181 %, total −0.150 %. The parse / scan / preprocess ceilings are exceeded by main itself.bundle/engine-wasm-brotli: same-host build of base vs branch: raw +1,533 bytes (+0.015 %), brotli-11 −148 bytes. Main has no headroom left: #9969, another Rust PR, fails the same ceiling (141 bytes over the allowed size).- A ceiling moves only through a human PR to
tests/perf-ratchets/*.json; this PR does not touch them.
Timed A/B: pending, measuring head dbbc43a500. Prepared native A/B (ifc-benchmark, 5 paired rounds, exclusive) on FZK, ISSUE_129, advanced_model, schependomlaan, Holter, BWK and the synthetic 7,096 case. Peak RSS is measured separately (the BVH costs memory). Results will be added here.
What it does not fix
For the opening above, the remaining ~28 s is retriangulation.
-
retriangulate::insert_pointstill visits every triangle of the face being retriangulated for each inserted point. Its AABB prefilter skips the exactlocatebut not the visit, so the cost is points × triangles. -
After this change the profile of that operation is
orient2d_from_lam_iv47 %,coord2d_cached21 %,insert_point10 %. -
A spatial index whose candidates come back sorted by triangle index would keep the cavity, and so the output, identical. I built and measured one (a grid over the face's triangle boxes):
- identity held;
- but it returned 41 % of the face per insertion on the real operation and 55–63 % on the synthetic cylinder, with no gain (24.9 s vs 25.1 s, untimed);
- the cause is that this insertion never flips edges, so faces fill with slivers whose boxes overlap most of the face.
Not opened. Removing the slivers (Lawson flips) changes the tessellation and is a separate decision.
-
Flush and coplanar
IfcBooleanResultcuts, the other slow pattern on these models, are not affected by this PR either.
Not for merge yet
A kernel change: it waits for a maintainer's review regardless of evidence. Labelled unqueued and ci:native-adapter.
🤖 Generated with Claude Code
Timed evidence: capped native A/B (2026-11-14, Pop, slot 96)
This is evidence of the win's size, not a gate. The instruction ratchet already shows the reduction (FZK geometry −694,611 Ir).
Setup
- One exclusive benchmark admission, three units through
quiet/capped-unit.sh; other work was capped to 2 cores, ≤ 420 s per window. perf_probe --cold --iters 1,RAYON_NUM_THREADS=8; base03da13dd84against branchdbbc43a500, in alternating ABBA order.- Every run was inside its cap window: none skipped, none crossing the window end. The package read 67–87 °C, and throttle counters moved in every run.
- The private models are the user's real files, referred to only by size alias. Triangle counts are equal in both arms for every file.
| case | runs per side | base median (ms) | branch median (ms) | change |
|---|---|---|---|---|
| private H7 (263 MB) | 4 | 35,166 | 26,126 | −25.7 % |
| synthetic wall minus 7,096-triangle cylinder | 2 | 14,577 | 10,308 | −29.3 % |
| private H4 (511 MB), control | 2 | 23,039 | 23,361 | +1.4 % |
| private H1 (1.26 GB), control | 2 | 25,833 | 24,032 | −7.0 % |
| private H2 (1.05 GB), control | 2 | 5,362 | 5,036 | −6.1 % |
Noise floor and verdict
- In an A/A run in the same admission (H7 base against base, 4 runs), the two halves' medians differed by 7.2 %, and the range was 14.5 % of the median.
- H7: the slowest branch run (26,644 ms) is faster than the fastest of all eight base runs (34,440 ms).
- Synthetic: the case separates the same way.
- Controls: their slow elements are flush/coplanar cuts, which this PR does not touch. Their changes are within the A/A spread: no regression, and no claimed gain.