From 80fb841f2a111e9d59d00828f7f498ab541b783c Mon Sep 17 00:00:00 2001 From: Jake Waggoner Date: Mon, 11 Oct 2021 19:30:56 -0400 Subject: [PATCH 1/5] Added anohter loop to check if a zero coin exists before allocating space --- types/coin.go | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/types/coin.go b/types/coin.go index 68cd7062505f..e1ef4fd29545 100644 --- a/types/coin.go +++ b/types/coin.go @@ -608,7 +608,16 @@ 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)) + result := ([]Coin)(nil) + + for i := 0; i < len(coins); i++ { + if coins[i].IsZero() { + result = make([]Coin, 0, len(coins)) + break + } else if i == len(coins)-1 { + return coins + } + } for _, coin := range coins { if !coin.IsZero() { From d32d000fd9ba68983070e17db2c42e3862d51e92 Mon Sep 17 00:00:00 2001 From: Jake Waggoner Date: Mon, 11 Oct 2021 19:56:19 -0400 Subject: [PATCH 2/5] Updated CHANGELOG.md --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 535ee7a42efc..efe040b31929 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -125,6 +125,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) Only allocate memory when a zero coin is found ### Bug Fixes From 5b93ecf3c31343f3285a55d2c37f470918708260 Mon Sep 17 00:00:00 2001 From: Jake Waggoner Date: Tue, 12 Oct 2021 23:55:51 -0400 Subject: [PATCH 3/5] Updated changelog and result slice declaration --- CHANGELOG.md | 2 +- types/coin.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b0b7cae825c7..654152441fa2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -126,7 +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) Only allocate memory when a zero coin is found +* [\#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 34b74478936b..ccab7eb04309 100644 --- a/types/coin.go +++ b/types/coin.go @@ -626,7 +626,7 @@ func (coins Coins) negative() Coins { // removeZeroCoins removes all zero coins from the given coin set in-place. func removeZeroCoins(coins Coins) Coins { - result := ([]Coin)(nil) + var result []Coin for i := 0; i < len(coins); i++ { if coins[i].IsZero() { From d640147bd06c999dd9bd733187d45f2e2886f740 Mon Sep 17 00:00:00 2001 From: Jake Waggoner Date: Wed, 13 Oct 2021 12:21:44 -0400 Subject: [PATCH 4/5] Move initialization after the scan loop --- types/coin.go | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/types/coin.go b/types/coin.go index ccab7eb04309..93ac3481f36c 100644 --- a/types/coin.go +++ b/types/coin.go @@ -626,17 +626,16 @@ func (coins Coins) negative() Coins { // removeZeroCoins removes all zero coins from the given coin set in-place. func removeZeroCoins(coins Coins) Coins { - var result []Coin - for i := 0; i < len(coins); i++ { if coins[i].IsZero() { - result = make([]Coin, 0, len(coins)) break } else if i == len(coins)-1 { return coins } } + result := make([]Coin, 0, len(coins)-1) + for _, coin := range coins { if !coin.IsZero() { result = append(result, coin) From 78e38b2c4cd321601dec2d04cacacc313de98859 Mon Sep 17 00:00:00 2001 From: Jake Waggoner Date: Wed, 13 Oct 2021 13:39:36 -0400 Subject: [PATCH 5/5] Add a check for 0 length slice --- types/coin.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/types/coin.go b/types/coin.go index 93ac3481f36c..ce222e1709f8 100644 --- a/types/coin.go +++ b/types/coin.go @@ -634,7 +634,10 @@ func removeZeroCoins(coins Coins) Coins { } } - result := make([]Coin, 0, len(coins)-1) + var result []Coin + if len(coins) > 0 { + result = make([]Coin, 0, len(coins)-1) + } for _, coin := range coins { if !coin.IsZero() {