From bb33bcce3559c5beab8f0aac7a7fb5b9dce4ea99 Mon Sep 17 00:00:00 2001 From: overlookmotel <557937+overlookmotel@users.noreply.github.com> Date: Wed, 31 Jul 2024 00:29:00 +0000 Subject: [PATCH] perf(parser): speed up lexing non-decimal numbers (#4571) Inline `Lexer::read_non_decimal` to reduce branching. --- crates/oxc_parser/src/lexer/kind.rs | 1 + crates/oxc_parser/src/lexer/numeric.rs | 4 ++++ 2 files changed, 5 insertions(+) diff --git a/crates/oxc_parser/src/lexer/kind.rs b/crates/oxc_parser/src/lexer/kind.rs index df8aa3d0950e7..ecaa14248789c 100644 --- a/crates/oxc_parser/src/lexer/kind.rs +++ b/crates/oxc_parser/src/lexer/kind.rs @@ -206,6 +206,7 @@ impl Kind { ) } + #[inline] // Inline into `read_non_decimal` - see comment there as to why pub fn matches_number_byte(self, b: u8) -> bool { match self { Decimal => b.is_ascii_digit(), diff --git a/crates/oxc_parser/src/lexer/numeric.rs b/crates/oxc_parser/src/lexer/numeric.rs index c9090e426a775..1655306e3f3e0 100644 --- a/crates/oxc_parser/src/lexer/numeric.rs +++ b/crates/oxc_parser/src/lexer/numeric.rs @@ -39,6 +39,10 @@ impl<'a> Lexer<'a> { self.check_after_numeric_literal(kind) } + // Inline into the 3 calls from `read_zero` so that value of `kind` is known + // and `kind.matches_number_byte` can be statically reduced to just the match arm + // that applies for this specific kind. `matches_number_byte` is also marked `#[inline]`. + #[inline] fn read_non_decimal(&mut self, kind: Kind) -> Kind { self.consume_char();