Skip to content

Commit

Permalink
perf(parser): fast path for ASCII when checking char after numeric li…
Browse files Browse the repository at this point in the history
…teral (#4577)

Make a fast path for ASCII characters when lexer checks for invalid chars after a numeric literal. Also shorten the check for ASCII chars.
  • Loading branch information
overlookmotel committed Jul 31, 2024
1 parent 56ae615 commit ae1d38f
Showing 1 changed file with 23 additions and 6 deletions.
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

0 comments on commit ae1d38f

Please sign in to comment.