diff --git a/CHANGELOG.md b/CHANGELOG.md index 1cbb65c163fe..a24cc57f3035 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -126,6 +126,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ * [\#9699](https://github.com/cosmos/cosmos-sdk/pull/9699) Add `:`, `.`, `-`, and `_` as allowed characters in the default denom regular expression. * (genesis) [\#9697](https://github.com/cosmos/cosmos-sdk/pull/9697) Ensure `InitGenesis` returns with non-empty validator set. * [\#10262](https://github.com/cosmos/cosmos-sdk/pull/10262) Remove unnecessary logging in `x/feegrant` simulation. +* [\#10339](https://github.com/cosmos/cosmos-sdk/pull/10339) Improve performance of `removeZeroCoins` by only allocating memory when necessary * [\#10341](https://github.com/cosmos/cosmos-sdk/pull/10341) Move from `io/ioutil` to `io` and `os` packages. ### Bug Fixes diff --git a/types/coin.go b/types/coin.go index ea8557a5df57..ce222e1709f8 100644 --- a/types/coin.go +++ b/types/coin.go @@ -626,7 +626,18 @@ func (coins Coins) negative() Coins { // removeZeroCoins removes all zero coins from the given coin set in-place. func removeZeroCoins(coins Coins) Coins { - result := 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() {