Sorted-batch evaluators for LinearInterpolation and CubicSpline (~9x speedup)#527
Draft
ChrisRackauckas-Claude wants to merge 1 commit into
Draft
Conversation
Extends the pattern from SciML#526 to two more piecewise interpolation types. When the query vector is sorted and the extrapolation modes are simple (None / Constant / Linear / Extension), walk the knots and queries in lockstep in O(n+m) instead of running a per-query binary search via the iguesser. Falls back to `map!` when the inputs don't fit the fast path: unsorted `tt`, Periodic/Reflective extrapolation, or (for LinearInterpolation) `u` containing NaN — preserving the existing NaN-handling semantics from `_interpolate(::LinearInterpolation, ...)`. The scalar `_interpolate`, derivative, integral, AD, and plotting paths are unchanged. Output matches the per-point path to floating-point round-off, verified across each fast-path extrapolation mode paired left × right, both `cache_parameters` settings for CubicSpline, knot pass-through, the Periodic/Reflective fallback, the `None` throwing behavior on out-of-range sorted queries, and the NaN-in-u fallback for LinearInterpolation. Benchmarks (Julia 1.10, n=256, m=4096 sorted queries, BenchmarkTools `minimum`): LinearInterpolation: 126 us -> 13.6 us 9.3x CubicSpline: 167 us -> 19.7 us 8.5x Unsorted-input fallback path stays at ~450 us for both (matches the prior `map!` cost; the `issorted` short-circuits on the first out-of-order pair). Co-Authored-By: Chris Rackauckas <accounts@chrisrackauckas.com>
This was referenced May 15, 2026
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.
Summary
Extends the sorted-batch fast path introduced in #526 (
AkimaInterpolation) to two more piecewise interpolation types:LinearInterpolationCubicSplineWhen the query vector
ttis sorted and the extrapolation modes are simple (None,Constant,Linear,Extension), walks the knots and queries in lockstep in O(n+m) instead of running a per-query binary search via the iguesser. Falls back tomap!when the inputs don't fit the fast path: unsortedtt, Periodic/Reflective extrapolation, or (forLinearInterpolation)ucontaining NaN — preserving the existing NaN-handling semantics from_interpolate(::LinearInterpolation, ...).The scalar
_interpolate, derivative, integral, AD, and plotting paths are unchanged. Output matches the per-point path to floating-point round-off.Benchmarks
Julia 1.10, n=256 knots, m=4096 sorted queries, BenchmarkTools
minimum:LinearInterpolationCubicSplineFor reference,
AkimaInterpolation(post-#526) is at 16.1 μs on the same setup — Linear edges it out slightly with the simpler polynomial; CubicSpline trails due to the extra arithmetic per query.Unsorted-input fallback stays at ~450 μs for both (
issortedshort-circuits on the first out-of-order pair, thenmap!cost dominates).Why these two
Of the piecewise types, these have the highest per-query baseline cost relative to the binary-search overhead, so they get the largest win from the lockstep walk. Hermite splines (
CubicHermiteSpline,QuinticHermiteSpline) andQuadraticSplineare equally amenable to the same pattern — happy to do them as a follow-up if this design is accepted.What is in this PR
src/interpolation_methods.jl: adds(A::LinearInterpolation{<:AbstractVector{<:Number}})(out, tt)and(A::CubicSpline{<:AbstractVector{<:Number}})(out, tt)callables, each with their own_*_eval_fast_applicablepredicate and_*_eval_sorted!inner loop. The Linear and Extension extrapolation cases collapse to one branch forLinearInterpolation(a line is linear); they're kept separate forCubicSplinesince the boundary slope and the continued cubic differ.test/interpolation_tests.jl: new "Sorted-batch evaluator" sub-testsets in the Linear and CubicSpline sections, mirroring the coverage from Optimize AkimaInterpolation constructor (3.6-4.1x) and add sorted-batch evaluator (8-9x) #526 (all fast-path extrapolation modes paired left × right, knot pass-through, Periodic/Reflective fallback,Nonethrowing, unsorted fallback,DimensionMismatch, pluscache_parameters=truefor CubicSpline and the NaN-in-u fallback for Linear).Test plan
Pkg.test()all groups pass locally on Julia 1.10 (Core 2738 / Methods 42151 / Extensions 13178 / Misc 11)max diffprint before adding the testset)Please ignore until reviewed by @ChrisRackauckas.
🤖 Generated with Claude Code