Repository navigation
perf(parser): append sorted intervals without scanning the set - #1302
Merged
Merged
Conversation
The first query in a process deserializes the lexer's ATN, which builds large Unicode character sets one interval at a time. IntervalSet::add walked the whole list for each one, which was about half of the first query's compile time. An interval that goes into an empty set, or starts after the last one without touching it, is now appended directly; the result is the same. This is a local patch on the vendored ANTLR 4.13.1. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
adsharma
reviewed
Oct 19, 2026
Interval::adjacent() computes b + 1 and a - 1, and a - 1 overflows when the last interval starts at SSIZE_MIN. The fast path now compares against the last interval's end directly: it already knows that end is below the new start, so b + 1 cannot overflow, and the other half of adjacent() can never hold there. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The vendored ANTLR 4.13.1 runtime carries four local changes: the interval append fast path, and three older build fixes (a missing <chrono> include for Windows, a non-virtual ParseTree::operator== for GCC 13 warnings, and deprecation pragmas in Vocabulary.cpp). They were recorded nowhere, so a runtime upgrade would have dropped them. The port fetches the 4.13.1 tag, applies patches/, and keeps Ladybug's CMakeLists.txt; `./pkg antlr4_runtime update` reproduces the vendored tree byte for byte. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.
This work was produced with the help of language models.
Fixes #1301. Part of #1290.
The first query in a process deserialized the lexer's ATN, which builds large Unicode character sets one interval at a time, and
IntervalSet::addwalked the whole interval list for each one: about half of the first query's 9.39 ms compile time forRETURN 1(query-summary time, median of 8 processes). The 0.18 ms in #1290 is a later query.Fix. An interval that goes into an empty set, or starts after the last interval without touching it, is appended directly; everything else takes the existing loop, so the set comes out the same. Upstream 4.13.2 and the dev branch have the same loop, so the change is a patch in
third_party/ports/antlr4_runtime, next to three older local changes to the vendored 4.13.1 runtime that were recorded nowhere.Performance. First query compile 9.39 → 5.16 ms, 77.7M → 42.0M instructions; later queries unchanged. Mallocs are unchanged too: about 68.4K for the first query (within run-to-run noise) and 601 per later
RETURN 1at one thread. The rest of the first compile is ANTLR filling its prediction cache on first use.Tests.
AntlrIntervalSetTest.AddKeepsCanonicalForm: random and sorted inserts plus touching intervals, against brute-force membership and the canonical sorted, disjoint, non-adjacent form.EofMergesOnlyWithTouchingRange: EOF (-1) merges with a range starting at 0 and stays separate from one starting at 1.EmptyIntervalAndRangeLimits: an empty interval is ignored, and appends and merges nearSSIZE_MAXgive the same set. All three fail if the fast path appends an interval that touches the last one.AppendAfterRangeMinimum: a set starting atSSIZE_MINtakes the fast path without signed overflow (fails under UBSan if the check usesadjacent()). Full ctest on this branch: 3104/3104.Independent of #1297 and #1300; with all three the first query compiles in 5.1 ms and executes in 0.33 ms (query-summary median of 8 processes).