Skip to content

Commit 8ea72a8

Browse files
committed
perf(parser): speed up parsing octal literals (#4258)
Micro-optimization to parsing octal numbers. This removes usage of `char`, so removes a surprising amount of instructions.
1 parent 226ecd7 commit 8ea72a8

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

crates/oxc_parser/src/lexer/number.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,14 @@ fn parse_int_without_underscores(s: &str, kind: Kind) -> Result<f64, &'static st
3131
Kind::Decimal => Ok(parse_decimal(s)),
3232
Kind::Binary => Ok(parse_binary(&s[2..])),
3333
Kind::Octal => {
34-
let s = if s.starts_with("0o") || s.starts_with("0O") {
35-
&s[2..]
34+
// Octals always begin with `0`. Trim off leading `0`, `0o` or `0O`.
35+
let second_byte = s.as_bytes()[1];
36+
let s = if second_byte == b'o' || second_byte == b'O' {
37+
// SAFETY: We just checked that 2nd byte is ASCII, so slicing off 2 bytes
38+
// must be in bounds and on a UTF-8 character boundary.
39+
unsafe { s.get_unchecked(2..) }
3640
} else {
37-
s // legacy octal
41+
&s[1..] // legacy octal
3842
};
3943
Ok(parse_octal(s))
4044
}

0 commit comments

Comments
 (0)