Skip to content

Commit 98b5e84

Browse files
committed
Squash the most egregious perf bugs
1 parent a6cc656 commit 98b5e84

File tree

1 file changed

+14
-30
lines changed

1 file changed

+14
-30
lines changed

base/broadcast.jl

Lines changed: 14 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -266,28 +266,22 @@ of the `Broadcasted` object empty (populated with `nothing`). If they do so, how
266266
they must provide their own `Base.axes(::Broadcasted{Style})` and
267267
`Base.getindex(::Broadcasted{Style}, I::Union{Int,CartesianIndex})` methods as appropriate.
268268
"""
269-
@inline instantiate(bc::Broadcasted{Style}) where {Style} = _instantiate(bc)
270-
271-
_instantiate(bc) = bc
272-
273-
@inline function _instantiate(bc::Broadcasted{Style,Nothing,Nothing}) where {Style}
274-
args = map(instantiate, bc.args)
275-
axes = combine_indices(args...)
276-
@inline _newindexer(arg) = newindexer(axes, arg)
277-
indexing = map(_newindexer, args)
278-
return Broadcasted{Style}(bc.f, args, axes, indexing)
279-
end
280-
281-
@inline function _instantiate(bc::Broadcasted{Style,<:Any,Nothing}) where {Style}
282-
args = map(instantiate, bc.args)
283-
axes = broadcast_shape(bc.axes, combine_indices(args...))
284-
@inline _newindexer(arg) = newindexer(axes, arg)
285-
indexing = map(_newindexer, args)
269+
@inline function instantiate(bc::Broadcasted{Style}) where {Style}
270+
args = instantiate(bc.args)
271+
if bc.axes isa Nothing
272+
axes = combine_indices(args...)
273+
else
274+
axes = broadcast_shape(bc.axes, combine_indices(args...))
275+
end
276+
indexing = map_newindexer(axes, args)
286277
return Broadcasted{Style}(bc.f, args, axes, indexing)
287278
end
288279

289280
instantiate(bc::Broadcasted{<:Union{AbstractArrayStyle{0}, Style{Tuple}}}) = bc
290281

282+
@inline instantiate(args::Tuple) = (instantiate(args[1]), instantiate(Base.tail(args))...)
283+
instantiate(args::Tuple{}) = ()
284+
291285
## Flattening
292286

293287
"""
@@ -516,18 +510,8 @@ end
516510
end
517511

518512
# Equivalent to map(x->newindexer(shape, x), As) (but see #17126)
519-
map_newindexer(shape, ::Tuple{}) = (), ()
520-
@inline function map_newindexer(shape, As)
521-
A1 = As[1]
522-
keeps, Idefaults = map_newindexer(shape, tail(As))
523-
keep, Idefault = newindexer(shape, A1)
524-
(keep, keeps...), (Idefault, Idefaults...)
525-
end
526-
@inline function map_newindexer(shape, A, Bs)
527-
keeps, Idefaults = map_newindexer(shape, Bs)
528-
keep, Idefault = newindexer(shape, A)
529-
(keep, keeps...), (Idefault, Idefaults...)
530-
end
513+
map_newindexer(shape, ::Tuple{}) = ()
514+
@inline map_newindexer(shape, As) = (newindexer(shape, As[1]), map_newindexer(shape, tail(As))...)
531515

532516
@inline function Base.getindex(bc::Broadcasted, I)
533517
@boundscheck checkbounds(bc, I)
@@ -802,7 +786,7 @@ end
802786
# LHS and RHS will always match. This is not true in general, but with the `.op=`
803787
# syntax it's fairly common for an argument to be `===` a source.
804788
broadcast_unalias(dest, src) = dest === src ? src : unalias(dest, src)
805-
map_broadcasted_args(f, bc::Broadcasted) = typeof(bc)(bc.f, map(arg->map_broadcasted_args(f, arg), bc.args), bc.axes, bc.indexing)
789+
@inline map_broadcasted_args(f, bc::Broadcasted) = typeof(bc)(bc.f, map(arg->map_broadcasted_args(f, arg), bc.args), bc.axes, bc.indexing)
806790
map_broadcasted_args(f, arg) = f(arg)
807791

808792
# Specialize this method if all you want to do is specialize on typeof(dest)

0 commit comments

Comments
 (0)