Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve type stability of lt(p::Perm, a::Integer, b::Integer) #46732

Merged
merged 1 commit into from
Sep 13, 2022

Conversation

ranocha
Copy link
Member

@ranocha ranocha commented Sep 13, 2022

This fixes a few hundred invalidations when loading Static/jl/ArrayInterface.jl. It's based on the following code.

julia> using Pkg; Pkg.activate(temp=true); Pkg.add("ArrayInterface")

julia> using SnoopCompileCore; invalidations = @snoopr(using ArrayInterface); using SnoopCompile

julia> trees = invalidation_trees(invalidations);

julia> ftrees = filtermod(Base.Sort, trees; recursive=true)
...
 inserting &(x::Static.True, y::Bool) in Static at ~/.julia/packages/Static/sVI3g/src/Static.jl:411 invalidated:
   mt_backedges: 1: signature Tuple{typeof(&), Any, Bool} triggered MethodInstance for Base.Order.lt(::Base.Order.Perm{<:Base.Order.ForwardOrdering, <:Union{AbstractVector{Union{Missing, Float32}}, AbstractVector{Union{Missing, Float64}}, AbstractVector{Missing}, AbstractVector{Float32}, AbstractVector{Float64}}}, ::Int64, ::Int64) (9 children)
                 2: signature Tuple{typeof(&), Any, Bool} triggered MethodInstance for Base.Order.lt(::Base.Order.Perm{_A, Vector{Base.StackTraces.StackFrame}} where _A<:Base.Order.Ordering, ::Int64, ::Int64) (13 children)
                 3: signature Tuple{typeof(&), Any, Bool} triggered MethodInstance for Base.Order.lt(::Base.Order.Perm{_A, Vector{Int64}} where _A<:Base.Order.Ordering, ::Int64, ::Int64) (18 children)
                 4: signature Tuple{typeof(&), Any, Bool} triggered MethodInstance for Base.Order.lt(::Base.Order.Perm{_A, Vector{Float64}} where _A<:Base.Order.Ordering, ::Int64, ::Int64) (28 children)
                 5: signature Tuple{typeof(&), Any, Bool} triggered MethodInstance for Base.Order.lt(::Base.Order.Perm{_A, Vector{Tuple{Float64, Float64}}} where _A<:Base.Order.Ordering, ::Int64, ::Int64) (49 children)
                 6: signature Tuple{typeof(&), Any, Bool} triggered MethodInstance for Base.Order.lt(::Base.Order.Perm{<:Base.Order.ReverseOrdering{Base.Order.ForwardOrdering}, <:Union{AbstractVector{Union{Missing, Float32}}, AbstractVector{Union{Missing, Float64}}, AbstractVector{Missing}, AbstractVector{Float32}, AbstractVector{Float64}}}, ::Int64, ::Int64) (23 children)

