From 4db9838ae633df4675cc26bfc248699632319e7b Mon Sep 17 00:00:00 2001 From: Dev Ojha Date: Mon, 19 Feb 2024 03:15:57 -0600 Subject: [PATCH] perf: Remove an extra heap allocation from NewLegacyDec (#19466) Co-authored-by: Marko --- math/CHANGELOG.md | 1 + math/dec.go | 3 ++- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/math/CHANGELOG.md b/math/CHANGELOG.md index db0512ffae..0d76574cfe 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 +* [#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 ### Bug Fixes diff --git a/math/dec.go b/math/dec.go index 686061402a..bd43232bf3 100644 --- a/math/dec.go +++ b/math/dec.go @@ -102,8 +102,9 @@ func LegacyNewDec(i int64) LegacyDec { // create a new Dec from integer with decimal place at prec // CONTRACT: prec <= Precision func LegacyNewDecWithPrec(i, prec int64) LegacyDec { + bi := big.NewInt(i) return LegacyDec{ - new(big.Int).Mul(big.NewInt(i), precisionMultiplier(prec)), + bi.Mul(bi, precisionMultiplier(prec)), } }