Skip to content

Commit

Permalink
Optimization for non-union invariant parameter.
Browse files Browse the repository at this point in the history
  • Loading branch information
N5N3 committed Jan 30, 2023
1 parent 0d1c461 commit f9b6264
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
35 changes: 34 additions & 1 deletion src/subtype.c
Original file line number Diff line number Diff line change
Expand Up @@ -1437,11 +1437,44 @@ static int _local_forall_exists_subtype(jl_value_t *x, jl_value_t *y, jl_stenv_t
return sub;
}

static int obviously_nounion(jl_value_t *x, jl_stenv_t *e, jl_typeenv_t *log) JL_NOTSAFEPOINT
{
if (x == NULL || x == (jl_value_t*)jl_any_type || x == jl_bottom_type)
return 1;
if (jl_is_unionall(x))
return obviously_nounion(((jl_unionall_t *)x)->body, e, log);
if (jl_is_datatype(x)) {
jl_datatype_t *xd = (jl_datatype_t *)x;
for (int i = 0; i < jl_nparams(xd); i++) {
jl_value_t *param = jl_tparam(xd, i);
if (jl_is_vararg(param))
param = jl_unwrap_vararg(param);
if (!obviously_nounion(param, e, log))
return 0;
}
return 1;
}
if (!jl_is_typevar(x))
return 0;
jl_typeenv_t *t = log;
while (t != NULL) {
if (x == (jl_value_t *)t->var)
return 0;
t = t->prev;
}
jl_typeenv_t newlog = { (jl_tvar_t*)x, NULL, log };
jl_varbinding_t *xb = lookup(e, (jl_tvar_t *)x);
return obviously_nounion(xb ? xb->lb : ((jl_tvar_t *)x)->lb, e, &newlog) &&
obviously_nounion(xb ? xb->ub : ((jl_tvar_t *)x)->ub, e, &newlog);
}

static int local_forall_exists_subtype(jl_value_t *x, jl_value_t *y, jl_stenv_t *e, int param, int limit_slow)
{
int16_t oldRmore = e->Runions.more;
int sub;
if (pick_union_decision(e, 1) == 0) {
if (obviously_nounion(y, e, NULL))
sub = _local_forall_exists_subtype(x, y, e, param);
else if (pick_union_decision(e, 1) == 0) {
jl_saved_unionstate_t oldRunions; push_unionstate(&oldRunions, &e->Runions);
e->Lunions.used = e->Runions.used = 0;
e->Lunions.depth = e->Runions.depth = 0;
Expand Down
4 changes: 4 additions & 0 deletions test/subtype.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2436,3 +2436,7 @@ let A = Tuple{Type{T}, T} where T,
C = Tuple{Type{MyType47877{W, V} where V<:Union{MyAbstract47877{W}, Base.BitInteger}}, MyType47877{W, V} where V<:Union{MyAbstract47877{W}, Base.BitInteger}} where W<:Base.BitInteger
@test typeintersect(B, A) == C
end

let a = (isodd(i) ? Pair{Char, String} : Pair{String, String} for i in 1:2000)
@test Tuple{Type{Pair{Union{Char, String}, String}}, a...} <: Tuple{Type{Pair{K, V}}, Vararg{Pair{A, B} where B where A}} where V where K
end

0 comments on commit f9b6264

Please sign in to comment.