Skip to content

Commit 23b78d9

Browse files
mergify[bot]ValarDragonjulienrbrt
authored
perf: Speedup coins.Sort() when coins is of length 1 (backport #18875) (#18877)
Co-authored-by: Dev Ojha <ValarDragon@users.noreply.github.com> Co-authored-by: Julien Robert <julien@rbrt.fr>
1 parent 52c3db2 commit 23b78d9

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
@@ -43,6 +43,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
4343
* (x/gov) [#18707](https://github.com/cosmos/cosmos-sdk/pull/18707) Improve genesis validation.
4444
* (x/auth/tx) [#18772](https://github.com/cosmos/cosmos-sdk/pull/18772) Remove misleading gas wanted from tx simulation failure log.
4545
* (client/tx) [#18852](https://github.com/cosmos/cosmos-sdk/pull/18852) Add `WithFromName` to tx factory.
46+
* (types) [#18875](https://github.com/cosmos/cosmos-sdk/pull/18875) Speedup coins.Sort() if len(coins) <= 1
4647

4748
### Bug Fixes
4849

types/coin.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -820,7 +820,12 @@ var _ sort.Interface = Coins{}
820820

821821
// Sort is a helper function to sort the set of coins in-place
822822
func (coins Coins) Sort() Coins {
823-
sort.Sort(coins)
823+
// sort.Sort(coins) does a costly runtime copy as part of `runtime.convTSlice`
824+
// So we avoid this heap allocation if len(coins) <= 1. In the future, we should hopefully find
825+
// a strategy to always avoid this.
826+
if len(coins) > 1 {
827+
sort.Sort(coins)
828+
}
824829
return coins
825830
}
826831

0 commit comments

Comments
 (0)