Skip to content

Commit

Permalink
fix #20671, slowdown loading SIUnits
Browse files Browse the repository at this point in the history
This adds a fast path to intersection that helps when we have a type
with lots of parameters that just need to be matched up 1-to-1 with
parameters of another type.

Also fixes a bug in intersection uncovered by this test case.
  • Loading branch information
JeffBezanson committed Mar 27, 2017
1 parent f65b8ab commit 70a5624
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 5 deletions.
22 changes: 17 additions & 5 deletions src/subtype.c
Original file line number Diff line number Diff line change
Expand Up @@ -1148,8 +1148,12 @@ static jl_value_t *intersect_var(jl_tvar_t *b, jl_value_t *a, jl_stenv_t *e, int
jl_value_t *root=NULL; jl_savedenv_t se;
if (param == 2) {
jl_value_t *ub = R ? intersect_ufirst(a, bb->ub, e, d) : intersect_ufirst(bb->ub, a, e, d);
JL_GC_PUSH1(&ub);
if (!subtype_in_env(bb->lb, a, e)) {
JL_GC_PUSH2(&ub, &root);
save_env(e, &root, &se);
int issub = subtype_in_env(bb->lb, ub, e);
restore_env(e, root, &se);
free(se.buf);
if (!issub) {
JL_GC_POP();
return jl_bottom_type;
}
Expand Down Expand Up @@ -1219,6 +1223,9 @@ static jl_value_t *intersect_var(jl_tvar_t *b, jl_value_t *a, jl_stenv_t *e, int
}
return ub;
}
else if (bb->ub == bb->lb) {
return ub;
}
root = NULL;
JL_GC_PUSH2(&root, &ub);
save_env(e, &root, &se);
Expand Down Expand Up @@ -1326,9 +1333,14 @@ static jl_value_t *finish_unionall(jl_value_t *res, jl_varbinding_t *vb, jl_sten
}
}
else {
varval = root = (jl_value_t*)jl_new_typevar(vb->var->name, vb->lb, vb->ub);
res = jl_instantiate_unionall((jl_unionall_t*)res, root);
res = jl_new_struct(jl_unionall_type, (jl_tvar_t*)root, res);
if (vb->lb != vb->var->lb || vb->ub != vb->var->ub) {
varval = root = (jl_value_t*)jl_new_typevar(vb->var->name, vb->lb, vb->ub);
res = jl_instantiate_unionall((jl_unionall_t*)res, root);
res = jl_new_struct(jl_unionall_type, (jl_tvar_t*)root, res);
}
else {
varval = (jl_value_t*)vb->var;
}
}
}

Expand Down
12 changes: 12 additions & 0 deletions test/subtype.jl
Original file line number Diff line number Diff line change
Expand Up @@ -622,6 +622,10 @@ abstract type AbstractTriangular{T,S<:AbstractMatrix} <: AbstractMatrix{T} end
struct UpperTriangular{T,S<:AbstractMatrix} <: AbstractTriangular{T,S} end
struct UnitUpperTriangular{T,S<:AbstractMatrix} <: AbstractTriangular{T,S} end

immutable SIQ20671{T<:Number,m,kg,s,A,K,mol,cd,rad,sr} <: Number
val::T
end

function test_intersection()
@testintersect(Vector{Float64}, Vector{Union{Float64,Float32}}, Bottom)

Expand Down Expand Up @@ -887,6 +891,14 @@ function test_intersection()
@testintersect(Pair{L,Tuple{L,Pair{L,HL}}} where {L,HL},
Pair{R,Tuple{Pair{R,HR},R}} where {R,HR},
Bottom) # X == Pair{X,...} is not satisfiable

# issue #20671 --- this just took too long
@testintersect(Tuple{Type{SIQ20671{T, mT, kgT, sT, AT, KT, molT, cdT, radT, srT}},
SIQ20671{S, mS, kgS, sS, AS, KS, molS, cdS, radS, srS}} where {T, mT, kgT, sT, AT, KT, molT, cdT, radT, srT,
S, mS, kgS, sS, AS, KS, molS, cdS, radS, srS},
Tuple{Type{T}, T} where T,
Tuple{Type{SIQ20671{T,mS,kgS,sS,AS,KS,molS,cdS,radS,srS}},
SIQ20671{T,mS,kgS,sS,AS,KS,molS,cdS,radS,srS}} where {T,mS,kgS,sS,AS,KS,molS,cdS,radS,srS})
end

function test_intersection_properties()
Expand Down

0 comments on commit 70a5624

Please sign in to comment.