Repository navigation
perf(geom): limit the spline fit solver to the matrix band - #2435
Conversation
`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>
|
No actionable comments were generated in the recent review.
|
| 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:
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: 
4 · 
1
-
Failed checks: 1 warning Passed checks: 4 passedFull 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.
|
Overview of the three independent speedups in
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 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 |
austin62jake
left a comment
There was a problem hiding this comment.
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-geomtests pass, includingbanded_solve_matches_the_dense_sweep_bit_for_bit, the hostile-values test andfits_are_bit_identical_to_the_dense_solver. cargo xtask cipasses all 7 steps (2622 tests) with this PR merged ontomaintogether 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
left a comment
There was a problem hiding this comment.
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
left a comment
There was a problem hiding this comment.
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.
|
Merged |
What
solveincrates/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 widthkl, upper widthku; partial pivoting widens the upper band to at mostkl + 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:-0.0counts as non-zero when measuring the band, because the skipped entries rely on being+0.0.1e100takes the full sweep, because a skipped0 * xis not zero for non-finitex. The1e100limit 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-treeagainst current upstream/main is clean.Measurements
Release build,
taskseton one core,perf stat -e instructions:u, same bench program for both binaries (output hashes identical for old and new):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 oldsolveis kept as a test-only oracle (solve_dense). Random matrices of sizes 1 to 120 with many band shapes, including full width, compared withto_bits, with right-hand sides containing+0.0and-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 newsolveand once with the oracle; knots, control points, weights and fit points compared withto_bits.the_cyclic_matrix_of_a_closed_fit_keeps_the_full_sweep.cargo test -p cadcraft-geompasses (108 tests);cargo fmtandcargo clippy -p cadcraft-geom --all-targets -- -D warningsare clean. I did not run the fullcargo 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