Skip to content

Commit

Permalink
Merge pull request #21331 from pabloferz/pz/stable-bctup
Browse files Browse the repository at this point in the history
Type-stabilize broadcast over tuples and scalars
  • Loading branch information
pabloferz authored Apr 9, 2017
2 parents 965c938 + ae6cdd1 commit 7c1e5af
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 14 deletions.
25 changes: 11 additions & 14 deletions base/broadcast.jl
Original file line number Diff line number Diff line change
Expand Up @@ -311,20 +311,17 @@ end
end
end
@inline broadcast_c(f, ::Type{Any}, a...) = f(a...)
broadcast_c(f, ::Type{Tuple}, As...) =
ntuple(k -> f(_tuplebroadcast_getargs(As, k)...), _tuplebroadcast_reslength(As))
broadcast_c{T}(f, ::Type{Tuple}, ::Type{T}, As...) =
ntuple(k -> f(T, _tuplebroadcast_getargs(As, k)...), _tuplebroadcast_reslength(As))
@inline _tuplebroadcast_getargs(::Tuple{}, k) = ()
@inline _tuplebroadcast_getargs(As, k) =
(_broadcast_getindex(first(As), k), _tuplebroadcast_getargs(tail(As), k)...)
@noinline _tuplebroadcast_reslength(As) =
_tuplebroadcast_maxlength(_tuplebroadcast_length(first(As)), tail(As))
@inline _tuplebroadcast_maxlength(l, As) =
_tuplebroadcast_maxlength(max(l, _tuplebroadcast_length(first(As))), tail(As))
@inline _tuplebroadcast_maxlength(l, ::Tuple{}) = l
@inline _tuplebroadcast_length(t::Tuple) = length(t)
@inline _tuplebroadcast_length(s) = 1
@inline broadcast_c(f, ::Type{Tuple}, A, Bs...) =
tuplebroadcast(f, first_tuple(A, Bs...), A, Bs...)
@inline tuplebroadcast{N}(f, ::NTuple{N,Any}, As...) =
ntuple(k -> f(tuplebroadcast_getargs(As, k)...), Val{N})
@inline tuplebroadcast{N,T}(f, ::NTuple{N,Any}, ::Type{T}, As...) =
ntuple(k -> f(T, tuplebroadcast_getargs(As, k)...), Val{N})
first_tuple(A::Tuple, Bs...) = A
@inline first_tuple(A, Bs...) = first_tuple(Bs...)
tuplebroadcast_getargs(::Tuple{}, k) = ()
@inline tuplebroadcast_getargs(As, k) =
(_broadcast_getindex(first(As), k), tuplebroadcast_getargs(tail(As), k)...)

"""
broadcast(f, As...)
Expand Down
6 changes: 6 additions & 0 deletions test/broadcast.jl
Original file line number Diff line number Diff line change
Expand Up @@ -494,3 +494,9 @@ end
A[1:3,1:3] .= [ones(2,2)]
@test all(A[1:3,1:3] .== [ones(2,2)])
end

# Issue #21291
let t = (0, 1, 2)
o = 1
@test @inferred(broadcast(+, t, o)) == (1, 2, 3)
end

2 comments on commit 7c1e5af

@nanosoldier
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Executing the daily benchmark build, I will reply here when finished:

@nanosoldier runbenchmarks(ALL, isdaily = true)

@nanosoldier
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your benchmark job has completed - possible performance regressions were detected. A full report can be found here. cc @jrevels

Please sign in to comment.