Skip to content

Commit db690e0

Browse files
committed
Mitagate some of the performance issues with non-type-stable...
broadcasting by preprocessing the arguments to potentially wrap them with indexing helpers.
1 parent a8a2608 commit db690e0

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

base/broadcast.jl

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -742,19 +742,20 @@ const NonleafHandlingStyles = Union{DefaultArrayStyle,ArrayConflict}
742742
# When ElType is not concrete, use narrowing. Use the first output
743743
# value to determine the starting output eltype; copyto_nonleaf!
744744
# will widen `dest` as needed to accommodate later values.
745-
iter = CartesianIndices(axes(bc))
745+
bc′ = preprocess(nothing, bc)
746+
iter = CartesianIndices(axes(bc′))
746747
state = start(iter)
747748
if done(iter, state)
748749
# if empty, take the ElType at face value
749-
return broadcast_similar(Style(), ElType, axes(bc), bc)
750+
return broadcast_similar(Style(), ElType, axes(bc), bc)
750751
end
751752
# Initialize using the first value
752753
I, state = next(iter, state)
753-
@inbounds val = bc[I]
754-
dest = broadcast_similar(Style(), typeof(val), axes(bc), bc)
754+
@inbounds val = bc[I]
755+
dest = broadcast_similar(Style(), typeof(val), axes(bc), bc)
755756
@inbounds dest[I] = val
756757
# Now handle the remaining values
757-
return copyto_nonleaf!(dest, bc, iter, state, 1)
758+
return copyto_nonleaf!(dest, bc, iter, state, 1)
758759
end
759760

760761
## general `copyto!` methods
@@ -787,6 +788,7 @@ end
787788
# LHS and RHS will always match. This is not true in general, but with the `.op=`
788789
# syntax it's fairly common for an argument to be `===` a source.
789790
broadcast_unalias(dest, src) = dest === src ? src : unalias(dest, src)
791+
broadcast_unalias(::Nothing, src) = src
790792

791793
# Preprocessing a `Broadcasted` does two things:
792794
# * unaliases any arguments from `dest`

0 commit comments

Comments
 (0)