Skip to content

Commit fce2daf

Browse files
committed
specialize getindex for CartesianIndices of type OneTo and IdentityUnitRange
1 parent 876df79 commit fce2daf

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

base/multidimensional.jl

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -358,21 +358,26 @@ module IteratorsMD
358358
CartesianIndex()
359359
end
360360
@propagate_inbounds function Base.getindex(iter::CartesianIndices{N,R}, I::Vararg{Int, N}) where {N,R}
361-
CartesianIndex(getindex.(iter.indices, I))
361+
CartesianIndex(_get_cartesianindex.(iter.indices, I))
362362
end
363363

364364
# CartesianIndices act as a multidimensional range, so cartesian indexing of CartesianIndices
365365
# with compatible dimensions may be seen as indexing into the component ranges.
366366
# This may use the special indexing behavior implemented for ranges to return another CartesianIndices
367367
@propagate_inbounds function Base.getindex(iter::CartesianIndices{N,R},
368368
I::Vararg{Union{OrdinalRange{<:Integer, <:Integer}, Colon}, N}) where {N,R}
369-
CartesianIndices(getindex.(iter.indices, I))
369+
CartesianIndices(_get_cartesianindex.(iter.indices, I))
370370
end
371371
@propagate_inbounds function Base.getindex(iter::CartesianIndices{N},
372372
C::CartesianIndices{N}) where {N}
373-
CartesianIndices(getindex.(iter.indices, C.indices))
373+
CartesianIndices(_get_cartesianindex.(iter.indices, C.indices))
374374
end
375375

376+
# specialize to gives better hint to compiler to enable SIMD (#42115)
377+
@inline _get_cartesianindex(::OneTo, i::Int) = i
378+
@inline _get_cartesianindex(::Base.IdentityUnitRange, i::Int) = i
379+
@inline _get_cartesianindex(r, i) = getindex(r, i)
380+
376381
# If dimensions permit, we may index into a CartesianIndices directly instead of constructing a SubArray wrapper
377382
@propagate_inbounds function Base.view(c::CartesianIndices{N}, r::Vararg{Union{OrdinalRange{<:Integer, <:Integer}, Colon},N}) where {N}
378383
getindex(c, r...)

0 commit comments

Comments
 (0)