Skip to content

Comments

refactor(parser): reduce unnecessary backtracking in hot paths#12708

Merged
Boshen merged 5 commits intomainfrom
copilot/fix-9b4c1d3d-d94e-47cb-8974-b735a64a255f
Aug 1, 2025
Merged

refactor(parser): reduce unnecessary backtracking in hot paths#12708
Boshen merged 5 commits intomainfrom
copilot/fix-9b4c1d3d-d94e-47cb-8974-b735a64a255f

Conversation

Copy link
Contributor

Copilot AI commented Aug 1, 2025

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)

  • Replaced checkpoint/rewind with lookahead in is_un_parenthesized_async_arrow_function_worker
  • Eliminates backtracking when checking if async is followed by an arrow function pattern

Optional chaining parsing (284K+ rewinds)

  • Added fast peek check before expensive checkpoint/rewind operations
  • Avoids backtracking when ?. is not followed by a valid optional chain pattern

TypeScript asserts type parsing (288K+ rewinds)

  • Replaced checkpoint/rewind with lookahead for asserts type predicate parsing
  • Eliminates unnecessary backtracking when parsing type references

Performance 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:

  1. Adding fast checks before expensive lookahead operations
  2. Replacing backtracking with forward-only parsing where possible
  3. Using lookahead closures to avoid checkpoint/rewind cycles

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.

@graphite-app
Copy link
Contributor

graphite-app bot commented Aug 1, 2025

How to use the Graphite Merge Queue

Add either label to this PR to merge it via the merge queue:

  • 0-merge - adds this PR to the back of the merge queue
  • hotfix - for urgent hot fixes, skip the queue and merge this PR next

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.

Copilot AI and others added 2 commits August 1, 2025 09:18
- 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 AI changed the title [WIP] Study @oxc-project/oxc/issues/11334, remove unnecessary backtracking from @oxc-project/oxc/files/crates/oxc_parser/src parser: reduce unnecessary backtracking in hot paths Aug 1, 2025
Copilot AI requested a review from Boshen August 1, 2025 09:38
@github-actions github-actions bot added the A-parser Area - Parser label Aug 1, 2025
@Boshen
Copy link
Member

Boshen commented Aug 1, 2025

@copilot run just c if CI fails.

Co-authored-by: Boshen <1430279+Boshen@users.noreply.github.com>
@Boshen
Copy link
Member

Boshen commented Aug 1, 2025

@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>
Copy link
Contributor Author

Copilot AI commented Aug 1, 2025

@copilot please continue.

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:

  1. Arrow function parsing - replaced checkpoint/rewind with lookahead (900K+ rewinds)
  2. Optional chaining - added fast peek check to avoid unnecessary backtracking (284K+ rewinds)
  3. TypeScript asserts parsing - replaced checkpoint/rewind with lookahead (288K+ rewinds)

All parser tests pass and the span assertion issues are resolved. Changes committed in c92396f.

@codspeed-hq
Copy link

codspeed-hq bot commented Aug 1, 2025

CodSpeed Instrumentation Performance Report

Merging #12708 will not alter performance

Comparing copilot/fix-9b4c1d3d-d94e-47cb-8974-b735a64a255f (c92396f) with main (5c9fdd2)

Summary

✅ 34 untouched benchmarks

@Boshen Boshen changed the title parser: reduce unnecessary backtracking in hot paths refactor(parser): reduce unnecessary backtracking in hot paths Aug 1, 2025
@github-actions github-actions bot added the C-cleanup Category - technical debt or refactoring. Solution not expected to change behavior label Aug 1, 2025
@Boshen Boshen marked this pull request as ready for review August 1, 2025 12:58
@Boshen Boshen merged commit 4fc0868 into main Aug 1, 2025
24 of 26 checks passed
@Boshen Boshen deleted the copilot/fix-9b4c1d3d-d94e-47cb-8974-b735a64a255f branch August 1, 2025 12:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-parser Area - Parser C-cleanup Category - technical debt or refactoring. Solution not expected to change behavior

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants