Skip to content

perf(geom): limit the spline fit solver to the matrix band - #2435

Merged
FredApps merged 1 commit into
storytold:mainfrom
vangel76:perf/spline-solve-band
Nov 8, 2026
Merged

FredApps merged 1 commit into
storytold:mainfrom
vangel76:perf/spline-solve-band

Conversation

@vangel76

@vangel76 vangel76 commented Oct 28, 2026 •

Copy link
Copy Markdown
Contributor

What

solve in crates/geom/src/spline.rs (used by open, closed and least-squares spline fits) ran dense Gaussian elimination on a banded matrix. The pivot scan, the elimination loops and the back-substitution now stop at the band, measured from the matrix (lower width kl, upper width ku; partial pivoting widens the upper band to at most kl + ku). Storage is still dense. The cyclic matrix of a closed fit has full bandwidth and keeps the old cost.

The skipped operations only combine entries that are exactly +0.0, so results are bit-identical:

  • A stored -0.0 counts as non-zero when measuring the band, because the skipped entries rely on being +0.0.
  • A back-substitution row whose banded sum is zero redoes the full sum, because skipped zero terms could flip the sign of a zero.
  • A matrix or right-hand side with NaN, infinity or magnitudes above 1e100 takes the full sweep, because a skipped 0 * x is not zero for non-finite x. The 1e100 limit is a guard, not a proof: finite input that overflows during elimination could still differ from the full sweep in which non-finite value appears.

The change is inside solve. It does not touch the other parts of spline.rs that #2129 and #2122 change. git merge-tree against current upstream/main is clean.

Measurements

Release build, taskset on one core, perf stat -e instructions:u, same bench program for both binaries (output hashes identical for old and new):

Scenario Before After Ratio
200 open fits x 100 points 210.0M 91.0M 2.3x
2 open fits x 1000 points 166.7M 32.6M 5.1x
tolerance fit, 400 points 14.1M 3.7M 3.8x
20 closed fits x 100 points (full bandwidth) 22.5M 23.5M 0.96x

The closed case pays a small fixed cost for the measurement, which stops after two rows when the matrix is cyclic.

Tests

  • banded_solve_matches_the_dense_sweep_bit_for_bit: the old solve is kept as a test-only oracle (solve_dense). Random matrices of sizes 1 to 120 with many band shapes, including full width, compared with to_bits, with right-hand sides containing +0.0 and -0.0.
  • solve_handles_negative_zeros_signed_zero_sums_and_hostile_values: stored -0.0, NaN, infinity, huge values, singular and empty systems.
  • fits_are_bit_identical_to_the_dense_solver: whole fits (open, closed, tolerance, end tangents, chord knots, kinks, points with exact zero coordinates; sizes 3, 4, 5, 7, 12, 30, 100, 150, 300 and 1000) run once with the new solve and once with the oracle; knots, control points, weights and fit points compared with to_bits.
  • the_cyclic_matrix_of_a_closed_fit_keeps_the_full_sweep.
  • I checked that the tests fail when the band is narrowed or the zero-sum redo is removed. They do not fail when the non-finite guard is removed (the dense sweep also gives NaN there), so that guard is only exercised as a no-crash/same-result case.
  • cargo test -p cadcraft-geom passes (108 tests); cargo fmt and cargo clippy -p cadcraft-geom --all-targets -- -D warnings are clean. I did not run the full cargo xtask ci; only crates/geom changed.

Not in this PR

Band storage and one factorisation for both right-hand sides (step 2 in the issue) stays a follow-up: it changes the storage and the callers, which is more than this change.

Part of #2432

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Performance
    • Spline fitting now limits calculations to relevant matrix bands for typical inputs, reducing unnecessary work.
    • Unusual numeric values continue to use a full-width calculation path, preserving results across edge cases.

`solve` ran dense Gaussian elimination on a banded matrix. The pivot
scan, the elimination and the back-substitution now stop at the band
measured from the matrix. The skipped operations only combine exact
zeros, so results are bit-identical; the cyclic matrix of a closed fit
has full bandwidth and keeps the full sweep. The old solver stays as a
test-only oracle.

Part of storytold#2432

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 98b47c1.

Files selected for processing: 2
  • crates/geom/src/spline.rs
  • crates/geom/src/spline/tests.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 spline solver now measures matrix bandwidth and limits pivot searches and elimination updates to those bands. It uses full-width processing for non-finite or large values. Tests compare solver results and fitted spline data bit-for-bit with a dense reference solver.

Changes: Spline solver

Layer / File(s) Summary
Bound solver operations to matrix bands
crates/geom/src/spline.rs
solve measures matrix bandwidths and bounds pivot searches and elimination updates. It uses full-width processing for non-finite or large values. Back-substitution uses the widened upper band, with a full-row sum when the banded sum is zero.
Validate against dense solver and fit output
crates/geom/src/spline.rs, crates/geom/src/spline/tests.rs
Test-only helpers provide a dense solver oracle. Tests compare solver and spline-fit results bit-for-bit across varied matrix widths, values, options, and input sizes.

Priority: Low

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

Change: Refactor

Possibly related issues

  • Issue 2432 in storytold/cadcraft reports dense elimination for banded spline-fit matrices. This change limits solver loops to the measured matrix band.
  • Issue 1933 in storytold/cadcraft reports dense O(n³) solving for large spline fits. This change skips operations outside the matrix band.
  • Issue 1074 in storytold/cadcraft reports dense O(n³) solving for splines with many fit points. This change limits solver operations to the matrix band.

Suggested reviewers: echelon, aiongg

Merge Risk: Minimal · up to 98b47

No actionable issue remains from this review. The change is mergeable after 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 42.86% 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 clearly and concisely describes the main change: limiting the spline fit solver to the matrix band for performance.
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Docstring Coverage

Explanation

Docstring coverage is 42.86% 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 2 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 banded_solve_matches_the_dense_sweep_bit_for_bit, the hostile-values test and fits_are_bit_identical_to_the_dense_solver.
  • cargo xtask ci passes all 7 steps (2622 tests) with this PR merged onto main together with #2434 and #2436, 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; measured with #2434 and #2436 merged too): five Spline::from_fit_points through 1,000 points take 42.8 ms on main and 28.3 ms with this PR, about 1.5×. That's less than your 5.1× for the same size, probably because my number is wall time for the whole fit, not instructions in solve.

I checked the early exit for the cyclic matrix: when the last row reaches column 0, kl = n - 1 makes the pivot range, the elimination range and the back-substitution range all reach n, so leaving the other rows unmeasured can't narrow anything. The + 1/min(n - 1) bounds only run inside for c in 0..n, so n - 1 can't underflow.

@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 Gaussian elimination in the spline fit solver is limited to the band of the interpolation and least-squares matrices (lower width kl, upper width ku, and partial pivoting widening the upper band to at most kl + ku), and the comment says the skipped operations only combine entries that are exactly +0.0, so the result is bit-identical. That claim is the thing to test: with partial pivoting, row swaps within the band keep the zeros structure (as in LAPACK's banded solvers), but -0.0 and NaN can differ from the full sweep. I did not read the rest. Add a test comparing the banded and full solvers bit for bit on random banded systems, for several bandwidths, plus an ill-conditioned one and a singular one (the same error), and a benchmark with the speedup. 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 98b47c1984d4dfcd6fe799e6a6cad838ba667252. No findings after reviewing both changed files and the solver callers. Checked pivot/elimination bandwidths, the closed-fit full-width path, signed-zero handling, and the non-finite/large-value fallback. The dense-oracle comparisons, singular/empty systems and hostile-value tests are already present; this supersedes my earlier partial review, which asked for that coverage without inspecting it.

Validation: inspected the hosted gate logs, which checked this head merged into 336243e0 as ed53b24. All seven gate steps passed (fmt, clippy, tests, i18n, assets, layers, wasm), with 2632 tests passed and 1 skipped. The banded/dense, hostile-value, full-fit and cyclic-matrix comparisons all passed. CodeRabbit also passed for this head. No local Rust build or benchmark was run.

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

FredApps commented Nov 8, 2026

Copy link
Copy Markdown
Contributor

Merged 98b47c19 into main as 004cbecd. 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.

3 participants