Skip to content

[Perf][SwiftParser] Record lookahead ranges only for a parse that hands them on - #3447

Draft
rintaro wants to merge 1 commit into
swiftlang:mainfrom
rintaro:perf-parser-16-lookahead-ranges
Draft

rintaro wants to merge 1 commit into
swiftlang:mainfrom
rintaro:perf-parser-16-lookahead-ranges

Conversation

@rintaro

@rintaro rintaro commented Sep 18, 2026

Copy link
Copy Markdown
Member

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.

Parser now takes collectsLookaheadRanges. The parseIncrementally entry points, which return the ranges, pass true; the entry points that return only a tree leave it false. A parse given a parseTransition records regardless of what the caller asked, because what it hands on is what the next reparse decides reuse from, and IncrementalParseLookup will not reuse a node it has no range for — so a caller driving Parser directly 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.lookaheadRanges is public internal(set), and a caller driving Parser directly for a full parse now finds it empty unless it passes collectsLookaheadRanges: true. An incremental parse is not affected.

…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

rintaro commented Sep 18, 2026

Copy link
Copy Markdown
Member Author

@swift-ci Please test

@rintaro
rintaro marked this pull request as draft September 22, 2026 05:42
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.

1 participant