Skip to content

Commit 5d4cd87

Browse files
committed
Fast path for inferred cases
1 parent 52479fc commit 5d4cd87

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

base/broadcast.jl

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,9 @@ end
241241

242242
# broadcast methods that dispatch on the type found by inference
243243
function _broadcast_t(f, ::Type{Any}, shape, iter, As...)
244+
if isempty(iter)
245+
return similar(Array{Any}, shape)
246+
end
244247
nargs = length(As)
245248
keeps, Idefaults = map_newindexer(shape, As)
246249
st = start(iter)
@@ -272,14 +275,16 @@ ziptype(A, B) = Zip2{Tuple{eltype(A)},Tuple{eltype(B)}}
272275

273276
# broadcast methods that dispatch on the type of the final container
274277
@inline function broadcast_c(f, ::Type{Array}, As...)
275-
S = _default_eltype(Generator{ziptype(As...),ftype(f, As...)})
278+
T = _default_eltype(Generator{ziptype(As...),ftype(f, As...)})
276279
shape = broadcast_indices(As...)
277280
iter = CartesianRange(shape)
281+
if isleaftype(T)
282+
return _broadcast_t(f, T, shape, iter, As...)
283+
end
278284
if isempty(iter)
279-
return similar(Array{S}, shape)
285+
return similar(Array{T}, shape)
280286
end
281-
T = isleaftype(S) ? S : Any
282-
return _broadcast_t(f, T, shape, iter, As...)
287+
return _broadcast_t(f, Any, shape, iter, As...)
283288
end
284289
function broadcast_c(f, ::Type{Tuple}, As...)
285290
shape = broadcast_indices(As...)

0 commit comments

Comments
 (0)