Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert "Avoid allocations in views of views (#53231)" #57044

Merged
merged 1 commit into from
Jan 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions base/subarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -291,18 +291,18 @@ reindex(idxs::Tuple{Slice, Vararg{Any}}, subidxs::Tuple{Any, Vararg{Any}}) =

# Re-index into parent vectors with one subindex
reindex(idxs::Tuple{AbstractVector, Vararg{Any}}, subidxs::Tuple{Any, Vararg{Any}}) =
(@_propagate_inbounds_meta; (maybeview(idxs[1], subidxs[1]), reindex(tail(idxs), tail(subidxs))...))
(@_propagate_inbounds_meta; (idxs[1][subidxs[1]], reindex(tail(idxs), tail(subidxs))...))

# Parent matrices are re-indexed with two sub-indices
reindex(idxs::Tuple{AbstractMatrix, Vararg{Any}}, subidxs::Tuple{Any, Any, Vararg{Any}}) =
(@_propagate_inbounds_meta; (maybeview(idxs[1], subidxs[1], subidxs[2]), reindex(tail(idxs), tail(tail(subidxs)))...))
(@_propagate_inbounds_meta; (idxs[1][subidxs[1], subidxs[2]], reindex(tail(idxs), tail(tail(subidxs)))...))

# In general, we index N-dimensional parent arrays with N indices
@generated function reindex(idxs::Tuple{AbstractArray{T,N}, Vararg{Any}}, subidxs::Tuple{Vararg{Any}}) where {T,N}
if length(subidxs.parameters) >= N
subs = [:(subidxs[$d]) for d in 1:N]
tail = [:(subidxs[$d]) for d in N+1:length(subidxs.parameters)]
:(@_propagate_inbounds_meta; (maybeview(idxs[1], $(subs...)), reindex(tail(idxs), ($(tail...),))...))
:(@_propagate_inbounds_meta; (idxs[1][$(subs...)], reindex(tail(idxs), ($(tail...),))...))
else
:(throw(ArgumentError("cannot re-index SubArray with fewer indices than dimensions\nThis should not occur; please submit a bug report.")))
end
Expand Down
25 changes: 0 additions & 25 deletions test/subarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1027,31 +1027,6 @@ catch err
err isa ErrorException && startswith(err.msg, "syntax:")
end


@testset "avoid allocating in reindex" begin
a = reshape(1:16, 4, 4)
inds = ([2,3], [3,4])
av = view(a, inds...)
av2 = view(av, 1, 1)
@test parentindices(av2) === (2,3)
av2 = view(av, 2:2, 2:2)
@test parentindices(av2) === (view(inds[1], 2:2), view(inds[2], 2:2))

inds = (reshape([eachindex(a);], size(a)),)
av = view(a, inds...)
av2 = view(av, 1, 1)
@test parentindices(av2) === (1,)
av2 = view(av, 2:2, 2:2)
@test parentindices(av2) === (view(inds[1], 2:2, 2:2),)

inds = (reshape([eachindex(a);], size(a)..., 1),)
av = view(a, inds...)
av2 = view(av, 1, 1, 1)
@test parentindices(av2) === (1,)
av2 = view(av, 2:2, 2:2, 1:1)
@test parentindices(av2) === (view(inds[1], 2:2, 2:2, 1:1),)
end

@testset "isassigned" begin
a = Vector{BigFloat}(undef, 5)
a[2] = 0
Expand Down
Loading