Skip to content

Commit 82d784f

Browse files
perf(lexer): reduce bounds checks in Lexer::get_string (#16317)
`Lexer::get_string` was slicing source text twice - once to obtain `raw` and then again to trim off a byte from start/end in the case of strings. Instead, slice only once. This removes 2 bounds checks and 2 x UTF-8 character boundary checks for strings.
1 parent cc2f352 commit 82d784f

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

crates/oxc_parser/src/lexer/string.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -255,15 +255,21 @@ impl<'a> Lexer<'a> {
255255
return self.escaped_strings[&token.start()];
256256
}
257257

258-
let raw = &self.source.whole()[token.start() as usize..token.end() as usize];
258+
let source_text = self.source.whole();
259+
let mut start = token.start() as usize;
260+
let mut end = token.end() as usize;
259261
match token.kind() {
260262
Kind::Str => {
261-
&raw[1..raw.len() - 1] // omit surrounding quotes
263+
// Omit surrounding quotes
264+
start += 1;
265+
end -= 1;
262266
}
263267
Kind::PrivateIdentifier => {
264-
&raw[1..] // omit leading `#`
268+
// Omit leading `#`
269+
start += 1;
265270
}
266-
_ => raw,
271+
_ => {}
267272
}
273+
&source_text[start..end]
268274
}
269275
}

0 commit comments

Comments
 (0)