Skip to content

Commit 9de88bb

Browse files
authored
Update broadcast.jl
1 parent d533a6b commit 9de88bb

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

base/broadcast.jl

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -991,11 +991,17 @@ preprocess_args(dest, args::Tuple{}) = ()
991991
# Specialize this method if all you want to do is specialize on typeof(dest)
992992
@inline function copyto!(dest::AbstractArray, bc::Broadcasted{Nothing})
993993
axes(dest) == axes(bc) || throwdm(axes(dest), axes(bc))
994-
# Performance optimization: broadcast!(identity, dest, A) is equivalent to copyto!(dest, A) if indices match
994+
# Performance optimization: broadcast!(identity, dest, A) is equivalent to copyto!(dest, A) if indices match.
995+
# However copyto!(dest, A) is very slow in many cases, implement a faster version here.
995996
if bc.f === identity && bc.args isa Tuple{AbstractArray} # only a single input argument to broadcast!
996997
A = bc.args[1]
997998
if axes(dest) == axes(A)
998-
return copyto!(dest, A)
999+
A′ = broadcast_unalias(dest, A)
1000+
iter = IndexStyle(dest) isa IndexCartesian ? dest : A′
1001+
@inbounds @simd for I in eachindex(iter)
1002+
dest[I] = A′[I]
1003+
end
1004+
return dest
9991005
end
10001006
end
10011007
bc′ = preprocess(dest, bc)

0 commit comments

Comments
 (0)