Skip to content

Keep semantic context operands in one order in every runtime - #4991

Open
knutwannheden wants to merge 9 commits into
antlr:devfrom
knutwannheden:semantic-context-operand-order
Open

knutwannheden wants to merge 9 commits into
antlr:devfrom
knutwannheden:semantic-context-operand-order

Conversation

@knutwannheden

@knutwannheden knutwannheden commented Oct 29, 2026 •

Copy link
Copy Markdown

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 AND and OR contexts, 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

AND and OR compare and hash their operands in the order they hold them. That order came from:

  • Java, Python and JavaScript: hash-set iteration. Two predicates that share a bucket keep insertion order, so renumbering rules changes which contexts differ.
  • C#, C++ and Dart: insertion order, so any two predicates combined the other way round differ.
  • Go (map iteration) and Swift (a per-instance hash seed): chance, different on every run.

One order everywhere

AND and OR keep 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.

                       dev               this PR
and({0:0}?, {0:2}?)    {0:0}?&&{0:2}?    {0:0}?&&{0:2}?
and({0:2}?, {0:0}?)    {0:2}?&&{0:0}?    {0:0}?&&{0:2}?

Equality, the hash code, toString and 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, where OR.opnds is a public var. 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):

             cached configurations          building the cache
             dev                   this PR  dev             this PR
Java         15,206 / 33,226       15,206   35 / 144 ms     25 / 20 ms
JavaScript   18,374 / 15,206       15,206   406 / 287 ms    65 / 67 ms
Python       20,972 / 17,590       15,206   942 / 685 ms    399 / 407 ms
Swift        21,334 – 36,064       15,206   0.4 / 1.0 s     60 ms
C#           313,184               15,206   16.5 s          25 – 56 ms
C++          313,184               15,206   23.4 s          17 ms
Dart         313,184               15,206   103 – 107 s     21 ms
Go           573,260 – 580,984     15,206   58 – 60 s       19 – 23 ms

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

  • C#, C++ and Dart evaluate the operands in sorted order, not in the order prediction met them. Java, Python and JavaScript never kept that order, and the Java javadoc calls the evaluation "short-circuiting, but unordered".
  • Python, Go and JavaScript keep only the lowest precedence predicate in an AND and the highest in an OR, as Java does.
  • Go's Equals compares the operand counts, so an AND whose operands are a prefix of another's no longer compares equal.

Tests

The runtime test SemPredEvalParser/PredicatesCombinedInEitherOrder counts the predicate evaluations of a prediction that meets two predicates in both orders. On dev it 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.

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
knutwannheden force-pushed the semantic-context-operand-order branch from 4ca356c to 147bb1e Compare November 2, 2026 06:27
@knutwannheden knutwannheden changed the title Compare semantic context operands as a set Keep semantic context operands in one order in every runtime Nov 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

SemanticContext.AND and OR equality depends on the order their operands were combined in

1 participant