Repository navigation
Keep semantic context operands in one order in every runtime - #4991
Open
knutwannheden wants to merge 9 commits into
Open
knutwannheden wants to merge 9 commits into
knutwannheden wants to merge 9 commits into
Conversation
SemanticContext.AND and OR collect their operands in a HashSet, and
compare and hash them in the set's iteration order. Two operands that
share a bucket iterate in insertion order, so the same predicates
combined the other way round make an unequal context:
and({0:0}?, {0:2}?) = {0:0}?&&{0:2}?
and({0:2}?, {0:0}?) = {0:2}?&&{0:0}?
The prediction cache then holds configurations that differ only in that
order, and evaluates every copy's predicates.
AND and OR now merge their operands, each side already sorted, into one
order: predicates by rule and predicate index, then precedence
predicates by precedence, then nested operators by their operands.
Equality and the hash code use that order, and the hash code is
computed once.
Fixes antlr#4990.
Signed-off-by: Knut Wannheden <knut.wannheden@gmail.com>
AND and OR collected their operands in a set and compared them in the set's iteration order, so the same predicates combined the other way round could make an unequal context. They merge their sorted operands into the order the Java runtime uses, and compute the hash once. The Python runtime also kept every precedence predicate where Java keeps only the lowest for AND and the highest for OR. It keeps one, as Java does. Signed-off-by: Knut Wannheden <knut.wannheden@gmail.com>
NewAND and NewOR collected their operands in a JStore, whose Values() ranges over a map, so the operand order changed from one construction to the next. They merge their sorted operands into the order the Java runtime uses, and compute the hash once. Equals compares the operand counts, so an AND whose operands are a prefix of another's no longer compares equal. The Go runtime also kept every precedence predicate where Java keeps only the lowest for AND and the highest for OR. It keeps one, as Java does. Signed-off-by: Knut Wannheden <knut.wannheden@gmail.com>
AND and OR collected their operands in a HashSet, which enumerates in insertion order, so the same predicates combined the other way round made an unequal context. They merge their sorted operands into the order the Java runtime uses, and compute the hash once. Signed-off-by: Knut Wannheden <knut.wannheden@gmail.com>
AND and OR collected their operands in a set literal, which keeps insertion order, so the same predicates combined the other way round made an unequal context. They merge their sorted operands into the order the Java runtime uses, and compute the hash once. Signed-off-by: Knut Wannheden <knut.wannheden@gmail.com>
AND and OR kept their operands in insertion order, so the same predicates combined the other way round made an unequal context. They merge their sorted operands into the order the Java runtime uses. The classes gain no members, so their layout is unchanged. Signed-off-by: Knut Wannheden <knut.wannheden@gmail.com>
AND and OR collected their operands in a HashSet and compared them in its iteration order, so the same predicates combined the other way round could make an unequal context. They merge their sorted operands into the order the Java runtime uses, and compute the hash once. The JavaScript runtime also kept every precedence predicate where Java keeps only the lowest for AND and the highest for OR. It keeps one, as Java does. Signed-off-by: Knut Wannheden <knut.wannheden@gmail.com>
AND and OR collected their operands in a Set, which seeds its hashing per instance, so the operand order could change from one construction to the next. They merge their sorted operands into the order the Java runtime uses. OR's opnds is a public var that callers can reassign, so OR does not store its hash code, and AND computes its hash the same way. Signed-off-by: Knut Wannheden <knut.wannheden@gmail.com>
SLL prediction can combine the same two predicates in either order on its way to one DFA state. The runtimes kept the two orders as distinct semantic contexts, so the state's predicate evaluated the AND of them twice. The new runtime test pins a single evaluation in every target. Without the runtime commits before it, the test fails in the Java, Python, JavaScript, Go, C#, Dart and C++ runtimes. Swift chose the operand order at random per construction, so it fails there only some of the time. Signed-off-by: Knut Wannheden <knut.wannheden@gmail.com>
knutwannheden
force-pushed
the
semantic-context-operand-order
branch
from
November 2, 2026 06:27
4ca356c to
147bb1e
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #4990.
On
dev, the same grammar and input build a different prediction cache in every ANTLR target. For a generated grammar of about 15 rules, Java caches 15,206 DFA configurations and Go about 575,000, a different number on every run. Building that cache takes 35 ms in Java and 58 s in Go.SLL prediction combines the predicates it passes into
ANDandORcontexts, and a configuration's context is part of its identity in the DFA cache. Two paths that reach one state past the same predicates in a different order have to make equal contexts. Otherwise the cache keeps both configurations and evaluates both sets of predicates.Each target ordered the operands its own way
ANDandORcompare and hash their operands in the order they hold them. That order came from:One order everywhere
ANDandORkeep their operands sorted: predicates by rule and predicate index, then precedence predicates by precedence, then nested operators operand by operand. Each constructor merges the two operand lists, both already sorted, and drops duplicates.Equality, the hash code,
toStringand evaluation all follow that order. The constructors compute the hash code once, except in C++, where a new member would change the class layout, and in Swift, whereOR.opndsis a publicvar. The public API and the serialized ATN are unchanged. There is one commit per runtime, then one for the runtime test.The grammar and a harness for each runtime are in https://github.com/knutwannheden/antlr-semantic-context-order. With this PR every target builds the same cache from the same input (6 block kinds, 200 programs, two rule numberings, median of 3 runs):
This PR first compared the operands as a set, in Java only. That also removes the duplicates, but building the cache took up to 2.5× longer than with sorted operands, and every target would still evaluate the operands in its own order.
Behaviour that changes
ANDand the highest in anOR, as Java does.Equalscompares the operand counts, so anANDwhose operands are a prefix of another's no longer compares equal.Tests
The runtime test
SemPredEvalParser/PredicatesCombinedInEitherOrdercounts the predicate evaluations of a prediction that meets two predicates in both orders. Ondevit fails in Java, Python, JavaScript, Go, C#, Dart and C++, and in Swift some of the time. It passes in every target here. Unit tests in six runtimes pin the order, the precedence reduction and nested operators.Not in this PR
PHP lives in antlr/antlr-php-runtime and keeps insertion order too, so the runtime test skips it until a PR there lands.
This change was written with the help of an AI coding assistant. I have reviewed and tested it, and sign off on it under the DCO.