Skip to content

Commit

Permalink
perf: Speedup DecCoin.Sort() when coins is of length 1 (backport cosm…
Browse files Browse the repository at this point in the history
…os#18888) (cosmos#18890)

Co-authored-by: Dong Lieu <[email protected]>
Co-authored-by: marbar3778 <[email protected]>
  • Loading branch information
3 people authored and emidev98 committed Mar 21, 2024
1 parent ce1a7be commit fc75bbc
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ Ref: https://keepachangelog.com/en/1.0.0/

### Improvements

* (types) [#18888](https://github.com/cosmos/cosmos-sdk/pull/18888) Speedup DecCoin.Sort() if len(coins) <= 1
* (x/gov) [#18707](https://github.com/cosmos/cosmos-sdk/pull/18707) Improve genesis validation.
* (server) [#18478](https://github.com/cosmos/cosmos-sdk/pull/18478) Add command flag to disable colored logs.

Expand Down
7 changes: 6 additions & 1 deletion types/dec_coin.go
Original file line number Diff line number Diff line change
Expand Up @@ -611,7 +611,12 @@ func (coins DecCoins) Swap(i, j int) { coins[i], coins[j] = coins[j], coins[i] }

// Sort is a helper function to sort the set of decimal coins in-place.
func (coins DecCoins) Sort() DecCoins {
sort.Sort(coins)
// sort.Sort(coins) does a costly runtime copy as part of `runtime.convTSlice`
// So we avoid this heap allocation if len(coins) <= 1. In the future, we should hopefully find
// a strategy to always avoid this.
if len(coins) > 1 {
sort.Sort(coins)
}
return coins
}

Expand Down

0 comments on commit fc75bbc

Please sign in to comment.