Skip to content

Commit 7582fc0

Browse files
ValarDragonmergify[bot]
authored andcommitted
perf: Speedup coins.Sort() when coins is of length 1 (#18875)
(cherry picked from commit bd04173) # Conflicts: # CHANGELOG.md
1 parent b912ccf commit 7582fc0

2 files changed

Lines changed: 13 additions & 1 deletion

File tree

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,13 @@ Ref: https://keepachangelog.com/en/1.0.0/
4545

4646
### Improvements
4747

48+
<<<<<<< HEAD
49+
=======
50+
* (types) [#18875](https://github.com/cosmos/cosmos-sdk/pull/18875) Speedup coins.Sort() if len(coins) <= 1
51+
* (client/keys) [#18745](https://github.com/cosmos/cosmos-sdk/pull/18745) Improve `<appd> keys export` and `<appd> keys mnemonic` by adding --yes option to skip interactive confirmation.
52+
* (client/keys) [#18743](https://github.com/cosmos/cosmos-sdk/pull/18743) Improve `<appd> keys add -i` by hiding inputting of bip39 passphrase.
53+
* (client/keys) [#18703](https://github.com/cosmos/cosmos-sdk/pull/18703) Improve `<appd> keys add` and `<appd> keys show` by checking whether there are duplicate keys in the multisig case.
54+
>>>>>>> bd0417301 (perf: Speedup coins.Sort() when coins is of length 1 (#18875))
4855
* (x/gov) [#18707](https://github.com/cosmos/cosmos-sdk/pull/18707) Improve genesis validation.
4956
* (server) [#18478](https://github.com/cosmos/cosmos-sdk/pull/18478) Add command flag to disable colored logs.
5057

types/coin.go

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

828828
// Sort is a helper function to sort the set of coins in-place
829829
func (coins Coins) Sort() Coins {
830-
sort.Sort(coins)
830+
// sort.Sort(coins) does a costly runtime copy as part of `runtime.convTSlice`
831+
// So we avoid this heap allocation if len(coins) <= 1. In the future, we should hopefully find
832+
// a strategy to always avoid this.
833+
if len(coins) > 1 {
834+
sort.Sort(coins)
835+
}
831836
return coins
832837
}
833838

0 commit comments

Comments
 (0)