Skip to content

Commit 2e9c0f2

Browse files
committed
More incremental perf improvements
1 parent 98b5e84 commit 2e9c0f2

File tree

2 files changed

+29
-25
lines changed

2 files changed

+29
-25
lines changed

base/broadcast.jl

+28-24
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ they must provide their own `Base.axes(::Broadcasted{Style})` and
267267
`Base.getindex(::Broadcasted{Style}, I::Union{Int,CartesianIndex})` methods as appropriate.
268268
"""
269269
@inline function instantiate(bc::Broadcasted{Style}) where {Style}
270-
args = instantiate(bc.args)
270+
args = instantiate_args(bc.args)
271271
if bc.axes isa Nothing
272272
axes = combine_indices(args...)
273273
else
@@ -279,8 +279,8 @@ end
279279

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

282-
@inline instantiate(args::Tuple) = (instantiate(args[1]), instantiate(Base.tail(args))...)
283-
instantiate(args::Tuple{}) = ()
282+
@inline instantiate_args(args::Tuple) = (instantiate(args[1]), instantiate_args(Base.tail(args))...)
283+
instantiate_args(args::Tuple{}) = ()
284284

285285
## Flattening
286286

@@ -526,38 +526,37 @@ Base.@propagate_inbounds Base.getindex(bc::Broadcasted{Nothing}, I) =
526526
Base.@propagate_inbounds _broadcast_getindex(::Base.RefValue{Type{T}}, I) where {T} = T
527527
Base.@propagate_inbounds _broadcast_getindex(A::Tuple{Any}, I) = A[1]
528528
Base.@propagate_inbounds _broadcast_getindex(A::Tuple, I) = A[I[1]]
529-
Base.@propagate_inbounds _broadcast_getindex(A, I) = _broadcast_getindex(combine_styles(A), A, I)
530-
Base.@propagate_inbounds _broadcast_getindex(::AbstractArrayStyle{0}, A, I) = A[]
531-
Base.@propagate_inbounds _broadcast_getindex(::Any, A, I) = A[I]
529+
Base.@propagate_inbounds _broadcast_getindex(A::Ref, I) = A[]
530+
Base.@propagate_inbounds _broadcast_getindex(A, I) = A[I]
532531

533532
# For Broadcasted
534-
Base.@propagate_inbounds _broadcast_getindex(bc::Broadcasted, I) = _broadcast_getindex_bc(bc, I, bc.indexing)
533+
Base.@propagate_inbounds function _broadcast_getindex(bc::Broadcasted, I)
534+
bc.indexing isa Nothing && throw(ArgumentError("a Broadcasted{$Style} wrapper skipped instantiation but has not defined _broadcast_getindex"))
535+
args = _getindex(bc.args, I, bc.indexing)
536+
return _broadcast_getindex_evalf(bc.f, args...)
537+
end
535538

536539
Base.@propagate_inbounds _broadcast_getindex(bc::Broadcasted{<:Union{Style{Tuple}, AbstractArrayStyle{0}}}, I) =
537540
_broadcast_getindex_evalf(bc.f, _getindex(bc.args, I)...)
538541

539542
# Utilities for _broadcast_getindex
540543
# For most styles
541-
Base.@propagate_inbounds _getidx(arg, I, keep_default) = _broadcast_getindex(arg, newindex(I, keep_default...))
542-
Base.@propagate_inbounds _getindex(args::Tuple, I, indexing::Tuple) =
543-
(_getidx(args[1], I, indexing[1]), _getindex(tail(args), I, tail(indexing))...)
544-
Base.@propagate_inbounds _getindex(args::Tuple{Any}, I, indexing::Tuple{Any}) =
545-
(_getidx(args[1], I, indexing[1]),)
546-
Base.@propagate_inbounds _getindex(args::Tuple{}, I, ::Tuple) = ()
547-
Base.@propagate_inbounds _getindex(args::Tuple{}, I, ::Tuple{Any}) = ()
544+
Base.@propagate_inbounds _getindex(args::Tuple, I, indexing) =
545+
(_broadcast_getindex(args[1], newindex(I, indexing[1][1], indexing[1][2])), _getindex(tail(args), I, tail(indexing))...)
546+
Base.@propagate_inbounds _getindex(args::Tuple{Any}, I, indexing) =
547+
(_broadcast_getindex(args[1], newindex(I, indexing[1][1], indexing[1][2])),)
548+
Base.@propagate_inbounds _getindex(args::Tuple{Any,Any}, I, indexing) =
549+
(_broadcast_getindex(args[1], newindex(I, indexing[1][1], indexing[1][2])),
550+
_broadcast_getindex(args[2], newindex(I, indexing[2][1], indexing[2][2])))
551+
Base.@propagate_inbounds _getindex(args::Tuple{}, I, indexing) = ()
548552
# For styles skipping reindexers
549553
Base.@propagate_inbounds _getindex(args::Tuple, I) = (_broadcast_getindex(args[1], I), _getindex(tail(args), I)...)
550554
Base.@propagate_inbounds _getindex(args::Tuple{Any}, I) = (_broadcast_getindex(args[1], I),)
555+
Base.@propagate_inbounds _getindex(args::Tuple{Any,Any}, I) =
556+
(_broadcast_getindex(args[1], I), _broadcast_getindex(args[2], I))
551557
Base.@propagate_inbounds _getindex(args::Tuple{}, I) = ()
552558

553559

554-
Base.@propagate_inbounds function _broadcast_getindex_bc(bc::Broadcasted{Style}, I, indexing) where {Style}
555-
args = _getindex(bc.args, I, indexing)
556-
return _broadcast_getindex_evalf(bc.f, args...)
557-
end
558-
_broadcast_getindex_bc(bc::Broadcasted{Style}, I, ::Nothing) where {Style} =
559-
throw(ArgumentError("a Broadcasted{$Style} wrapper skipped instantiation but has not defined _broadcast_getindex"))
560-
561560
"""
562561
broadcastable(x)
563562
@@ -786,8 +785,13 @@ end
786785
# LHS and RHS will always match. This is not true in general, but with the `.op=`
787786
# syntax it's fairly common for an argument to be `===` a source.
788787
broadcast_unalias(dest, src) = dest === src ? src : unalias(dest, src)
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)
790-
map_broadcasted_args(f, arg) = f(arg)
788+
789+
@inline map_broadcast_unalias(dest, bc::Broadcasted) = typeof(bc)(bc.f, map_broadcast_unalias_args(dest, bc.args), bc.axes, bc.indexing)
790+
map_broadcast_unalias(dest, x) = broadcast_unalias(dest, x)
791+
792+
@inline map_broadcast_unalias_args(dest, args::Tuple) = (map_broadcast_unalias(dest, args[1]), map_broadcast_unalias_args(dest, tail(args))...)
793+
map_broadcast_unalias_args(dest, args::Tuple{Any}) = (map_broadcast_unalias(dest, args[1]),)
794+
map_broadcast_unalias_args(dest, args::Tuple{}) = ()
791795

792796
# Specialize this method if all you want to do is specialize on typeof(dest)
793797
@inline function copyto!(dest::AbstractArray, bc::Broadcasted{Nothing})
@@ -799,7 +803,7 @@ map_broadcasted_args(f, arg) = f(arg)
799803
return copyto!(dest, A)
800804
end
801805
end
802-
bc′ = map_broadcasted_args(arg->broadcast_unalias(dest, arg), bc)
806+
bc′ = map_broadcast_unalias(dest, bc)
803807
@simd for I in CartesianIndices(axes(bc′))
804808
@inbounds dest[I] = bc′[I]
805809
end

base/compiler/optimize.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ function isinlineable(m::Method, src::CodeInfo, mod::Module, params::Params, bon
263263
return inlineable
264264
end
265265

266-
const enable_new_optimizer = RefValue(false)
266+
const enable_new_optimizer = RefValue(true)
267267

268268
# converge the optimization work
269269
function optimize(me::InferenceState)

0 commit comments

Comments
 (0)