From e6d5a444a51c3a08705ed664476b63cc9cf99c3f Mon Sep 17 00:00:00 2001 From: camc314 <18101008+camc314@users.noreply.github.com> Date: Sun, 27 Apr 2025 10:55:41 +0000 Subject: [PATCH] perf(lexer): use `get_unchecked` for byte access in comment parsing (#10635) our benchmarks for lexer only has a handful of `@preserve` comments so i doubt perf change will be noticable. --- crates/oxc_parser/src/lexer/trivia_builder.rs | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/crates/oxc_parser/src/lexer/trivia_builder.rs b/crates/oxc_parser/src/lexer/trivia_builder.rs index 1b93f65bbe0aa..a451584bd73c1 100644 --- a/crates/oxc_parser/src/lexer/trivia_builder.rs +++ b/crates/oxc_parser/src/lexer/trivia_builder.rs @@ -214,15 +214,19 @@ fn contains_license_or_preserve_comment(s: &str) -> bool { let search_len = hay.len() - 8; for i in memchr_iter(b'@', &hay[..search_len]) { - match hay[i + 1] { + debug_assert!(i < search_len); + // SAFETY: we `i` has a max val of len of bytes - 8, so accessing `i + 1` is safe + match unsafe { hay.get_unchecked(i + 1) } { // spellchecker:off b'l' => { - if &hay[i + 2..i + 1 + 7] == b"icense" { + // SAFETY: we `i` has a max val of len of bytes - 8, so accessing `i + 7` is safe + if unsafe { hay.get_unchecked(i + 2..i + 1 + 7) } == b"icense" { return true; } } b'p' => { - if &hay[i + 2..i + 1 + 8] == b"reserve" { + // SAFETY: we `i` has a max val of len of bytes - 8, so accessing `i + 8` is safe + if unsafe { hay.get_unchecked(i + 2..i + 1 + 8) } == b"reserve" { return true; } }