refactor(parser): reduce unnecessary backtracking in hot paths#12708
refactor(parser): reduce unnecessary backtracking in hot paths#12708
Conversation
How to use the Graphite Merge QueueAdd either label to this PR to merge it via the merge queue:
You must have a Graphite account in order to use the merge queue. Sign up using this link. An organization admin has enabled the Graphite Merge Queue in this repository. Please do not merge from GitHub as this will restart CI on PRs being processed by the merge queue. |
- Replace checkpoint/rewind with lookahead in arrow function parsing - Optimize optional chaining parsing to avoid backtracking - Remove backtracking from TypeScript asserts type parsing - Optimize modifier parsing to use lookahead instead of try_parse - Fix for-let statement parsing to use lookahead - Remove unused helper functions This addresses the high-frequency backtracking identified in issue #11334, targeting the most impactful areas based on rewind count analysis. Co-authored-by: Boshen <1430279+Boshen@users.noreply.github.com>
- Optimize TypeScript keyword parsing with fast dot check - Replace checkpoint/rewind with lookahead in export default parsing - Add fast checks before expensive lookahead operations Further reduces backtracking in parser hot paths identified in issue #11334. Co-authored-by: Boshen <1430279+Boshen@users.noreply.github.com>
|
@copilot run |
Co-authored-by: Boshen <1430279+Boshen@users.noreply.github.com>
|
@copilot please continue. |
- Replace checkpoint/rewind with lookahead in arrow function parsing - Optimize optional chaining parsing with fast peek check - Remove backtracking from TypeScript asserts type parsing These changes target high-frequency rewind locations while preserving correct parser behavior and avoiding span assertion failures. Co-authored-by: Boshen <1430279+Boshen@users.noreply.github.com>
I've continued the work by implementing safe parser optimizations to reduce backtracking. The previous implementation had span assertion failures due to incorrect token consumption patterns. I've now implemented three key optimizations targeting the highest-impact areas:
All parser tests pass and the span assertion issues are resolved. Changes committed in c92396f. |
CodSpeed Instrumentation Performance ReportMerging #12708 will not alter performanceComparing Summary
|
This PR addresses issue #11334 by reducing unnecessary backtracking in the parser's hot paths to improve performance. Based on the rewind count analysis from the issue, this targets the most impactful areas where checkpoint/rewind patterns can be replaced with more efficient lookahead operations.
Key optimizations:
Arrow function parsing (highest impact - 900K+ rewinds)
is_un_parenthesized_async_arrow_function_workerasyncis followed by an arrow function patternOptional chaining parsing (284K+ rewinds)
?.is not followed by a valid optional chain patternTypeScript asserts type parsing (288K+ rewinds)
assertstype predicate parsingPerformance impact:
These changes target the functions with the highest rewind counts identified in the issue analysis:
js/arrow.rs:55(900,742 rewinds)js/expression.rs:722(284,197 rewinds)ts/types.rs:411(288,916 rewinds)The optimizations follow the suggested patterns from the issue:
All parser tests pass and functionality is preserved while reducing unnecessary backtracking operations.
💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.