Skip to content

Commit

Permalink
exists_equal tuning.
Browse files Browse the repository at this point in the history
Make sure we don't forget the bound in `env`.
And this makes `subtype` slightly faster by reducing heavy re-subtyping.
But of course we will consume more Runion stack with this change.
  • Loading branch information
N5N3 committed Jan 28, 2023
1 parent 90d1d1e commit 48fc5ff
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
17 changes: 14 additions & 3 deletions src/subtype.c
Original file line number Diff line number Diff line change
Expand Up @@ -1461,6 +1461,7 @@ static int forall_exists_equal(jl_value_t *x, jl_value_t *y, jl_stenv_t *e)
(is_definite_length_tuple_type(x) && is_indefinite_length_tuple_type(y)))
return 0;

jl_saved_unionstate_t oldLunions; push_unionstate(&oldLunions, &e->Lunions);

if ((jl_is_uniontype(x) && jl_is_uniontype(y))) {
// For 2 unions, first try a more efficient greedy algorithm that compares the unions
Expand All @@ -1472,7 +1473,6 @@ static int forall_exists_equal(jl_value_t *x, jl_value_t *y, jl_stenv_t *e)
// }

// Optimization
jl_saved_unionstate_t oldLunions; push_unionstate(&oldLunions, &e->Lunions);
int16_t old2Rmore = e->Runions.more;
if (pick_union_decision(e, 1) == 0) {
int16_t oldRmore = e->Runions.more;
Expand Down Expand Up @@ -1506,7 +1506,6 @@ static int forall_exists_equal(jl_value_t *x, jl_value_t *y, jl_stenv_t *e)
if ((!jl_has_free_typevars(x) || !jl_has_free_typevars(y)))
sub = local_forall_exists_subtype(x, y, e, 2);
else {
jl_saved_unionstate_t oldLunions; push_unionstate(&oldLunions, &e->Lunions);
e->Lunions.used = 0;
while (1) {
e->Lunions.more = 0;
Expand All @@ -1518,7 +1517,19 @@ static int forall_exists_equal(jl_value_t *x, jl_value_t *y, jl_stenv_t *e)
pop_unionstate(&e->Lunions, &oldLunions);
}

return sub && subtype(y, x, e, 0);
if (sub) {
e->Lunions.used = 0;
while (1) {
e->Lunions.more = 0;
e->Lunions.depth = 0;
sub = subtype(y, x, e, 0);
if (!sub || !next_union_state(e, 0))
break;
}
pop_unionstate(&e->Lunions, &oldLunions);
}

return sub;
}

static int exists_subtype(jl_value_t *x, jl_value_t *y, jl_stenv_t *e, jl_value_t *saved, jl_savedenv_t *se, int param)
Expand Down
8 changes: 4 additions & 4 deletions test/subtype.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1480,10 +1480,10 @@ f24521(::Type{T}, ::Type{T}) where {T} = T
@test f24521(Tuple{Vararg{Int64}}, Tuple{Vararg{Int64,N}} where N) == Tuple{Vararg{Int64,N}} where N

# issue #26654
@test_broken !(Ref{Union{Int64, Ref{Number}}} <: Ref{Union{Ref{T}, T}} where T)
@test_broken !(Ref{Union{Int64, Val{Number}}} <: Ref{Union{Val{T}, T}} where T)
@test_broken !(Ref{Union{Ref{Number}, Int64}} <: Ref{Union{Ref{T}, T}} where T)
@test_broken !(Ref{Union{Val{Number}, Int64}} <: Ref{Union{Val{T}, T}} where T)
@test !(Ref{Union{Int64, Ref{Number}}} <: Ref{Union{Ref{T}, T}} where T)
@test !(Ref{Union{Int64, Val{Number}}} <: Ref{Union{Val{T}, T}} where T)
@test !(Ref{Union{Ref{Number}, Int64}} <: Ref{Union{Ref{T}, T}} where T)
@test !(Ref{Union{Val{Number}, Int64}} <: Ref{Union{Val{T}, T}} where T)

# issue #26180
@test !(Ref{Union{Ref{Int64}, Ref{Number}}} <: Ref{Ref{T}} where T)
Expand Down

0 comments on commit 48fc5ff

Please sign in to comment.