Skip to content

perf(parser): remove lexer lookahead#11332

Closed
camchenry wants to merge 3 commits into05-27-refactor_parser_remove_lexer_lookahead_in_module_parsingfrom
05-27-perf_parser_remove_lexer_lookahead
Closed

perf(parser): remove lexer lookahead#11332
camchenry wants to merge 3 commits into05-27-refactor_parser_remove_lexer_lookahead_in_module_parsingfrom
05-27-perf_parser_remove_lexer_lookahead

Conversation

@camchenry
Copy link
Member

@camchenry camchenry commented May 27, 2025

Removes the lookahead VecDeque in the lexer and all of the bookkeeping code depending on it.

@github-actions github-actions bot added A-parser Area - Parser C-performance Category - Solution not expected to change functional behavior, only performance labels May 27, 2025
Copy link
Member Author

camchenry commented May 27, 2025

Warning

This pull request is not mergeable via GitHub because a downstack PR is open. Once all requirements are satisfied, merge this PR as a stack on Graphite.
Learn more


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.

This stack of pull requests is managed by Graphite. Learn more about stacking.

@codspeed-hq
Copy link

codspeed-hq bot commented May 27, 2025

CodSpeed Instrumentation Performance Report

Merging #11332 will degrade performances by 4.92%

Comparing 05-27-perf_parser_remove_lexer_lookahead (f0a9349) with 05-27-refactor_parser_remove_lexer_lookahead_in_module_parsing (f85a6a4)

Summary

⚡ 4 improvements
❌ 2 regressions
✅ 32 untouched benchmarks

⚠️ Please fix the performance issues or acknowledge them on CodSpeed.

Benchmarks breakdown

Benchmark BASE HEAD Change
lexer[RadixUIAdoptionSection.jsx] 21.9 µs 21 µs +4.33%
lexer[binder.ts] 961.2 µs 929.3 µs +3.43%
lexer[cal.com.tsx] 6 ms 5.8 ms +4.23%
lexer[react.development.js] 397.3 µs 383.8 µs +3.5%
parser[RadixUIAdoptionSection.jsx] 87.1 µs 91.7 µs -4.92%
parser[react.development.js] 1.4 ms 1.4 ms -3.16%

@camchenry camchenry force-pushed the 05-27-refactor_parser_remove_lexer_lookahead_in_module_parsing branch from 7954053 to 1323dbb Compare May 27, 2025 19:56
@camchenry camchenry force-pushed the 05-27-perf_parser_remove_lexer_lookahead branch from 940e394 to 3bf4f58 Compare May 27, 2025 19:57
@camchenry
Copy link
Member Author

Similar to before, this speeds up the lexer (~3-4%) but slows down the parser (~3-4%) which nets out to being slower than before. However, once we work on #11334 and other small perf improvements, we should be able to recover this performance and do even better in the long run.

@camchenry camchenry marked this pull request as ready for review May 27, 2025 20:18
@camchenry camchenry requested review from Boshen and overlookmotel May 27, 2025 20:18
Comment on lines +345 to +351
if !trailing_separator
&& self.at(separator)
&& self.lookahead(|p| {
p.bump_any();
p.at(close)
})
{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest splitting this change out into a separate PR. Presumably these lines is where the parser perf regression is coming from. Removing the VecDeque from the lexer should be a gain in both lexer and parser benchmarks.

Then in this block of code, I think we can refactor to avoid using lookahead at all. I think that should be possible since we're immediately bumping on to next token anyway in the next line self.expect(separator). If we can do that, hopefully it'll remove the perf regression.

But it'd be useful to benchmark that in isolation without the change of removing the VecDeque also in the mix - hence why I think it'd be better in a separate PR.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@overlookmotel got it, I can an intermediate PR to benchmark this specifically. I was planning to write a follow-up PR to improve the performance in this function and hopefully remove the lookahead.

@camchenry camchenry force-pushed the 05-27-perf_parser_remove_lexer_lookahead branch from 3bf4f58 to 186b6f2 Compare May 27, 2025 21:05
@camchenry camchenry force-pushed the 05-27-refactor_parser_remove_lexer_lookahead_in_module_parsing branch from 1323dbb to f85a6a4 Compare May 27, 2025 21:07
@camchenry camchenry force-pushed the 05-27-perf_parser_remove_lexer_lookahead branch from 186b6f2 to 61fdf9f Compare May 27, 2025 21:07
@camchenry
Copy link
Member Author

Okay Graphite seems to think this PR doesn't exist. I'm just going to close and re-create.

@camchenry camchenry closed this May 27, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-parser Area - Parser C-performance Category - Solution not expected to change functional behavior, only performance

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants