Skip to content

Commit 427daf1

Browse files
N5N3KristofferC
authored andcommitted
Use CartesianIndices(Rsrc) as the shared iterator. (#45289)
There's no performance change, if the `indices`s of `Rdest` and `Rsrc` are all `NTuple{N,<:AbstractUnitRange}`. (cherry picked from commit a91be39)
1 parent 1054327 commit 427daf1

File tree

2 files changed

+15
-10
lines changed

2 files changed

+15
-10
lines changed

base/multidimensional.jl

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1076,16 +1076,18 @@ function copyto!(dest::AbstractArray{T1,N}, Rdest::CartesianIndices{N},
10761076
checkbounds(src, first(Rsrc))
10771077
checkbounds(src, last(Rsrc))
10781078
src′ = unalias(dest, src)
1079-
ΔI = first(Rdest) - first(Rsrc)
1079+
CRdest = CartesianIndices(Rdest)
1080+
CRsrc = CartesianIndices(Rsrc)
1081+
ΔI = first(CRdest) - first(CRsrc)
10801082
if @generated
10811083
quote
1082-
@nloops $N i (n->Rsrc.indices[n]) begin
1083-
@inbounds @nref($N,dest,n->i_n+ΔI[n]) = @nref($N,src′,i)
1084+
@nloops $N i (n->CRsrc.indices[n]) begin
1085+
@inbounds @nref($N,dest,n->Rdest.indices[n][i_n+ΔI[n]]) = @nref($N,src,n->Rsrc.indices[n][i_n])
10841086
end
10851087
end
10861088
else
1087-
for I in Rsrc
1088-
@inbounds dest[I + ΔI] = src′[I]
1089+
for I in CRsrc
1090+
@inbounds dest[Rdest[I + ΔI]] = src′[Rsrc[I]]
10891091
end
10901092
end
10911093
dest

test/copy.jl

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,14 @@ end
5858
@test B == A
5959
end
6060
let A = reshape(1:6, 3, 2), B = zeros(8,8)
61-
RA = CartesianIndices(axes(A))
62-
copyto!(B, CartesianIndices((5:7,2:3)), A, RA)
63-
@test B[5:7,2:3] == A
64-
B[5:7,2:3] .= 0
65-
@test all(x->x==0, B)
61+
RBs = Any[(5:7,2:3), (3:2:7,1:2:3), (6:-1:4,2:-1:1)]
62+
RAs = Any[axes(A), reverse.(axes(A))]
63+
for RB in RBs, RA in RAs
64+
copyto!(B, CartesianIndices(RB), A, CartesianIndices(RA))
65+
@test B[RB...] == A[RA...]
66+
B[RB...] .= 0
67+
@test all(iszero, B)
68+
end
6669
end
6770
end
6871

0 commit comments

Comments
 (0)