Skip to content

Commit

Permalink
perf(math): Use Dec Bigintmut API (#19479)
Browse files Browse the repository at this point in the history
  • Loading branch information
ValarDragon authored Feb 19, 2024
1 parent 52cca5f commit 72eae6d
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 5 deletions.
1 change: 1 addition & 0 deletions math/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
10 changes: 5 additions & 5 deletions math/dec.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)),
}
}

Expand Down Expand Up @@ -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")
}
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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,
Expand All @@ -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
Expand Down

0 comments on commit 72eae6d

Please sign in to comment.