Skip to content

Commit 5d406c1

Browse files
mergify[bot]DongLieutac0turtle
authored
perf: Speedup DecCoin.Sort() when coins is of length 1 (backport #18888) (#18890)
Co-authored-by: Dong Lieu <93205232+DongLieu@users.noreply.github.com> Co-authored-by: marbar3778 <marbar3778@yahoo.com>
1 parent 1b809a4 commit 5d406c1

2 files changed

Lines changed: 7 additions & 1 deletion

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
4646

4747
### Improvements
4848

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

types/dec_coin.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -611,7 +611,12 @@ func (coins DecCoins) Swap(i, j int) { coins[i], coins[j] = coins[j], coins[i] }
611611

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

0 commit comments

Comments
 (0)