julia> ftrees = filtermod(Base.Order, trees; recursive=true)
2-element Vector{SnoopCompile.MethodInvalidations}:
 inserting !(::Static.False) in Static at ~/.julia/packages/Static/sVI3g/src/Static.jl:427 invalidated:
   mt_backedges: 1: signature Tuple{typeof(!), Any} triggered MethodInstance for Base.Order.lt(::Base.Order.Perm{<:Base.Order.ReverseOrdering{Base.Order.ForwardOrdering}, <:Union{AbstractVector{Union{Missing, Float32}}, AbstractVector{Union{Missing, Float64}}, AbstractVector{Missing}, AbstractVector{Float32}, AbstractVector{Float64}}}, ::Int64, ::Int64) (0 children)
                 2: signature Tuple{typeof(!), Any} triggered MethodInstance for Base.Order.lt(::Base.Order.Perm{<:Base.Order.ForwardOrdering, <:Union{AbstractVector{Union{Missing, Float32}}, AbstractVector{Union{Missing, Float64}}, AbstractVector{Missing}, AbstractVector{Float32}, AbstractVector{Float64}}}, ::Int64, ::Int64) (0 children)
                 3: signature Tuple{typeof(!), Any} triggered MethodInstance for Base.Order.lt(::Base.Order.Perm{_A, Vector{Float64}} where _A<:Base.Order.Ordering, ::Int64, ::Int64) (0 children)
                 4: signature Tuple{typeof(!), Any} triggered MethodInstance for Base.Order.lt(::Base.Order.Perm{_A, Vector{Tuple{Float64, Float64}}} where _A<:Base.Order.Ordering, ::Int64, ::Int64) (0 children)
                 5: signature Tuple{typeof(!), Any} triggered MethodInstance for Base.Order.lt(::Base.Order.Perm{_A, Vector{Int64}} where _A<:Base.Order.Ordering, ::Int64, ::Int64) (0 children)
                 6: signature Tuple{typeof(!), Any} triggered MethodInstance for Base.Order.lt(::Base.Order.Perm{_A, Vector{Base.StackTraces.StackFrame}} where _A<:Base.Order.Ordering, ::Int64, ::Int64) (0 children)

 inserting &(x::Static.True, y::Bool) in Static at ~/.julia/packages/Static/sVI3g/src/Static.jl:411 invalidated:
   mt_backedges: 1: signature Tuple{typeof(&), Any, Bool} triggered MethodInstance for Base.Order.lt(::Base.Order.Perm{<:Base.Order.ForwardOrdering, <:Union{AbstractVector{Union{Missing, Float32}}, AbstractVector{Union{Missing, Float64}}, AbstractVector{Missing}, AbstractVector{Float32}, AbstractVector{Float64}}}, ::Int64, ::Int64) (9 children)
                 2: signature Tuple{typeof(&), Any, Bool} triggered MethodInstance for Base.Order.lt(::Base.Order.Perm{_A, Vector{Base.StackTraces.StackFrame}} where _A<:Base.Order.Ordering, ::Int64, ::Int64) (13 children)
                 3: signature Tuple{typeof(&), Any, Bool} triggered MethodInstance for Base.Order.lt(::Base.Order.Perm{_A, Vector{Int64}} where _A<:Base.Order.Ordering, ::Int64, ::Int64) (18 children)
                 4: signature Tuple{typeof(&), Any, Bool} triggered MethodInstance for Base.Order.lt(::Base.Order.Perm{_A, Vector{Float64}} where _A<:Base.Order.Ordering, ::Int64, ::Int64) (28 children)
                 5: signature Tuple{typeof(&), Any, Bool} triggered MethodInstance for Base.Order.lt(::Base.Order.Perm{_A, Vector{Tuple{Float64, Float64}}} where _A<:Base.Order.Ordering, ::Int64, ::Int64) (49 children)
                 6: signature Tuple{typeof(&), Any, Bool} triggered MethodInstance for Base.Order.lt(::Base.Order.Perm{<:Base.Order.ReverseOrdering{Base.Order.ForwardOrdering}, <:Union{AbstractVector{Union{Missing, Float32}}, AbstractVector{Union{Missing, Float64}}, AbstractVector{Missing}, AbstractVector{Float32}, AbstractVector{Float64}}}, ::Int64, ::Int64) (166 children)

With this PR, all of these invalidations are gone (but some others are still left, of course).

We cannot fix the type instabilities at a higher level since sort! (or its kwarg handler) avoids specialization on the keyword arguments by and lt given as functions.

This fixes a few hundred invalidations when loading Static/jl/ArrayInterface.jl.
@ranocha ranocha added compiler:latency Compiler latency backport 1.8 Change should be backported to release-1.8 domain:sorting Put things in order labels Sep 13, 2022
@timholy timholy merged commit 70bfa3f into JuliaLang:master Sep 13, 2022
@ranocha ranocha deleted the hr/fix_invalidations_lt branch September 14, 2022 05:36
@ranocha ranocha mentioned this pull request Sep 14, 2022
28 tasks
ranocha added a commit to ranocha/julia that referenced this pull request Sep 14, 2022
…aLang#46732)

This fixes a few hundred invalidations when loading Static/jl/ArrayInterface.jl.
KristofferC pushed a commit that referenced this pull request Sep 16, 2022
This fixes a few hundred invalidations when loading Static/jl/ArrayInterface.jl.

(cherry picked from commit 70bfa3f)
@KristofferC KristofferC removed the backport 1.8 Change should be backported to release-1.8 label Sep 30, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler:latency Compiler latency domain:sorting Put things in order
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants