From 0f113520f9b92c68aca15fbb7e241c12767fdd6c Mon Sep 17 00:00:00 2001 From: Dev Ojha Date: Mon, 19 Feb 2024 02:21:47 -0600 Subject: [PATCH] perf: Remove unneeded cmp in Ceil and marshalTo (#19467) --- math/CHANGELOG.md | 1 + math/dec.go | 8 +++----- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/math/CHANGELOG.md b/math/CHANGELOG.md index 85b04aed13..db0512ffae 100644 --- a/math/CHANGELOG.md +++ b/math/CHANGELOG.md @@ -43,6 +43,7 @@ Ref: https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.j * [#18874](https://github.com/cosmos/cosmos-sdk/pull/18874) Speedup `math.Int.Mul` by removing a duplicate overflow check * [#19386](https://github.com/cosmos/cosmos-sdk/pull/19386) Speedup `math.Int` overflow checks +* [#19467](https://github.com/cosmos/cosmos-sdk/pull/19467) Slightly speedup `math.LegacyDec` `Ceil` and `MarshalTo` methods ### Bug Fixes diff --git a/math/dec.go b/math/dec.go index 58abb9ca1a..686061402a 100644 --- a/math/dec.go +++ b/math/dec.go @@ -734,11 +734,9 @@ func (d LegacyDec) Ceil() LegacyDec { quo, rem = quo.QuoRem(tmp, precisionReuse, rem) // no need to round with a zero remainder regardless of sign - if rem.Cmp(zeroInt) == 0 { + if rem.Sign() == 0 { return LegacyNewDecFromBigInt(quo) - } - - if rem.Sign() == -1 { + } else if rem.Sign() == -1 { return LegacyNewDecFromBigInt(quo) } @@ -847,7 +845,7 @@ func (d *LegacyDec) MarshalTo(data []byte) (n int, err error) { i = new(big.Int) } - if i.Cmp(zeroInt) == 0 { + if i.Sign() == 0 { copy(data, []byte{0x30}) return 1, nil }