perf: Speedup coins.Sort() when coins is of length 1 (#18875)
This commit is contained in:
+6
-1
@@ -823,7 +823,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
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user