mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-28 11:37:44 +00:00
uucore: num_parser: Improve scale conversion to i64
It turns out repeatedly calling i64::MAX.into() and i64::MIN.into() is actually very expensive. Just do the conversion first, and if it fails, we know why. Sadly there is still a conversion happening under the hood in `-exponent + scale`, but that'd need to be fixed in Bigint. Improves sort -g performance by ~5%.
This commit is contained in:
parent
8426c1480c
commit
edc1e5def6
1 changed files with 6 additions and 7 deletions
|
@ -465,16 +465,15 @@ fn construct_extended_big_decimal<'a>(
|
|||
let bd = if scale == 0 && exponent.is_zero() {
|
||||
BigDecimal::from_bigint(signed_digits, 0)
|
||||
} else if base == Base::Decimal {
|
||||
let new_scale = BigInt::from(scale) - exponent;
|
||||
let new_scale = -exponent + scale;
|
||||
|
||||
// BigDecimal "only" supports i64 scale.
|
||||
// Note that new_scale is a negative exponent: large value causes an underflow, small value an overflow.
|
||||
if new_scale > i64::MAX.into() {
|
||||
return Err(make_error(false, negative));
|
||||
} else if new_scale < i64::MIN.into() {
|
||||
return Err(make_error(true, negative));
|
||||
// Note that new_scale is a negative exponent: large positive value causes an underflow, large negative values an overflow.
|
||||
if let Some(new_scale) = new_scale.to_i64() {
|
||||
BigDecimal::from_bigint(signed_digits, new_scale)
|
||||
} else {
|
||||
return Err(make_error(new_scale.is_negative(), negative));
|
||||
}
|
||||
BigDecimal::from_bigint(signed_digits, new_scale.to_i64().unwrap())
|
||||
} else if base == Base::Hexadecimal {
|
||||
// pow "only" supports u32 values, just error out if given more than 2**32 fractional digits.
|
||||
if scale > u32::MAX.into() {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue