Skip to content

perf(parser): speed up simple lookaheads by introducing Lexer::peek_token#11358

Merged
Boshen merged 1 commit intooxc-project:mainfrom
ulrichstark:attempt-at-speeding-up-simple-lookaheads-in-parser
Jul 7, 2025
Merged

perf(parser): speed up simple lookaheads by introducing Lexer::peek_token#11358
Boshen merged 1 commit intooxc-project:mainfrom
ulrichstark:attempt-at-speeding-up-simple-lookaheads-in-parser

Conversation

@ulrichstark
Copy link
Contributor

@ulrichstark ulrichstark commented May 28, 2025

This is my proof of concept function to implement @overlookmotel's tip:

We could also introduce a more slimmed-down (cheaper) version of lookahead when all you need is to get the next 1 or 2 tokens from lexer and then reset its pointer and errors_pos back to where they were before. Current lookahead does quite a lot of work which isn't required in these cases.

source: #11334 (comment)

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

codspeed-hq bot commented May 28, 2025

CodSpeed Instrumentation Performance Report

Merging #11358 will not alter performance

Comparing ulrichstark:attempt-at-speeding-up-simple-lookaheads-in-parser (7495fb4) with main (9736a7f)

Summary

✅ 38 untouched benchmarks

@ulrichstark ulrichstark marked this pull request as ready for review May 28, 2025 20:51
@ulrichstark

This comment was marked as outdated.

@leaysgur
Copy link
Member

&& self.lookahead(|p| {
p.bump_any();
if !p.at(Kind::Using) || p.cur_token().is_on_new_line() {
return false;
}
p.bump_any();
!p.cur_token().is_on_new_line()
})

FYI: Not often, but in a few places this happens.

@Boshen
Copy link
Member

Boshen commented May 29, 2025

This looks very promising! Let's wait until we fix all the code around parser peek before going into this.

@Boshen Boshen marked this pull request as draft May 29, 2025 01:26
@ulrichstark ulrichstark force-pushed the attempt-at-speeding-up-simple-lookaheads-in-parser branch from 1a8ba85 to cf5ff60 Compare May 29, 2025 09:54
@ulrichstark
Copy link
Contributor Author

ulrichstark commented May 29, 2025

Just rebased to include #11356 that was causing conflicts.

@ulrichstark ulrichstark force-pushed the attempt-at-speeding-up-simple-lookaheads-in-parser branch 2 times, most recently from c9f8278 to 0ec970e Compare June 6, 2025 20:55
@ulrichstark ulrichstark changed the title perf(parser): speed up simple lookaheads by introducing Lexer::lookahead_token perf(parser): speed up simple lookaheads by introducing Lexer::peek_token Jun 6, 2025
@ulrichstark
Copy link
Contributor Author

  • Rebased again
  • Changed api from Lexer::lookahead_token taking predicate to Lexer::peek_token returning peeked token
  • Found three more cases to use Lexer::peek_token

@Boshen Boshen self-assigned this Jun 7, 2025
@ulrichstark ulrichstark force-pushed the attempt-at-speeding-up-simple-lookaheads-in-parser branch from f247fc6 to 65f87d8 Compare June 15, 2025 15:04
@Boshen
Copy link
Member

Boshen commented Jun 16, 2025

I'll take a look at the remaining token peeks before this PR.

@Boshen Boshen force-pushed the attempt-at-speeding-up-simple-lookaheads-in-parser branch from 65f87d8 to 7495fb4 Compare July 7, 2025 08:57
@Boshen Boshen marked this pull request as ready for review July 7, 2025 09:24
@Boshen
Copy link
Member

Boshen commented Jul 7, 2025

Can't resist the 2%!

@Boshen Boshen merged commit 349d395 into oxc-project:main Jul 7, 2025
24 checks passed
@ulrichstark ulrichstark deleted the attempt-at-speeding-up-simple-lookaheads-in-parser branch July 7, 2025 09:50
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.

3 participants