Repository navigation
perf: build a missing polar box without copying the surface cache in AdvSurfaceIntersector (outputs bit-identical) - #1033
Merged
Conversation
…AdvSurfaceIntersector (outputs bit-identical) SmAdvSurfaceIntersector::BranchIsGaussSeparate reads the polar box of a tree node and builds it lazily when it is missing (GetPolarBox). The port shares the surface caches, so it copied the whole surface cache (tree, patches, aux data) to build the box on the copy, and dropped the copy. SurfaceCache::polar_box_unstored builds the same box from a shared reference without storing it, as surface p8, p10, p11 and the silhouette code already do. It gives the box polar_box gives on a copy: the same chain from the nearest ancestor with a box, the same guesses, and the stored box on a failure. On the lofted solids of tangency-labs#948, on top of the curve-cache memo (tangency-labs#992) and tangency-labs#1006 and tangency-labs#993, this saves 1.8 % of the instructions at 20 sections and 2.0 % at 40, and 1.1 % of the CPU cycles at both (median of 3 runs). On main alone the change is below the noise. Results are unchanged. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
bherbruck
approved these changes
Oct 25, 2026
bherbruck
left a comment
Collaborator
There was a problem hiding this comment.
Approved by the automated first-pass review: CI green, mergeable, no CI/build/dependency file changes, no red-flag patterns in added code (process/network/env/unsafe outside FFI/blobs/hidden unicode), 0 known-mismatch entries added. Logic is gated by CI and the difftest merge queue.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
A surface/surface intersection copied the whole surface cache each time it needed a polar box that had not been built yet, then dropped the copy. This PR builds the box from the shared cache without copying it. On the lofted solids of #948, on top of #992, #1006 and #993, this saves 1.8 % of the instructions at 20 sections and 2.0 % at 40, and 1.1 % of the CPU cycles. On main alone the change is below the noise. No result changes.
Cause
SmAdvSurfaceIntersector::BranchIsGaussSeparatecompares the polar boxes (normal cones) of two surface tree nodes and builds a missing box lazily (SmBezierAux2d::GetPolarBox). The port shares the surface caches between the solvers, soAdvSurfaceIntersector::polar_box(intersect/adv_surface_intersector.rs:656) did this:It copied the whole cache (tree, patches, aux data) to build one box, and did not keep it. Our rule for copies is: clone only when both copies survive and are mutated independently. Here the copy is dropped at once and only one value is read, so this is the read-only case: compute from
&SurfaceCache.In a clone audit of the #948 run on main + #992 + #1006 + #993 (frame-pointer profile, perf cycles), this copy was 0.5 % of all samples at 20 sections and 0.3 % at 40.
Change
AdvSurfaceIntersector::polar_boxcallsSurfaceCache::polar_box_unstored(k, id). That function builds the box from a shared reference without storing it, as surface p8, p10 and p11 and the silhouette code already do for the same reason. It gives whatpolar_boxgives on a copy:The module header records the change.
This PR does not keep the built box, and upstream does. Keeping it on the shared cache needs the cache to be mutable or lazily filled through
&self; that is part of the handle-agnostic cache design (issue draft: lazy parts inOnceLock).Test plan
Input: the outer and inner skins of #948 at 20 and 40 sections,
outer - inner, then∩ box;examples/toebox_loft.rs(local only). Release builds, pinned to the 4 performance cores, 3 runs each, interleaved. The counts are user-spacecpu_coreevents for the whole run.On main
b78b088a3+ #992 + #1006 + #993 (where the audit found the copy):On main
bff43b1bbalone, the difference is within the noise: instructions +0.05 % at 20 sections and -0.07 % at 40, cycles +0.2 % and -0.1 %. I did not find out why the instruction count at 20 sections is slightly higher on main.cargo fmt --all --check,cargo clippy --workspace --all-targets -- -D warnings,cargo check -p solidcore --target wasm32-unknown-unknown,cargo test -p solidcore: pass.--unit, 0 failed andmax_rel_err=0e0in each:intersect.adv_surface_intersector(1931 cases),surface.surface_cache_p1(872),intersect.surface_intersector_p5(1725),topology.face_p3(1781),boolean.api_brep,boolean.merge_p4.Not run
--greendifftest run (CI runs it).Related
#948 (the input), #982, #992, #1006 and #993 (the stack where the copy shows).