Skip to content

Commit

Permalink
Much faster broadcast over combinations of tuples and broadcast scalars.
Browse files Browse the repository at this point in the history
  • Loading branch information
Sacha0 committed Feb 27, 2017
1 parent f2b03d4 commit 60514a8
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions base/broadcast.jl
Original file line number Diff line number Diff line change
Expand Up @@ -297,11 +297,6 @@ _broadcast_eltype(f, A, Bs...) = Base._return_type(f, eltypestuple(A, Bs...))
end
return broadcast_t(f, Any, shape, iter, A, Bs...)
end
function broadcast_c(f, ::Type{Tuple}, As...)
shape = broadcast_indices(As...)
n = length(shape[1])
return ntuple(k->f((_broadcast_getindex(A, k) for A in As)...), n)
end
@inline function broadcast_c(f, ::Type{Nullable}, a...)
nonnull = all(hasvalue, a)
S = _broadcast_eltype(f, a...)
Expand All @@ -316,6 +311,17 @@ end
end
end
@inline broadcast_c(f, ::Type{Any}, a...) = f(a...)
broadcast_c(f, ::Type{Tuple}, As...) =
ntuple(k -> f(_tbcgetargs(As, k)...), _tbcreslength(As))
broadcast_c{T}(f, ::Type{Tuple}, ::Type{T}, As...) =
ntuple(k -> f(T, _tbcgetargs(As, k)...), _tbcreslength(As))
@inline _tbcgetargs(As, k) = (_broadcast_getindex(first(As), k), _tbcgetargs(tail(As), k)...)
@inline _tbcgetargs(::Tuple{}, k) = ()
_tbcreslength(As) = (Base.@_noinline_meta; _tbcmaxlength(_tbclength(first(As)), tail(As)))
@inline _tbcmaxlength(l, As) = _tbcmaxlength(max(l, _tbclength(first(As))), tail(As))
@inline _tbcmaxlength(l, ::Tuple{}) = l
@inline _tbclength(t::Tuple) = length(t)
@inline _tbclength(s) = 1

"""
broadcast(f, As...)
Expand Down

0 comments on commit 60514a8

Please sign in to comment.