From 7cda7fd8e6acbba6b32f2c3aae0e1198a64abf86 Mon Sep 17 00:00:00 2001 From: Dong Lieu <93205232+DongLieu@users.noreply.github.com> Date: Tue, 26 Dec 2023 19:24:28 +0700 Subject: [PATCH] perf: Speedup DecCoin.Sort() when coins is of length 1 (#18888) (cherry picked from commit de4c9e743f51b12f60f27113f1f1948d78d0dd04) # Conflicts: # CHANGELOG.md --- CHANGELOG.md | 4 ++++ types/dec_coin.go | 7 ++++++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2aa2aafde9c0..057f778608f3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -40,9 +40,13 @@ Ref: https://keepachangelog.com/en/1.0.0/ ### Improvements +<<<<<<< HEAD * (x/gov) [#18707](https://github.com/cosmos/cosmos-sdk/pull/18707) Improve genesis validation. * (x/auth/tx) [#18772](https://github.com/cosmos/cosmos-sdk/pull/18772) Remove misleading gas wanted from tx simulation failure log. * (client/tx) [#18852](https://github.com/cosmos/cosmos-sdk/pull/18852) Add `WithFromName` to tx factory. +======= +* (types) [#18888](https://github.com/cosmos/cosmos-sdk/pull/18888) Speedup DecCoin.Sort() if len(coins) <= 1 +>>>>>>> de4c9e743 (perf: Speedup DecCoin.Sort() when coins is of length 1 (#18888)) * (types) [#18875](https://github.com/cosmos/cosmos-sdk/pull/18875) Speedup coins.Sort() if len(coins) <= 1 ### Bug Fixes diff --git a/types/dec_coin.go b/types/dec_coin.go index 74580b8d8f4c..14c40bd329b1 100644 --- a/types/dec_coin.go +++ b/types/dec_coin.go @@ -609,7 +609,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 }