Skip to content

Commit 52006ae

Browse files
authored
Move isassigned within inbounds in copyto_unaliased (#53311)
This matches the other branches where the `isassigned` and ` _unsetindex!` are within `@inbounds` blocks, and this might help with performance after #53305
1 parent 09a27b3 commit 52006ae

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

base/abstractarray.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1100,9 +1100,9 @@ function copyto_unaliased!(deststyle::IndexStyle, dest::AbstractArray, srcstyle:
11001100
iterdest, itersrc = eachindex(dest), eachindex(src)
11011101
if iterdest == itersrc
11021102
# Shared-iterator implementation
1103-
for I in iterdest
1103+
@inbounds for I in iterdest
11041104
if isassigned(src, I)
1105-
@inbounds dest[I] = src[I]
1105+
dest[I] = src[I]
11061106
else
11071107
_unsetindex!(dest, I)
11081108
end

test/abstractarray.jl

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1979,3 +1979,31 @@ end
19791979
test_prechecked_iterate(LinearIndices(()))
19801980
test_prechecked_iterate(Base.SCartesianIndices2{3}(1:3))
19811981
end
1982+
1983+
@testset "IndexStyles in copyto!" begin
1984+
A = rand(3,2)
1985+
B = zeros(size(A))
1986+
colons = ntuple(_->:, ndims(B))
1987+
# Ensure that the AbstractArray methods are hit
1988+
# by using views instead of Arrays
1989+
@testset "IndexLinear - IndexLinear" begin
1990+
B .= 0
1991+
copyto!(view(B, colons...), A)
1992+
@test B == A
1993+
end
1994+
@testset "IndexLinear - IndexCartesian" begin
1995+
B .= 0
1996+
copyto!(view(B, colons...), view(A, axes(A)...))
1997+
@test B == A
1998+
end
1999+
@testset "IndexCartesian - IndexLinear" begin
2000+
B .= 0
2001+
copyto!(view(B, axes(B)...), A)
2002+
@test B == A
2003+
end
2004+
@testset "IndexCartesian - IndexCartesian" begin
2005+
B .= 0
2006+
copyto!(view(B, axes(B)...), view(A, axes(A)...))
2007+
@test B == A
2008+
end
2009+
end

0 commit comments

Comments
 (0)