diff --git a/math/CHANGELOG.md b/math/CHANGELOG.md index 0d76574cfe..5161b86e19 100644 --- a/math/CHANGELOG.md +++ b/math/CHANGELOG.md @@ -45,6 +45,7 @@ Ref: https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.j * [#19386](https://github.com/cosmos/cosmos-sdk/pull/19386) Speedup `math.Int` overflow checks * [#19466](https://github.com/cosmos/cosmos-sdk/pull/19466) Speedup `math.NewLegacyDec` by one heap allocation * [#19467](https://github.com/cosmos/cosmos-sdk/pull/19467) Slightly speedup `math.LegacyDec` `Ceil` and `MarshalTo` methods +* [#19479](https://github.com/cosmos/cosmos-sdk/pull/19479) Speedup `math.LegacyDec` functions that involve `math.Int` by removing a heap allocation. (`Ceil`, `TruncateInt`, `NewLegacyDecFromInt`) ### Bug Fixes diff --git a/math/dec.go b/math/dec.go index bd43232bf3..30d5ef464f 100644 --- a/math/dec.go +++ b/math/dec.go @@ -132,7 +132,7 @@ func LegacyNewDecFromInt(i Int) LegacyDec { // CONTRACT: prec <= Precision func LegacyNewDecFromIntWithPrec(i Int, prec int64) LegacyDec { return LegacyDec{ - new(big.Int).Mul(i.BigInt(), precisionMultiplier(prec)), + new(big.Int).Mul(i.BigIntMut(), precisionMultiplier(prec)), } } @@ -351,7 +351,7 @@ func (d LegacyDec) MulInt(i Int) LegacyDec { } func (d LegacyDec) MulIntMut(i Int) LegacyDec { - d.i.Mul(d.i, i.BigInt()) + d.i.Mul(d.i, i.BigIntMut()) if d.i.BitLen() > maxDecBitLen { panic("Int overflow") } @@ -434,7 +434,7 @@ func (d LegacyDec) QuoInt(i Int) LegacyDec { } func (d LegacyDec) QuoIntMut(i Int) LegacyDec { - d.i.Quo(d.i, i.BigInt()) + d.i.Quo(d.i, i.BigIntMut()) return d } @@ -692,7 +692,7 @@ func (d LegacyDec) RoundInt64() int64 { // RoundInt round the decimal using bankers rounding func (d LegacyDec) RoundInt() Int { - return NewIntFromBigInt(chopPrecisionAndRoundNonMutative(d.i)) + return NewIntFromBigIntMut(chopPrecisionAndRoundNonMutative(d.i)) } // chopPrecisionAndTruncate is similar to chopPrecisionAndRound, @@ -718,7 +718,7 @@ func (d LegacyDec) TruncateInt64() int64 { // TruncateInt truncates the decimals from the number and returns an Int func (d LegacyDec) TruncateInt() Int { - return NewIntFromBigInt(chopPrecisionAndTruncateNonMutative(d.i)) + return NewIntFromBigIntMut(chopPrecisionAndTruncateNonMutative(d.i)) } // TruncateDec truncates the decimals from the number and returns a Dec