From 3fa87ff6a6c9101ca2bd864be80643ce43a576cf Mon Sep 17 00:00:00 2001 From: Boshen Date: Thu, 23 Jan 2025 18:19:03 +0800 Subject: [PATCH] perf(lexer): peak 2 bytes after `!` (#8662) `!==` and `!=` is very frequent. image Picked this up while profiling. --- crates/oxc_parser/src/lexer/byte_handlers.rs | 26 +++++++++++++++----- 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/crates/oxc_parser/src/lexer/byte_handlers.rs b/crates/oxc_parser/src/lexer/byte_handlers.rs index 1be5ae6fe..4299328be 100644 --- a/crates/oxc_parser/src/lexer/byte_handlers.rs +++ b/crates/oxc_parser/src/lexer/byte_handlers.rs @@ -209,14 +209,28 @@ ascii_byte_handler!(LIN(lexer) { // ! ascii_byte_handler!(EXL(lexer) { lexer.consume_char(); - if lexer.next_ascii_byte_eq(b'=') { - if lexer.next_ascii_byte_eq(b'=') { - Kind::Neq2 - } else { - Kind::Neq + if let Some(next_2_bytes) = lexer.peek_2_bytes() { + match next_2_bytes[0] { + b'=' => { + if next_2_bytes[1] == b'=' { + lexer.consume_2_chars(); + Kind::Neq2 + } else { + lexer.consume_char(); + Kind::Neq + } + } + _ => Kind::Bang } } else { - Kind::Bang + // At EOF, or only 1 byte left + match lexer.peek_byte() { + Some(b'=') => { + lexer.consume_char(); + Kind::Neq + } + _ => Kind::Bang + } } });