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

avoid excessive renaming in subtype of intersection result #39623

Merged
merged 2 commits into from
Feb 17, 2021

Conversation

JeffBezanson
Copy link
Member

In computing typeintersect(A, B) <: A, the intersection result would tend to share typevars with A, causing subtyping to do lots of renaming. Luckily it turns out we can avoid that just by doing more renaming in the intersection result beforehand.

fixes #39505, fixes #39394

@JeffBezanson JeffBezanson added types and dispatch Types, subtyping and method dispatch compiler:latency Compiler latency backport 1.6 Change should be backported to release-1.6 labels Feb 11, 2021
@JeffBezanson
Copy link
Member Author

Example query:

using LinearAlgebra,StaticArrays

t1 = Tuple{typeof(vcat), Union{Adjoint{T, s6} where s6<:Union{StaticVector{s1, T} where s1, StaticMatrix{s4, s5, T} where {s4, s5}}, Diagonal{T, s13} where s13<:(StaticVector{s14, T} where s14), Hermitian{T, s10} where s10<:(StaticMatrix{s11, s12, T} where {s11, s12}), LowerTriangular{T, s18} where s18<:(StaticMatrix{s19, s20, T} where {s19, s20}), Symmetric{T, s7} where s7<:(StaticMatrix{s8, s9, T} where {s8, s9}), Transpose{T, s1} where s1<:Union{StaticVector{s1, T} where s1, StaticMatrix{s4, s5, T} where {s4, s5}}, UnitLowerTriangular{T, s24} where s24<:(StaticMatrix{s25, s26, T} where {s25, s26}), UnitUpperTriangular{T, s21} where s21<:(StaticMatrix{s22, s23, T} where {s22, s23}), UpperTriangular{T, s15} where s15<:(StaticMatrix{s16, s17, T} where {s16, s17}), StaticVector{s26, T} where s26, StaticMatrix{s5, s4, T} where {s5, s4}} where T, Union{Adjoint{T, s6} where s6<:Union{StaticVector{s1, T} where s1, StaticMatrix{s4, s5, T} where {s4, s5}}, Diagonal{T, s13} where s13<:(StaticVector{s14, T} where s14), Hermitian{T, s10} where s10<:(StaticMatrix{s11, s12, T} where {s11, s12}), LowerTriangular{T, s18} where s18<:(StaticMatrix{s19, s20, T} where {s19, s20}), Symmetric{T, s7} where s7<:(StaticMatrix{s8, s9, T} where {s8, s9}), Transpose{T, s1} where s1<:Union{StaticVector{s1, T} where s1, StaticMatrix{s4, s5, T} where {s4, s5}}, UnitLowerTriangular{T, s24} where s24<:(StaticMatrix{s25, s26, T} where {s25, s26}), UnitUpperTriangular{T, s21} where s21<:(StaticMatrix{s22, s23, T} where {s22, s23}), UpperTriangular{T, s15} where s15<:(StaticMatrix{s16, s17, T} where {s16, s17}), StaticVector{s26, T} where s26, StaticMatrix{s5, s4, T} where {s5, s4}} where T, Vararg{Union{Adjoint{T, s6} where s6<:Union{StaticVector{s1, T} where s1, StaticMatrix{s4, s5, T} where {s4, s5}}, Diagonal{T, s13} where s13<:(StaticVector{s14, T} where s14), Hermitian{T, s10} where s10<:(StaticMatrix{s11, s12, T} where {s11, s12}), LowerTriangular{T, s18} where s18<:(StaticMatrix{s19, s20, T} where {s19, s20}), Symmetric{T, s7} where s7<:(StaticMatrix{s8, s9, T} where {s8, s9}), Transpose{T, s1} where s1<:Union{StaticVector{s1, T} where s1, StaticMatrix{s4, s5, T} where {s4, s5}}, UnitLowerTriangular{T, s24} where s24<:(StaticMatrix{s25, s26, T} where {s25, s26}), UnitUpperTriangular{T, s21} where s21<:(StaticMatrix{s22, s23, T} where {s22, s23}), UpperTriangular{T, s15} where s15<:(StaticMatrix{s16, s17, T} where {s16, s17}), StaticVector{s26, T} where s26, StaticMatrix{s5, s4, T} where {s5, s4}} where T, N} where N}

t2 = Tuple{typeof(Base.vcat), Vararg{Union{Array{T, 1} where T, Array{T, 2} where T, LinearAlgebra.AbstractTriangular{T, A} where A<:(Array{T, 2} where T) where T, LinearAlgebra.Adjoint{var"#s831", var"#s830"} where var"#s830"<:(Array{T, 1} where T) where var"#s831", LinearAlgebra.Hermitian{T, A} where A<:(Array{T, 2} where T) where T, LinearAlgebra.Symmetric{T, A} where A<:(Array{T, 2} where T) where T, LinearAlgebra.Transpose{var"#s829", var"#s828"} where var"#s828"<:(Array{T, 1} where T) where var"#s829"}}}

isect = typeintersect(t1, t2)

isect <: t1  # 40x faster

@JeffBezanson
Copy link
Member Author

Ok, this got a bit interesting. To fix one test failure, I needed to make switch_union_tuple a bit more accurate by merging type variables when possible (since this change produces different typevars where we used to return the same one). Then there was this strange behavior in 1.6 and prior versions:

julia> U = Vector{Vector{T}} where T;

julia> S = Tuple{U, U};

julia> T = Union{Tuple{T, Vector{T}}, Tuple{T, Vector{Int64}}} where T;

julia> typeintersect(T,S)
Union{}

julia> typeintersect(T, Tuple{Vector{Vector{T}} where T, Vector{Vector{T}} where T})
Tuple{Array{Vector{T}, 1} where T, Array{Vector{T}, 1}} where T

IOW, when the unionall variables inside S are identical, we accidentally get Union{}, which I think is right, but obviously should not depend on the identities of the variables. Now we give a consistent but more conservative answer.

@JeffBezanson JeffBezanson merged commit 093b2a6 into master Feb 17, 2021
@JeffBezanson JeffBezanson deleted the jb/fix39505 branch February 17, 2021 03:08
KristofferC pushed a commit that referenced this pull request Feb 17, 2021
fixes #39505, fixes #39394

do fewer subtype checks in `jl_type_intersection2`

(cherry picked from commit 093b2a6)
@KristofferC KristofferC mentioned this pull request Feb 17, 2021
52 tasks
JeffBezanson added a commit that referenced this pull request Feb 18, 2021
JeffBezanson added a commit that referenced this pull request Feb 18, 2021
JeffBezanson added a commit that referenced this pull request Feb 18, 2021
JeffBezanson added a commit that referenced this pull request Feb 18, 2021
@KristofferC KristofferC removed the backport 1.6 Change should be backported to release-1.6 label Mar 14, 2021
ElOceanografo pushed a commit to ElOceanografo/julia that referenced this pull request May 4, 2021
antoine-levitt pushed a commit to antoine-levitt/julia that referenced this pull request May 9, 2021
JeffBezanson added a commit that referenced this pull request May 26, 2021
JeffBezanson added a commit that referenced this pull request Jun 9, 2021
staticfloat pushed a commit that referenced this pull request Dec 23, 2022
fixes #39505, fixes #39394

do fewer subtype checks in `jl_type_intersection2`

(cherry picked from commit 093b2a6)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler:latency Compiler latency types and dispatch Types, subtyping and method dispatch
Projects
None yet
3 participants