Skip to content

Commit 015cc63

Browse files
authored
Merge pull request #20817 from Sacha0/tuplebc
fast broadcast over combinations of tuples and scalars
2 parents 0827820 + 453da2c commit 015cc63

File tree

1 file changed

+14
-5
lines changed

1 file changed

+14
-5
lines changed

base/broadcast.jl

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -297,11 +297,6 @@ _broadcast_eltype(f, A, Bs...) = Base._return_type(f, eltypestuple(A, Bs...))
297297
end
298298
return broadcast_t(f, Any, shape, iter, A, Bs...)
299299
end
300-
function broadcast_c(f, ::Type{Tuple}, As...)
301-
shape = broadcast_indices(As...)
302-
n = length(shape[1])
303-
return ntuple(k->f((_broadcast_getindex(A, k) for A in As)...), n)
304-
end
305300
@inline function broadcast_c(f, ::Type{Nullable}, a...)
306301
nonnull = all(hasvalue, a)
307302
S = _broadcast_eltype(f, a...)
@@ -316,6 +311,20 @@ end
316311
end
317312
end
318313
@inline broadcast_c(f, ::Type{Any}, a...) = f(a...)
314+
broadcast_c(f, ::Type{Tuple}, As...) =
315+
ntuple(k -> f(_tuplebroadcast_getargs(As, k)...), _tuplebroadcast_reslength(As))
316+
broadcast_c{T}(f, ::Type{Tuple}, ::Type{T}, As...) =
317+
ntuple(k -> f(T, _tuplebroadcast_getargs(As, k)...), _tuplebroadcast_reslength(As))
318+
@inline _tuplebroadcast_getargs(::Tuple{}, k) = ()
319+
@inline _tuplebroadcast_getargs(As, k) =
320+
(_broadcast_getindex(first(As), k), _tuplebroadcast_getargs(tail(As), k)...)
321+
@noinline _tuplebroadcast_reslength(As) =
322+
_tuplebroadcast_maxlength(_tuplebroadcast_length(first(As)), tail(As))
323+
@inline _tuplebroadcast_maxlength(l, As) =
324+
_tuplebroadcast_maxlength(max(l, _tuplebroadcast_length(first(As))), tail(As))
325+
@inline _tuplebroadcast_maxlength(l, ::Tuple{}) = l
326+
@inline _tuplebroadcast_length(t::Tuple) = length(t)
327+
@inline _tuplebroadcast_length(s) = 1
319328

320329
"""
321330
broadcast(f, As...)

0 commit comments

Comments
 (0)