Skip to content

Commit d27454e

Browse files
committed
Using macro to avoid performance hit (thanks LingMan)
1 parent e93d03b commit d27454e

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

library/core/src/num/mod.rs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1078,14 +1078,20 @@ fn from_str_radix<T: FromStrRadixHelper>(src: &str, radix: u32) -> Result<T, Par
10781078
// `u8::MAX` is `ff` - any str of len 2 is guaranteed to not overflow.
10791079
// `i8::MAX` is `7f` - only a str of len 1 is guaranteed to not overflow.
10801080
unsafe {
1081-
let unchecked_additive_op =
1082-
if is_positive { T::unchecked_add } else { T::unchecked_sub };
1083-
1084-
for &c in digits {
1085-
result = result.unchecked_mul(radix);
1086-
let x = (c as char).to_digit(radix).ok_or(PIE { kind: InvalidDigit })?;
1087-
result = unchecked_additive_op(&result, x);
1081+
macro_rules! run_loop {
1082+
($unchecked_additive_op:ident) => {
1083+
for &c in digits {
1084+
result = result.unchecked_mul(radix);
1085+
let x = (c as char).to_digit(radix).ok_or(PIE { kind: InvalidDigit })?;
1086+
result = T::$unchecked_additive_op(&result, x);
1087+
}
1088+
};
10881089
}
1090+
if is_positive {
1091+
run_loop!(unchecked_add)
1092+
} else {
1093+
run_loop!(unchecked_sub)
1094+
};
10891095
}
10901096
} else {
10911097
let additive_op = if is_positive { T::checked_add } else { T::checked_sub };

0 commit comments

Comments
 (0)