mirror of
https://github.com/uutils/coreutils.git
synced 2026-06-10 15:48:22 -07: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:
@@ -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() {
|
||||
|
||||
Reference in New Issue
Block a user