Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf(parser): fast path for ASCII when checking char after numeric literal #4577

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 23 additions & 6 deletions crates/oxc_parser/src/lexer/numeric.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use oxc_syntax::identifier::is_identifier_start;
use oxc_syntax::identifier::{is_identifier_part_ascii, is_identifier_start};

use super::{Kind, Lexer, Span};
use crate::diagnostics;
Expand Down Expand Up @@ -189,12 +189,29 @@ impl<'a> Lexer<'a> {
}

fn check_after_numeric_literal(&mut self, kind: Kind) -> Kind {
let offset = self.offset();
// The SourceCharacter immediately following a NumericLiteral must not be an IdentifierStart or DecimalDigit.
let c = self.peek_char();
if c.is_none() || c.is_some_and(|ch| !ch.is_ascii_digit() && !is_identifier_start(ch)) {
return kind;
// The SourceCharacter immediately following a NumericLiteral must not be
// an IdentifierStart or DecimalDigit.
// Use a fast path for common case where next char is ASCII.
// NB: `!is_identifier_part_ascii(b as char)` is equivalent to
// `!b.is_ascii_digit() && !is_identifier_start_ascii(b as char)`
match self.peek_byte() {
Some(b) if b.is_ascii() => {
if !is_identifier_part_ascii(b as char) {
return kind;
}
}
Some(_) => {
// Unicode
let c = self.peek_char().unwrap();
if !is_identifier_start(c) {
return kind;
}
}
None => return kind,
}

// Invalid next char
let offset = self.offset();
self.consume_char();
while let Some(c) = self.peek_char() {
if is_identifier_start(c) {
Expand Down