Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(parser): extract u8 not &u8 when iterating over bytes #4568

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 35 additions & 35 deletions crates/oxc_parser/src/lexer/number.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,15 @@ fn parse_decimal(s: &str) -> f64 {
}

let mut result = 0_u64;
for b in s.as_bytes() {
for &b in s.as_bytes() {
// The latency of the multiplication can be hidden by issuing it
// before the result is needed to improve performance on
// modern out-of-order CPU as multiplication here is slower
// than the other instructions, we can get the end result faster
// doing multiplication first and let the CPU spends other cycles
// doing other computation and get multiplication result later.
result *= 10;
let n = decimal_byte_to_value(*b);
let n = decimal_byte_to_value(b);
result += n as u64;
}
result as f64
Expand All @@ -97,10 +97,10 @@ fn parse_decimal_with_underscores(s: &str) -> f64 {
}

let mut result = 0_u64;
for b in s.as_bytes() {
if *b != b'_' {
for &b in s.as_bytes() {
if b != b'_' {
result *= 10;
let n = decimal_byte_to_value(*b);
let n = decimal_byte_to_value(b);
result += n as u64;
}
}
Expand Down Expand Up @@ -159,9 +159,9 @@ fn parse_binary(s: &str) -> f64 {
}

let mut result = 0_u64;
for b in s.as_bytes() {
for &b in s.as_bytes() {
result <<= 1;
result |= binary_byte_to_value(*b) as u64;
result |= binary_byte_to_value(b) as u64;
}
result as f64
}
Expand All @@ -170,8 +170,8 @@ fn parse_binary(s: &str) -> f64 {
#[inline(never)]
fn parse_binary_slow(s: &str) -> f64 {
let mut result = 0_f64;
for b in s.as_bytes() {
let value = f64::from(binary_byte_to_value(*b));
for &b in s.as_bytes() {
let value = f64::from(binary_byte_to_value(b));
result = result.mul_add(2.0, value);
}
result
Expand All @@ -191,10 +191,10 @@ fn parse_binary_with_underscores(s: &str) -> f64 {
}

let mut result = 0_u64;
for b in s.as_bytes() {
if *b != b'_' {
for &b in s.as_bytes() {
if b != b'_' {
result <<= 1;
result |= binary_byte_to_value(*b) as u64;
result |= binary_byte_to_value(b) as u64;
}
}
result as f64
Expand All @@ -204,9 +204,9 @@ fn parse_binary_with_underscores(s: &str) -> f64 {
#[inline(never)]
fn parse_binary_with_underscores_slow(s: &str) -> f64 {
let mut result = 0_f64;
for b in s.as_bytes() {
if *b != b'_' {
let value = f64::from(binary_byte_to_value(*b));
for &b in s.as_bytes() {
if b != b'_' {
let value = f64::from(binary_byte_to_value(b));
result = result.mul_add(2.0, value);
}
}
Expand Down Expand Up @@ -239,8 +239,8 @@ fn parse_octal(s: &str) -> f64 {
}

let mut result = 0_u64;
for b in s.as_bytes() {
let n = octal_byte_to_value(*b);
for &b in s.as_bytes() {
let n = octal_byte_to_value(b);
result <<= 3;
result |= n as u64;
}
Expand All @@ -252,8 +252,8 @@ fn parse_octal(s: &str) -> f64 {
#[allow(clippy::cast_precision_loss, clippy::cast_lossless)]
fn parse_octal_slow(s: &str) -> f64 {
let mut result = 0_f64;
for b in s.as_bytes() {
let value = f64::from(octal_byte_to_value(*b));
for &b in s.as_bytes() {
let value = f64::from(octal_byte_to_value(b));
result = result.mul_add(8.0, value);
}
result
Expand All @@ -271,9 +271,9 @@ fn parse_octal_with_underscores(s: &str) -> f64 {
}

let mut result = 0_u64;
for b in s.as_bytes() {
if *b != b'_' {
let n = octal_byte_to_value(*b);
for &b in s.as_bytes() {
if b != b'_' {
let n = octal_byte_to_value(b);
result <<= 3;
result |= n as u64;
}
Expand All @@ -286,9 +286,9 @@ fn parse_octal_with_underscores(s: &str) -> f64 {
#[allow(clippy::cast_precision_loss, clippy::cast_lossless)]
fn parse_octal_with_underscores_slow(s: &str) -> f64 {
let mut result = 0_f64;
for b in s.as_bytes() {
if *b != b'_' {
let value = f64::from(octal_byte_to_value(*b));
for &b in s.as_bytes() {
if b != b'_' {
let value = f64::from(octal_byte_to_value(b));
result = result.mul_add(8.0, value);
}
}
Expand Down Expand Up @@ -333,8 +333,8 @@ fn parse_hex(s: &str) -> f64 {
}

let mut result = 0_u64;
for b in s.as_bytes() {
let n = hex_byte_to_value(*b);
for &b in s.as_bytes() {
let n = hex_byte_to_value(b);
result <<= 4;
result |= n as u64;
}
Expand All @@ -345,8 +345,8 @@ fn parse_hex(s: &str) -> f64 {
#[inline(never)]
fn parse_hex_slow(s: &str) -> f64 {
let mut result = 0_f64;
for b in s.as_bytes() {
let value = f64::from(hex_byte_to_value(*b));
for &b in s.as_bytes() {
let value = f64::from(hex_byte_to_value(b));
result = result.mul_add(16.0, value);
}
result
Expand All @@ -365,9 +365,9 @@ fn parse_hex_with_underscores(s: &str) -> f64 {
}

let mut result = 0_u64;
for b in s.as_bytes() {
if *b != b'_' {
let n = hex_byte_to_value(*b);
for &b in s.as_bytes() {
if b != b'_' {
let n = hex_byte_to_value(b);
result <<= 4;
result |= n as u64;
}
Expand All @@ -379,9 +379,9 @@ fn parse_hex_with_underscores(s: &str) -> f64 {
#[inline(never)]
fn parse_hex_with_underscores_slow(s: &str) -> f64 {
let mut result = 0_f64;
for b in s.as_bytes() {
if *b != b'_' {
let value = f64::from(hex_byte_to_value(*b));
for &b in s.as_bytes() {
if b != b'_' {
let value = f64::from(hex_byte_to_value(b));
result = result.mul_add(16.0, value);
}
}
Expand Down