diff --git a/CHANGELOG.md b/CHANGELOG.md index cc9a32b78b5a..752ab55f5191 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -42,6 +42,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ * [\#10262](https://github.com/cosmos/cosmos-sdk/pull/10262) Remove unnecessary logging in `x/feegrant` simulation. * [\#10327](https://github.com/cosmos/cosmos-sdk/pull/10327) Add null guard for possible nil `Amount` in tx fee `Coins` +* [\#10339](https://github.com/cosmos/cosmos-sdk/pull/10339) Improve performance of `removeZeroCoins` by only allocating memory when necessary ### Bug Fixes diff --git a/types/coin.go b/types/coin.go index fd81cb58203a..903945b25837 100644 --- a/types/coin.go +++ b/types/coin.go @@ -795,7 +795,18 @@ func (coins Coins) negative() Coins { // removeZeroCoins removes all zero coins from the given coin set in-place. func removeZeroCoins(coins Coins) Coins { - nonZeros := make([]Coin, 0, len(coins)) + for i := 0; i < len(coins); i++ { + if coins[i].IsZero() { + break + } else if i == len(coins)-1 { + return coins + } + } + + var result []Coin + if len(coins) > 0 { + result = make([]Coin, 0, len(coins)-1) + } for _, coin := range coins { if !coin.IsZero() {