From a5e9929d67fe9bdd4b07062eb3686a643515d8a3 Mon Sep 17 00:00:00 2001 From: overlookmotel Date: Sat, 18 Jan 2025 00:01:48 +0000 Subject: [PATCH] perf(lexer): `finish_next` only clear `kind` and `flags` --- crates/oxc_parser/src/lexer/mod.rs | 10 +++++----- crates/oxc_parser/src/lexer/token.rs | 8 ++++++++ 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/crates/oxc_parser/src/lexer/mod.rs b/crates/oxc_parser/src/lexer/mod.rs index 02397fdd2bda7..261117521aebd 100644 --- a/crates/oxc_parser/src/lexer/mod.rs +++ b/crates/oxc_parser/src/lexer/mod.rs @@ -215,12 +215,12 @@ impl<'a> Lexer<'a> { } fn finish_next(&mut self, kind: Kind) -> Token { - self.token.kind = kind; - self.token.end = self.offset(); - debug_assert!(self.token.start <= self.token.end); - let token = self.token; + let mut token = self.token; + token.kind = kind; + token.end = self.offset(); + debug_assert!(token.start <= token.end); self.trivia_builder.handle_token(token); - self.token = Token::default(); + self.token.clear_kind_and_flags(); token } diff --git a/crates/oxc_parser/src/lexer/token.rs b/crates/oxc_parser/src/lexer/token.rs index 63e3a774b6e6c..ff404ab3ccc9c 100644 --- a/crates/oxc_parser/src/lexer/token.rs +++ b/crates/oxc_parser/src/lexer/token.rs @@ -62,6 +62,14 @@ impl Token { debug_assert!(!self.has_separator || self.kind.is_number() || self.kind == Kind::default()); self.has_separator = true; } + + #[inline] + pub fn clear_kind_and_flags(&mut self) { + self.kind = Kind::default(); + self.is_on_new_line = false; + self.escaped = false; + self.has_separator = false; + } } #[cfg(test)]