Skip to content

Commit

Permalink
Do multiplication first + inline
Browse files Browse the repository at this point in the history
  • Loading branch information
overlookmotel authored and DonIsaac committed Jul 15, 2024
1 parent 8d75648 commit f51a737
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion crates/oxc_parser/src/lexer/number.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ fn parse_float_without_underscores(s: &str) -> Result<f64, &'static str> {
/// This is produces more compact assembly than `c - b'0'`.
///
/// <https://godbolt.org/z/WMarz15sq>
#[inline]
const fn decimal_byte_to_value(c: u8) -> u8 {
debug_assert!(c >= b'0' && c <= b'9');
c & 15
Expand All @@ -75,8 +76,15 @@ fn parse_decimal(s: &str) -> f64 {

let mut result = 0_u64;
for c 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(*c);
result = result * 10 + n as u64;
result += n as u64;
}
result as f64
}
Expand All @@ -99,6 +107,7 @@ fn parse_decimal_slow(s: &str) -> f64 {
/// This is produces more compact assembly than `c - b'0'`.
///
/// <https://godbolt.org/z/1vvrK78jf>
#[inline]
const fn binary_byte_to_value(c: u8) -> u8 {
debug_assert!(c == b'0' || c == b'1');
c & 1
Expand Down Expand Up @@ -159,6 +168,7 @@ fn parse_binary_slow(s: &str) -> f64 {
/// This is produces more compact assembly than `c - b'0'`.
///
/// <https://godbolt.org/z/9rYTsMoMM>
#[inline]
const fn octal_byte_to_value(c: u8) -> u8 {
debug_assert!(c >= b'0' && c <= b'7');
c & 7
Expand Down Expand Up @@ -212,6 +222,7 @@ fn parse_octal_slow(s: &str) -> f64 {
/// but only because compiler unrolls the loop.
///
/// <https://godbolt.org/z/5fsdv8rGo>
#[inline]
const fn hex_byte_to_value(c: u8) -> u8 {
debug_assert!((c >= b'0' && c <= b'9') || (c >= b'A' && c <= b'F') || (c >= b'a' && c <= b'f'));
if c < b'A' {
Expand Down

0 comments on commit f51a737

Please sign in to comment.