Skip to content

Extend to_indices to accept any integer in StaticArray index - #1352

Merged
mateuszbaran merged 3 commits into
JuliaArrays:masterfrom
barche:to-indices-fix
Sep 16, 2026
Merged

mateuszbaran merged 3 commits into
JuliaArrays:masterfrom
barche:to-indices-fix

Conversation

@barche

@barche barche commented Sep 13, 2026

Copy link
Copy Markdown
Contributor

When indexing an array using a StaticArray as indices, the return type depends on the element type of the StaticArray, resulting in a slowdown when the type differs from Int, as seen in the normals calculation code in GeometryBasics.jl (see PR JuliaGeometry/GeometryBasics.jl#285). This change makes sure any kind of integer behaves the same way and always returns an SVector as the indexing result.

@mateuszbaran
mateuszbaran merged commit 37a3c92 into JuliaArrays:master Sep 16, 2026
24 checks passed
@ffreyer

ffreyer commented Sep 16, 2026

Copy link
Copy Markdown

This seems to have broken

v = SVector(1,2,3,4)
v[v .== 1]

With 1.9.20 this returns [1] (base array), with 1.9.21 it errors:

ERROR: MethodError: no method matching index_sizes(::Base.LogicalIndex{Int64, SVector{4, Bool}})
The function `index_sizes` exists, but no method is defined for this combination of argument types.

Closest candidates are:
  index_sizes()
   @ StaticArrays ~/.julia/packages/StaticArrays/y4kA2/src/indexing.jl:83
  index_sizes(::Int64, Any...)
   @ StaticArrays ~/.julia/packages/StaticArrays/y4kA2/src/indexing.jl:84
  index_sizes(::S, Any...) where S<:Size
   @ StaticArrays ~/.julia/packages/StaticArrays/y4kA2/src/indexing.jl:81
  ...

Stacktrace:
 [1] _getindex(::IndexLinear, ::SVector{4, Int64}, ::StaticArrays.StaticIndexing{Base.LogicalIndex{Int64, SVector{4, Bool}}})
   @ StaticArrays ~/.julia/packages/StaticArrays/y4kA2/src/indexing.jl:265
 [2] getindex(A::SVector{4, Int64}, I::SVector{4, Bool})
   @ Base ./abstractarray.jl:1342
 [3] top-level scope
   @ REPL[4]:1

@mateuszbaran

Copy link
Copy Markdown
Collaborator

Thanks for reporting, it's odd we don't test for that. I'll fix it in a moment.

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