Conversation
…m on Every parse registered each node it might later reuse in a dictionary keyed by node identity, which is a hash insertion per node. Only an incremental reparse reads those ranges, and it reads them from the result of the parse before it, so a parse whose ranges nobody will receive is doing the work for nothing — which is every `Parser.parse(source:)`. `Parser` now takes `collectsLookaheadRanges`. The `parseIncrementally` entry points, which return the ranges to the caller, pass true; the ones that return only a tree leave it false. A parse given a `parseTransition` records regardless of what the caller asked, because the ranges it hands on are what the reparse after it decides reuse from, and a node with no recorded range is not reused at all — so a caller driving `Parser` directly would otherwise lose reuse for everything that parse produced, silently and with no diagnostic. `testLookaheadRangesAreRecordedForAnIncrementalParseWithoutAsking` pins that: it fails with the gate on the argument alone, reporting that nothing was reusable. A parse that does collect reserves the table against the byte count first, since a node is registered roughly every 90 bytes of source and the table would otherwise grow and rehash through the parse. Reserving is skipped for an incremental parse, whose table is replaced by the one it inherits.
rintaro
requested review from
hamishknight,
hborla and
nkcsgexi
as code owners
September 18, 2026 18:11
Member
Author
|
@swift-ci Please test |
rintaro
marked this pull request as draft
September 22, 2026 05:42
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.
Every parse used to register each node that might later be reused in a dictionary keyed by node identity, which is a hash insertion per node. In
Parser.parse(source:), nothing reads the dictionary and it was wasted.Parsernow takescollectsLookaheadRanges. TheparseIncrementallyentry points, which return the ranges, pass true; the entry points that return only a tree leave it false. A parse given aparseTransitionrecords regardless of what the caller asked, because what it hands on is what the next reparse decides reuse from, andIncrementalParseLookupwill not reuse a node it has no range for — so a caller drivingParserdirectly would otherwise lose reuse for everything that parse produced, with a correct tree and no diagnostic to show for it.A parse that does collect reserves the table against the source's byte count first, since a node is registered roughly every 90 bytes and the table would otherwise grow and rehash through the parse; an incremental parse skips the reserve, because the table it inherits replaces it.
Measured against
main(1f995c7), 2.5-2.9% instruction reduction, but 0.1% for deliberately corrupted sample source, which spends its parse recovering rather than registering nodes.One behavior change:
Parser.lookaheadRangesispublic internal(set), and a caller drivingParserdirectly for a full parse now finds it empty unless it passescollectsLookaheadRanges: true. An incremental parse is not affected.