Cut ~57% of load-time invalidations#1344
Open
Beforerr wants to merge 1 commit into
Open
Conversation
Three small signature tweaks that cut invalidated MIs ~57% (2030 -> 875)
and remove two whole invalidation trees:
- `eachindex(::IndexLinear, ::StaticArray)`: narrow to rank N >= 2 via a
`Union{StaticArray{<:Tuple,T,N} where T for N in 2:32...}`. Vectors fall
through to `Base.axes1` which already returns `SOneTo`. Concrete `N`
in the signature is what makes the invalidator see `Union{}` against
`AbstractVector{X}`; a `where N` clause does not.
- `any`/`all`/`count` with a function argument: drop the specialised
methods. Base routes `any(f, A)` through `mapreduce(f, |, A)`, and
we already specialise `mapreduce` for `StaticArray`, so the fast
path is preserved. The removed `::Bool` cast was defensive and not
needed for static eltypes; the explicit `init=false` matches the
default for `|`/`&`/`+`.
- `setindex!(::TrivialView, inds...)`: add the missing `v` slot.
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
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.
Three small dispatch tweaks that drop invalidated
MethodInstances from ~2030 to ~875 on Julia 1.12, with no behaviour change and all tests passing. Related to #1074.What changed
1.
eachindex(::IndexLinear, ::StaticArray)→ restrict to rank N ≥ 2.Base's
eachindex(::IndexLinear, ::AbstractVector) = axes1(A)already returnsSOneTofor static vectors, so we only need our specialised path for higher ranks. Pinning rank with concreteN(via aUnionover2:32) is what makes Julia's invalidator seeUnion{}againstAbstractVector{X}— awhere Nclause still intersects becauseNcould be 1. Wipes the entireeachindexinvalidation tree (~513 MIs).2. Drop
any(f::Function, ::StaticArray),all(f::Function, ::StaticArray),count(f, ::StaticArray).Base's
any(f, A)→_any(f, A, dims)→mapreduce(f, |, A; ...). Since we already specialisemapreduceforStaticArray, the fast path is preserved without the extra method. The dropped::Boolcast was defensive and isn't needed when eltypes are statically known;init=falsematches the default_InitialValuebehaviour for|/&/+. Removes the biggest single invalidation source (~640 MIs from compiler-internalany(::Function, ::AbstractArray)callers).3.
setindex!(::TrivialView, inds...)→setindex!(::TrivialView, v, inds...).The old signature was missing the value slot, so it superseded
Base.setindex!(::AbstractArray, v, I...)more aggressively than needed. -4 MIs.Numbers (Julia 1.12.6, this branch)
@time_importsLoad time is dominated by C-level method-table registration so the wall-clock win is small, but downstream packages that hit
AbstractVectororany(::Function, ::AbstractArray)will see less recompilation triggered byusing StaticArrays.Tested
Full
Pkg.test()suite passes locally.