From 0c6b6d49cb74ab38a5cd663f4207d2a1d1cef514 Mon Sep 17 00:00:00 2001 From: Dev Ojha Date: Tue, 26 Dec 2023 06:04:10 -0600 Subject: [PATCH] perf: Remove duplicate overflow check from int.Mul (#18874) --- math/CHANGELOG.md | 2 ++ math/int.go | 6 +----- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/math/CHANGELOG.md b/math/CHANGELOG.md index 2f512456c3..d246eda970 100644 --- a/math/CHANGELOG.md +++ b/math/CHANGELOG.md @@ -41,6 +41,8 @@ Ref: https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.j * [#18421](https://github.com/cosmos/cosmos-sdk/pull/18421) Add mutative api for `LegacyDec.BigInt()`. +* [#18874](https://github.com/cosmos/cosmos-sdk/pull/18874) Speedup `math.Int.Mul` by removing a duplicate overflow check + ### Bug Fixes * [#18519](https://github.com/cosmos/cosmos-sdk/pull/18519) Prevent Overflow in `Dec.Ceil()`. diff --git a/math/int.go b/math/int.go index 54ee8f4f23..5b4bb7fa8d 100644 --- a/math/int.go +++ b/math/int.go @@ -333,12 +333,8 @@ func (i Int) MulRaw(i2 int64) Int { // SafeMul multiples Int from another and returns an error if overflow func (i Int) SafeMul(i2 Int) (res Int, err error) { - // Check overflow - if i.i.BitLen()+i2.i.BitLen()-1 > MaxBitLen { - return Int{}, ErrIntOverflow - } res = Int{mul(i.i, i2.i)} - // Check overflow if sign of both are same + // Check overflow if res.i.BitLen() > MaxBitLen { return Int{}, ErrIntOverflow }