Skip to content

Region build: test candidate pairs once in their lowest shared cell - #2436

Merged
FredApps merged 2 commits into
storytold:mainfrom
vangel76:perf/region-build-no-siphash-set
Nov 8, 2026
Merged

FredApps merged 2 commits into
storytold:mainfrom
vangel76:perf/region-build-no-siphash-set

Conversation

@vangel76

@vangel76 vangel76 commented Oct 28, 2026 •

Copy link
Copy Markdown
Contributor

What

region::build (HATCH and BOUNDARY picks) remembered which segment pairs it had tested in a SipHash HashSet<(usize, usize)>, over a SipHash HashMap grid. Now each candidate pair is tested exactly once, in the lowest grid cell both segments occupy (the componentwise maximum of their lowest cells), so no set is needed. The grid is a flat Vec indexed by cell.

Results do not change:

  • The set of tested pairs is the same, and split parameters are sorted afterwards, so the graph is identical.
  • Argument order passed to the crossing test is the same as before (lower index first for grid pairs; long segment first for long-vs-any; the lower index for two long segments).
  • The visit budget still counts every pair visit in every cell list, and the distinct-pair budget still counts each tested pair once.

The old search is kept as a #[cfg(test)] oracle. New tests compare old and new:

  • the sorted split lists bit for bit, over 150 random seeds with each budget swept over 0..=400, which crosses the exhaustion point for most seeds;
  • inputs with long segments (the over-4096-cells path) and exhausted visit and pair budgets;
  • the full graph (pts, adj, src, edge_src) on 60 random drawings.

Numbers

Release build, taskset -c 15, perf stat -e instructions:u, 3 picks on 12k segments (55x55 grid of squares plus 200 random lines). Output hash is identical before and after.

instructions wall time
upstream/main (336243e) 4146M 0.47-0.83 s (machine was loaded)
this PR 912M 0.09-0.10 s

Instructions are the reliable number (4.5x fewer); wall times were noisy because other builds were running.

Checks

cargo fmt --check, cargo clippy -p cadcraft-geom --all-targets -- -D warnings, cargo test -p cadcraft-geom (107 passed), cargo xtask layers. Only crates/geom/src/region.rs changes.

Closes #2430

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Improvements
    • Updated segment-intersection processing to use a consistent traversal, improving how geometry is evaluated across varied inputs.
    • Added limits on the amount of intersection checking performed. When those limits are exceeded, the search returns no result rather than continuing indefinitely.
    • Long segments continue to be checked against all segments, while duplicate checks between long segments are avoided.

vangel76 and others added 2 commits October 28, 2026 23:57
The region build kept a SipHash HashSet of tested pairs over a SipHash
HashMap grid. Each pair is now tested only in the lowest grid cell both
segments occupy, and the grid is a flat Vec. The pairs tested, the visit
and distinct-pair budgets and the output are unchanged; the old search is
kept as a test oracle.

Closes storytold#2430

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@coderabbitai

coderabbitai Bot commented Oct 28, 2026 •

Copy link
Copy Markdown

Review in Change Stack →

No actionable comments were generated in the recent review.

Recent review info
Run configuration
Commits

Reviewing files that changed from the base of the PR and between 336243e and 08ea23e.

Files selected for processing: 1
  • crates/geom/src/region.rs

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 6 remain after this review.

Walkthrough

The region builder now finds segment intersections through deterministic grid traversal with visit and distinct-pair limits. Tests compare search results and constructed graphs with the former algorithm.

Changes: Region segment-pair search

Layer / File(s) Summary
Bounded deterministic pair search
crates/geom/src/region.rs
The search replaces the hash-based grid and pair deduplication with flat-grid traversal. It tests pairs in their lowest shared cell and returns None when either budget is exceeded. Randomized tests compare results with the former search.
Builder integration and graph validation
crates/geom/src/region.rs
build delegates to build_with using the production search. build_with accepts an injected search function. Randomized tests compare graph outputs from the oracle-backed builder and production build.

Priority: Normal

Estimated code review effort: 3 (Moderate) | ~25 minutes

Change: Refactor

Possibly related issues

  • The PR implements the performance proposal to replace hash-based pair tracking with lowest-shared-cell testing, while comparing results against the prior algorithm.

Suggested reviewers: echelon, dylanmtaylor, marekspodbiedry

Merge Risk: Minimal · up to 08ea2

No identified issue prevents merging after the normal checks.

Pre-merge checks: Passed 4 · Failed 1

Failed Failed checks: 1 warning

