Skip to content

Commit 68caeec

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

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

base/multidimensional.jl

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -358,8 +358,12 @@ 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
363+
# specialize to gives better hint to compiler to enable SIMD (#42115)
364+
@inline _get_cartesianindex(::OneTo, i::Int) = i
365+
@inline _get_cartesianindex(::Base.IdentityUnitRange, i::Int) = i
366+
@inline _get_cartesianindex(r, i) = getindex(r, i)
363367

364368
# CartesianIndices act as a multidimensional range, so cartesian indexing of CartesianIndices
365369
# with compatible dimensions may be seen as indexing into the component ranges.

0 commit comments

Comments
 (0)