From 45740bd907a30c1ce08b01d22300968fb96e64b0 Mon Sep 17 00:00:00 2001 From: Cam McHenry Date: Tue, 25 Nov 2025 21:56:21 -0500 Subject: [PATCH] perf(parser): remove bounds check for getting strings from lexer --- crates/oxc_parser/src/lexer/string.rs | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/crates/oxc_parser/src/lexer/string.rs b/crates/oxc_parser/src/lexer/string.rs index 9bfacdb040d61..f4c5fa865ba79 100644 --- a/crates/oxc_parser/src/lexer/string.rs +++ b/crates/oxc_parser/src/lexer/string.rs @@ -255,15 +255,26 @@ impl<'a> Lexer<'a> { return self.escaped_strings[&token.start()]; } - let raw = &self.source.whole()[token.start() as usize..token.end() as usize]; - match token.kind() { + let start = token.start() as usize; + let end = token.end() as usize; + let (start, end) = match token.kind() { Kind::Str => { - &raw[1..raw.len() - 1] // omit surrounding quotes + // Omit surrounding quotes + (start + 1, end - 1) } Kind::PrivateIdentifier => { - &raw[1..] // omit leading `#` + // Omit leading `#` + (start + 1, end) } - _ => raw, - } + _ => (start, end), + }; + let whole_source = self.source.whole(); + debug_assert!(start <= whole_source.len()); + debug_assert!(end <= whole_source.len()); + debug_assert!(start <= end); + debug_assert!(whole_source.is_char_boundary(start)); + debug_assert!(whole_source.is_char_boundary(end)); + // SAFETY: `token` is guaranteed to be within the source and on UTF-8 boundaries. + unsafe { whole_source.get_unchecked(start..end) } } }