Fix non-deterministic code generation caused by HashSet iteration order#232
Open
jensdietrich wants to merge 1 commit intoantlr:masterfrom
Open
Fix non-deterministic code generation caused by HashSet iteration order#232jensdietrich wants to merge 1 commit intoantlr:masterfrom
jensdietrich wants to merge 1 commit intoantlr:masterfrom
Conversation
Replaces HashSet with LinkedHashSet in two places where rewrite-rule element references are collected during grammar analysis. Because HashSet iteration order depends on System.identityHashCode(), which varies between JVM runs, the order in which rewrite stream variables were emitted into generated parsers was non-deterministic. Affected locations: - DefineGrammarItemsWalker.g: rewriteRefsDeep and rewriteRefsShallow on GrammarAST nodes (3 allocations) - Grammar.java: the labels set returned by getLabels() LinkedHashSet preserves insertion order (i.e. the order in which tokens and rule references appear in the grammar source), making the generated output stable across runs. Reported downstream in antlr/stringtemplate4#325, where STParser.java was observed to produce a different SHA-256 hash on every build.
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.
Summary
Replaces
HashSetwithLinkedHashSetin two places where rewrite-ruleelement references are collected during grammar analysis. Because
HashSetiteration order depends on
System.identityHashCode(), which varies betweenJVM runs, the order in which rewrite stream variables were emitted into
generated parsers was non-deterministic.
Affected locations:
DefineGrammarItemsWalker.g—rewriteRefsDeepandrewriteRefsShallowon
GrammarASTnodes (3 allocations)Grammar.java— thelabelsset returned bygetLabels()LinkedHashSetpreserves insertion order (i.e. the order in which tokens andrule references appear in the grammar source), making the generated output
stable across runs.
Motivation: reproducible builds
This bug causes generated parser sources to differ between builds. Concrete
symptoms visible in the generated Java code:
Non-deterministic generated sources undermine
reproducible builds, which are an
important defence in software supply chain security. The Java compiler itself
treats non-determinism in generated code as a bug — see for example
JDK-8264306 and
JDK-8295024.
The issue was reported downstream in
antlr/stringtemplate4#325,
where
STParser.javawas observed to produce a different SHA-256 hash onevery build.
Performance impact
The performance impact of this change is expected to be negligible.
LinkedHashSetoffers the same O(1) amortised complexity asHashSetforadd(),contains(), andremove()— the only difference is a smallconstant overhead per insertion to maintain the doubly-linked list that
preserves insertion order. This is corroborated by benchmarks in
Performance: TreeSet vs HashSet vs LinkedHashSet,
which show that
LinkedHashSetandHashSetperform virtually identically,while
TreeSet(O(log n)) is measurably slower. The sets affected by thischange are small — they hold only the token and rule references appearing in a
single grammar rewrite rule — so even the constant overhead is inconsequential
in practice.
Remaining known limitation
After this fix, generated files still differ between runs in exactly one
place: the wall-clock timestamp ANTLR3 writes into the header comment of
every generated file:
// $ANTLR 3.5.3 org/stringtemplate/v4/compiler/STParser.g 2026-04-14 08:54:49This does not affect compiled artefacts (
.classfiles / JARs): thetimestamp lives in a comment which is stripped by the Java compiler, so
binary outputs remain reproducible. Eliminating the timestamp entirely would
be a separate follow-up change.