@@ -442,21 +442,36 @@ Note that `dest` is only used to store the result, and does not supply
442
442
arguments to `f` unless it is also listed in the `As`,
443
443
as in `broadcast!(f, A, A, B)` to perform `A[:] = broadcast(f, A, B)`.
444
444
"""
445
- broadcast! (f, dest, As... ) = broadcast! (f, dest, combine_styles (As... ), As... )
446
- broadcast! (f, dest, :: BroadcastStyle , As... ) = broadcast! (f, dest, nothing , As... )
447
- @inline function broadcast! (f, C, :: Void , A, Bs:: Vararg{Any,N} ) where N
448
- if isa (f, typeof (identity)) && N == 0
449
- if isa (A, Number)
450
- return fill! (C, A)
451
- elseif isa (C, AbstractArray) && isa (A, AbstractArray) && Base. axes (C) == Base. axes (A)
452
- return copy! (C, A)
445
+ @inline broadcast! (f, dest, As... ) = broadcast! (f, dest, combine_styles (As... ), As... )
446
+ @inline broadcast! (f, dest, :: BroadcastStyle , As... ) = broadcast! (f, dest, nothing , As... )
447
+
448
+ # Default behavior (separated out so that it can be called by users who want to extend broadcast!).
449
+ @inline function broadcast! (f, dest, :: Void , As:: Vararg{Any, N} ) where N
450
+ if f isa typeof (identity) && N == 1
451
+ A = As[1 ]
452
+ if A isa AbstractArray && Base. axes (dest) == Base. axes (A)
453
+ return copy! (dest, A)
453
454
end
454
455
end
455
- return _broadcast! (f, C, A, Bs ... )
456
+ return _broadcast! (f, dest, As ... )
456
457
end
457
458
458
- # This indirection allows size-dependent implementations (e.g., see the copying `identity`
459
- # specialization above)
459
+ # Optimization for the all-Scalar case.
460
+ @inline function broadcast! (f, dest, :: Scalar , As:: Vararg{Any, N} ) where N
461
+ if dest isa AbstractArray
462
+ if f isa typeof (identity) && N == 1
463
+ return fill! (dest, As[1 ])
464
+ else
465
+ @inbounds for I in eachindex (dest)
466
+ dest[I] = f (As... )
467
+ end
468
+ return dest
469
+ end
470
+ end
471
+ return _broadcast! (f, dest, As... )
472
+ end
473
+
474
+ # This indirection allows size-dependent implementations.
460
475
@inline function _broadcast! (f, C, A, Bs:: Vararg{Any,N} ) where N
461
476
shape = broadcast_indices (C)
462
477
@boundscheck check_broadcast_indices (shape, A, Bs... )
0 commit comments