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

Normalize !=, <, and > in guards #113

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/binding.jl
Original file line number Diff line number Diff line change
Expand Up @@ -496,6 +496,15 @@ function shred_where_clause(
assigned::ImmutableDict{Symbol, Symbol})::BoundPattern
if @capture(guard, !g_)
return shred_where_clause(g, !inverted, location, binder, assigned)
# Normalize comparisons to ==, <=, and >= to get better deuplicate guard detection
# We could also normalize >= to <= by swapping the arguments, but that may have unexpected
# behavior if the arguments are not side-effect free.
elseif @capture(guard, g1_ != g2_)
return shred_where_clause(Expr(:call, :(==), g1, g2), !inverted, location, binder, assigned)
elseif @capture(guard, g1_ > g2_)
return shred_where_clause(Expr(:call, :(<=), g1, g2), !inverted, location, binder, assigned)
elseif @capture(guard, g1_ < g2_)
return shred_where_clause(Expr(:call, :(>=), g1, g2), !inverted, location, binder, assigned)
elseif @capture(guard, g1_ && g2_) || @capture(guard, g1_ || g2_)
left = shred_where_clause(g1, inverted, location, binder, assigned)
right = shred_where_clause(g2, inverted, location, binder, assigned)
Expand Down
44 changes: 44 additions & 0 deletions test/rematch2.jl
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,50 @@ end
end) == 7
end

@testset "test for decision automaton optimizations 5" begin
with_neq = (Match.@match_count_nodes some_value begin
Foo(x, y) where x == y => 1
Foo(x, y) where x != y => 2
end)
without_neq = (Match.@match_count_nodes some_value begin
Foo(x, y) where x == y => 1
Foo(x, y) => 2
end)
without_eq = (Match.@match_count_nodes some_value begin
Foo(x, y) where x != y => 2
Foo(x, y) => 1
end)
# All three should generate the equivalent automatons
@test with_neq == 8
@test without_eq == 8
@test without_eq == 8
end

@testset "test for decision automaton optimizations 6" begin
count_leq = (Match.@match_count_nodes some_value begin
Foo(x, y) where x <= y => 1
Foo(x, y) where x > y => 2
end)
count_geq = (Match.@match_count_nodes some_value begin
Foo(x, y) where x >= y => 1
Foo(x, y) where x < y => 2
end)
count_neq = (Match.@match_count_nodes some_value begin
Foo(x, y) where x != y => 1
Foo(x, y) where x == y => 2
end)
# All three should generate the similar automatons, avoiding duplicate comparisons
@test count_leq == 8
@test count_geq == 8
@test count_neq == 8
# If the optimization does not apply, the automaton should be larger
count_other = (Match.@match_count_nodes some_value begin
Foo(x, y) where x == y => 1
Foo(x, y) where x >= y => 2
end)
@test count_other == 10
end

@testset "test for sharing where clause conjuncts" begin
# Node 1 TEST «input_value» isa Main.Rematch2Tests.Foo ELSE: Node 18 («label_2»)
# Node 2 FETCH «input_value.x» := «input_value».x
Expand Down
Loading