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>
This commit is contained in:
mergify[bot]
2023-12-23 13:22:51 +00:00
committed by GitHub
co-authored by Dev Ojha Julien Robert
parent 52c3db2eae
commit 23b78d9dd1
2 changed files with 7 additions and 1 deletions
+6 -1
View File
@@ -820,7 +820,12 @@ var _ sort.Interface = Coins{}
// Sort is a helper function to sort the set of coins in-place
func (coins Coins) Sort() Coins {
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
}