Skip to content

Commit d1b1d84

Browse files
authored
implement SubArray reindexing without using @generated (#58800)
The tuple specialization for indexing with UnitRanges is highly optimized, type stable, and constant foldable for statically known values like these. Seems like a very good thing to just lean on that. It's how I would've written this in the first place had that optimized method existed when I wrote it. It's much more readable and — I imagine — it should be quite a bit friendlier to the compiler. The only time you should see a performance difference here is when you have a SubArray with 11 or more indices **and** one of those indices is 3-dimensional or more.
1 parent 633ef96 commit d1b1d84

File tree

1 file changed

+2
-9
lines changed

1 file changed

+2
-9
lines changed

base/subarray.jl

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -300,15 +300,8 @@ reindex(idxs::Tuple{AbstractMatrix, Vararg{Any}}, subidxs::Tuple{Any, Any, Varar
300300
(@_propagate_inbounds_meta; (idxs[1][subidxs[1], subidxs[2]], reindex(tail(idxs), tail(tail(subidxs)))...))
301301

302302
# In general, we index N-dimensional parent arrays with N indices
303-
@generated function reindex(idxs::Tuple{AbstractArray{T,N}, Vararg{Any}}, subidxs::Tuple{Vararg{Any}}) where {T,N}
304-
if length(subidxs.parameters) >= N
305-
subs = [:(subidxs[$d]) for d in 1:N]
306-
tail = [:(subidxs[$d]) for d in N+1:length(subidxs.parameters)]
307-
:(@_propagate_inbounds_meta; (idxs[1][$(subs...)], reindex(tail(idxs), ($(tail...),))...))
308-
else
309-
:(throw(ArgumentError("cannot re-index SubArray with fewer indices than dimensions\nThis should not occur; please submit a bug report.")))
310-
end
311-
end
303+
reindex(idxs::Tuple{AbstractArray{<:Any,N}, Vararg{Any}}, subidxs::Tuple{Vararg{Any}}) where {N} =
304+
(@_propagate_inbounds_meta; (idxs[1][subidxs[1:N]...], reindex(tail(idxs), subidxs[N+1:end])...))
312305

313306
# In general, we simply re-index the parent indices by the provided ones
314307
SlowSubArray{T,N,P,I} = SubArray{T,N,P,I,false}

0 commit comments

Comments
 (0)