Skip to content

Commit

Permalink
Use CartesianIndices(Rsrc) as the shared iterator. (#45289)
Browse files Browse the repository at this point in the history
There's no performance change, if the `indices`s of `Rdest` and `Rsrc` are all `NTuple{N,<:AbstractUnitRange}`.

(cherry picked from commit a91be39)
  • Loading branch information
N5N3 authored and KristofferC committed May 23, 2022
1 parent 1054327 commit 427daf1
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 deletions.
12 changes: 7 additions & 5 deletions base/multidimensional.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1076,16 +1076,18 @@ function copyto!(dest::AbstractArray{T1,N}, Rdest::CartesianIndices{N},
checkbounds(src, first(Rsrc))
checkbounds(src, last(Rsrc))
src′ = unalias(dest, src)
ΔI = first(Rdest) - first(Rsrc)
CRdest = CartesianIndices(Rdest)
CRsrc = CartesianIndices(Rsrc)
ΔI = first(CRdest) - first(CRsrc)
if @generated
quote
@nloops $N i (n->Rsrc.indices[n]) begin
@inbounds @nref($N,dest,n->i_n+ΔI[n]) = @nref($N,src′,i)
@nloops $N i (n->CRsrc.indices[n]) begin
@inbounds @nref($N,dest,n->Rdest.indices[n][i_n+ΔI[n]]) = @nref($N,src,n->Rsrc.indices[n][i_n])
end
end
else
for I in Rsrc
@inbounds dest[I + ΔI] = src′[I]
for I in CRsrc
@inbounds dest[Rdest[I + ΔI]] = src′[Rsrc[I]]
end
end
dest
Expand Down
13 changes: 8 additions & 5 deletions test/copy.jl
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,14 @@ end
@test B == A
end
let A = reshape(1:6, 3, 2), B = zeros(8,8)
RA = CartesianIndices(axes(A))
copyto!(B, CartesianIndices((5:7,2:3)), A, RA)
@test B[5:7,2:3] == A
B[5:7,2:3] .= 0
@test all(x->x==0, B)
RBs = Any[(5:7,2:3), (3:2:7,1:2:3), (6:-1:4,2:-1:1)]
RAs = Any[axes(A), reverse.(axes(A))]
for RB in RBs, RA in RAs
copyto!(B, CartesianIndices(RB), A, CartesianIndices(RA))
@test B[RB...] == A[RA...]
B[RB...] .= 0
@test all(iszero, B)
end
end
end

Expand Down

0 comments on commit 427daf1

Please sign in to comment.