Skip to content

Commit

Permalink
engine: more efficient sorting
Browse files Browse the repository at this point in the history
Signed-off-by: Vicent Marti <[email protected]>
  • Loading branch information
vmg committed Oct 27, 2023
1 parent ba7b9ca commit c2b9797
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions go/vt/vtgate/engine/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"context"
"fmt"
"math/rand"
"slices"
"sort"
"strconv"
"strings"
Expand Down Expand Up @@ -428,27 +429,27 @@ func (route *Route) sort(in *sqltypes.Result) (*sqltypes.Result, error) {

comparers := extractSlices(route.OrderBy)

sort.Slice(out.Rows, func(i, j int) bool {
slices.SortFunc(out.Rows, func(a, b sqltypes.Row) int {
var cmp int
if err != nil {
return true
return -1
}
// If there are any errors below, the function sets
// the external err and returns true. Once err is set,
// all subsequent calls return true. This will make
// Slice think that all elements are in the correct
// order and return more quickly.
for _, c := range comparers {
cmp, err = c.compare(out.Rows[i], out.Rows[j])
cmp, err = c.compare(a, b)
if err != nil {
return true
return -1
}
if cmp == 0 {
continue
if cmp != 0 {
return cmp
}
return cmp < 0
return cmp

Check failure on line 450 in go/vt/vtgate/engine/route.go

View workflow job for this annotation

GitHub Actions / Static Code Checks Etc

SA4004: the surrounding loop is unconditionally terminated (staticcheck)
}
return true
return 0
})

return out.Truncate(route.TruncateColumnCount), err
Expand Down

0 comments on commit c2b9797

Please sign in to comment.