planner: use slices.Sort to simple code (#61505)

This commit is contained in:
Weizhen Wang
2025-06-05 13:31:20 +08:00
committed by GitHub
parent 7caaf406a3
commit ac812cac18
2 changed files with 21 additions and 25 deletions

View File

@ -15,6 +15,7 @@
package core
import (
"cmp"
"context"
"fmt"
"maps"
@ -5269,6 +5270,11 @@ func (t *TblColPosInfo) MemoryUsage() (sum int64) {
return
}
// Cmp compares two TblColPosInfo by their Start field.
func (t *TblColPosInfo) Cmp(a TblColPosInfo) int {
return cmp.Compare(t.Start, a.Start)
}
// TblColPosInfoSlice attaches the methods of sort.Interface to []TblColPosInfos sorting in increasing order.
type TblColPosInfoSlice []TblColPosInfo
@ -5277,16 +5283,6 @@ func (c TblColPosInfoSlice) Len() int {
return len(c)
}
// Swap implements sort.Interface#Swap.
func (c TblColPosInfoSlice) Swap(i, j int) {
c[i], c[j] = c[j], c[i]
}
// Less implements sort.Interface#Less.
func (c TblColPosInfoSlice) Less(i, j int) bool {
return c[i].Start < c[j].Start
}
// FindTblIdx finds the ordinal of the corresponding access column.
func (c TblColPosInfoSlice) FindTblIdx(colOrdinal int) (int, bool) {
if len(c) == 0 {
@ -5321,7 +5317,9 @@ func buildColumns2HandleWithWrtiableColumns(
cols2Handles = append(cols2Handles, TblColPosInfo{TblID: tblID, Start: offset, End: end, HandleCols: handleCol})
}
}
sort.Sort(cols2Handles)
slices.SortFunc(cols2Handles, func(a, b TblColPosInfo) int {
return a.Cmp(b)
})
return cols2Handles, nil
}
@ -5363,8 +5361,9 @@ func pruneAndBuildColPositionInfoForDelete(
}
}
// Sort by start position. To do the later column pruning.
// TODO: `sort`` package has a rather worse performance. We should replace it with the new `slice` package.
sort.Sort(cols2PosInfos)
slices.SortFunc(cols2PosInfos, func(a, b TblColPosInfo) int {
return a.Cmp(b)
})
prunedColCnt := 0
var err error
for i := range cols2PosInfos {

View File

@ -931,6 +931,10 @@ type partitionRange struct {
end int
}
func (p *partitionRange) Cmp(a partitionRange) int {
return cmp.Compare(p.start, a.start)
}
// partitionRangeOR represents OR(range1, range2, ...)
type partitionRangeOR []partitionRange
@ -958,14 +962,6 @@ func (or partitionRangeOR) Len() int {
return len(or)
}
func (or partitionRangeOR) Less(i, j int) bool {
return or[i].start < or[j].start
}
func (or partitionRangeOR) Swap(i, j int) {
or[i], or[j] = or[j], or[i]
}
func (or partitionRangeOR) union(x partitionRangeOR) partitionRangeOR {
or = append(or, x...)
return or.simplify()
@ -977,13 +973,14 @@ func (or partitionRangeOR) simplify() partitionRangeOR {
return or
}
// Make the ranges order by start.
sort.Sort(or)
sorted := or
slices.SortFunc(or, func(i, j partitionRange) int {
return i.Cmp(j)
})
// Iterate the sorted ranges, merge the adjacent two when their range overlap.
// For example, [0, 1), [2, 7), [3, 5), ... => [0, 1), [2, 7) ...
res := sorted[:1]
for _, curr := range sorted[1:] {
res := or[:1]
for _, curr := range or[1:] {
last := &res[len(res)-1]
if curr.start > last.end {
res = append(res, curr)