Check name Status Explanation Resolution
Docstring Coverage Warning Docstring coverage is 57.14% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documenta… Write docstrings for the functions missing them to satisfy the coverage threshold.
Passed Passed checks: 4 passed
Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title "Region build: test candidate pairs once in their lowest shared cell" matches the main change. The raw summary and PR objectives both describe testing each candidate segment pair once, in th…
Linked Issues check Passed The PR satisfies the coding requirements in [#2430]. pair_splits_limited removes the tested-pair HashSet, tests each shared pair in its lowest shared cell, and uses a flat Vec grid. The long-seg…
Out of Scope Changes check Passed The diff is limited to crates/geom/src/region.rs. The production refactoring, injected search function, oracle, and regression tests directly support [#2430]. No unrelated behavior or files appear i…
Full details: Title check

Explanation

The title "Region build: test candidate pairs once in their lowest shared cell" matches the main change. The raw summary and PR objectives both describe testing each candidate segment pair once, in the lowest grid cell shared by both segments, inside region::build. The title does not mention the flat Vec grid or the budget changes, but those are secondary to the main change. The title is 67 characters and 11 words, which is longer than ideal but still readable and specific.

Full details: Linked Issues check

Explanation

The PR satisfies the coding requirements in [#2430]. pair_splits_limited removes the tested-pair HashSet, tests each shared pair in its lowest shared cell, and uses a flat Vec grid. The long-segment path preserves one test per pair. The code preserves visit and distinct-pair budgets. Tests compare sorted split bits with the oracle across 150 seeds and budget sweeps, cover long segments and exhausted budgets, and compare full graph output across 60 drawings.

Full details: Out of Scope Changes check

Explanation

The diff is limited to crates/geom/src/region.rs. The production refactoring, injected search function, oracle, and regression tests directly support [#2430]. No unrelated behavior or files appear in the reviewed changes.

Full details: Docstring Coverage

Explanation

Docstring coverage is 57.14% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentation-only edits and deletions within surviving functions are included; unchanged diff context and fully deleted functions are excluded. Analyzed 7 functions across 1 files.

Resolution

Write docstrings for the functions missing them to satisfy the coverage threshold.

Finishing Touches
🧪 Generate unit tests (beta)
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Comment @coderabbitai help to get the list of available commands.

@vangel76

Copy link
Copy Markdown
Contributor Author

Overview of the three independent speedups in crates/geom from the same audit (#2430, #2431, #2432), so reviewers can see them together. Each PR touches one file, they do not depend on each other and can merge in any order.

PR Where What Measured (release, one pinned core, perf stat -e instructions:u)
#2436 region.rs build (HATCH / BOUNDARY picks) No pair-dedup hash set: each pair is tested once, in the lowest shared grid cell; flat Vec grid 3 picks on 12k segments: 4146M to 912M (4.5x)
#2434 nurbs.rs derivative_unchecked No allocation up to degree 10: both bases from one sweep on the stack Spline length + 20 param_at_length: 128.9M to 74.8M (1.72x); spline fillet x30: 162.9M to 124.0M (1.31x)
#2435 spline.rs solve Elimination limited to the band of the matrix (dense storage kept) 200 fits x 100 points: 210.0M to 91.0M (2.3x); 2 fits x 1000 points: 166.7M to 32.6M (5.1x); tolerance fit, 400 points: 14.1M to 3.7M (3.8x); closed fits: +4% (they keep the full sweep)

All three keep their results bit-identical to the old code: the old implementation stays as a test-only oracle and tests compare the outputs with to_bits over random inputs (the PRs list the guards for signed zeros and non-finite input in #2435). They add no new dependencies.

Known limits: #2435 only does the band-limited loops; band storage with one factorisation for both right-hand sides is a possible follow-up. The pair-budget exit in the long-segment phase of #2436 is not covered by a test. The full cargo xtask ci was not run locally, only the geom checks; CI runs the rest.

@austin62jake austin62jake left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested on Windows 10 Pro x64 (Rust 1.99.1, cargo-nextest 0.9.150), with this PR merged onto current main (336243e). It merges cleanly, and it works.

  • All 113 cadcraft-geom tests pass, including the three that compare the new search with the old one (split lists over the budget sweep, the long-segment path, and the whole graph).
  • cargo xtask ci passes all 7 steps (2622 tests) with this PR merged onto main together with #2434 and #2435, which I tested at the same time, and #2198 so the gate runs on Windows.

Timing on this machine (release build, wall time, three runs each): three region::boundary_at picks in a 55 × 55 grid of separate squares (12,100 segments) take 27.3 ms on main and 22.1 ms with this PR, about 1.2×. My drawing has no lines crossing the squares, so few pairs share more than one cell, and the old HashSet cost less there than in your case with 200 random lines.

On memory: the flat grid is cells * cells lists with cells clamped to 512, so it allocates at most 262,144 empty Vecs (about 6 MB) whatever the input. That's bounded, and the grid.get_mut(gx * cells + gy) access can't go out of range.

@FredApps FredApps left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ReviewerA — source-read COMMENT (not an approval)

I read the first hunk: the region builder is restructured so candidate segment pairs are tested once, in their lowest shared grid cell, instead of in every cell both segments cover (which tests a pair repeatedly), as the title says. That is the standard fix for uniform-grid duplicates. It must give the same set of splits as before; floating-point results can differ in order. I did not read the new code. Add a test that compares the old and new builders on random polylines and on a set with segments crossing several cells (the same splits, compared after sorting), a degenerate zero-length segment, and collinear overlaps. Report timings on a large drawing. Nothing was run.

I ran nothing and have no exact-head CI result for this PR, so this is not an approval. Where a diff was long I read its code hunks rather than every generated or documentation line.

Status: waiting_author.

@FredApps FredApps left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed 08ea23ec2dac90c665f63350fc10cc0e043a91d3. No findings after reviewing the complete region-builder change and its call path. The componentwise maximum of the two lower cell bounds identifies exactly one shared cell; the separate long-segment pass avoids double testing long/long pairs and retains the distinct-pair budget. The randomized split, budget and whole-graph oracle tests are already present. This supersedes my earlier partial review, which asked for existing tests without reading them.

Validation: inspected the hosted gate logs, which checked this head merged into 336243e0 as 16aed82. All seven gate steps passed (fmt, clippy, tests, i18n, assets, layers, wasm), with 2631 tests passed and 1 skipped. The three added oracle comparisons passed, including the long-segment path. CodeRabbit also passed for this head. No local Rust build or benchmark was run.

@FredApps
FredApps merged commit cd71385 into storytold:main Nov 8, 2026
6 checks passed
@FredApps

FredApps commented Nov 8, 2026

Copy link
Copy Markdown
Contributor

Merged 08ea23ec into main as cd71385d. The approved head was unchanged, maintainer edits were enabled, and the hosted gate passed. No additional local tests were run for this merge.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Perf] Region build tests candidate pairs through a SipHash set; 78% of a HATCH/BOUNDARY pick is hashing

3 participants