Skip to content

Commit ac812ca

Browse files
authored
planner: use slices.Sort to simple code (#61505)
1 parent 7caaf40 commit ac812ca

File tree

2 files changed

+21
-25
lines changed

2 files changed

+21
-25
lines changed

pkg/planner/core/logical_plan_builder.go

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
package core
1616

1717
import (
18+
"cmp"
1819
"context"
1920
"fmt"
2021
"maps"
@@ -5269,6 +5270,11 @@ func (t *TblColPosInfo) MemoryUsage() (sum int64) {
52695270
return
52705271
}
52715272

5273+
// Cmp compares two TblColPosInfo by their Start field.
5274+
func (t *TblColPosInfo) Cmp(a TblColPosInfo) int {
5275+
return cmp.Compare(t.Start, a.Start)
5276+
}
5277+
52725278
// TblColPosInfoSlice attaches the methods of sort.Interface to []TblColPosInfos sorting in increasing order.
52735279
type TblColPosInfoSlice []TblColPosInfo
52745280

@@ -5277,16 +5283,6 @@ func (c TblColPosInfoSlice) Len() int {
52775283
return len(c)
52785284
}
52795285

5280-
// Swap implements sort.Interface#Swap.
5281-
func (c TblColPosInfoSlice) Swap(i, j int) {
5282-
c[i], c[j] = c[j], c[i]
5283-
}
5284-
5285-
// Less implements sort.Interface#Less.
5286-
func (c TblColPosInfoSlice) Less(i, j int) bool {
5287-
return c[i].Start < c[j].Start
5288-
}
5289-
52905286
// FindTblIdx finds the ordinal of the corresponding access column.
52915287
func (c TblColPosInfoSlice) FindTblIdx(colOrdinal int) (int, bool) {
52925288
if len(c) == 0 {
@@ -5321,7 +5317,9 @@ func buildColumns2HandleWithWrtiableColumns(
53215317
cols2Handles = append(cols2Handles, TblColPosInfo{TblID: tblID, Start: offset, End: end, HandleCols: handleCol})
53225318
}
53235319
}
5324-
sort.Sort(cols2Handles)
5320+
slices.SortFunc(cols2Handles, func(a, b TblColPosInfo) int {
5321+
return a.Cmp(b)
5322+
})
53255323
return cols2Handles, nil
53265324
}
53275325

@@ -5363,8 +5361,9 @@ func pruneAndBuildColPositionInfoForDelete(
53635361
}
53645362
}
53655363
// Sort by start position. To do the later column pruning.
5366-
// TODO: `sort`` package has a rather worse performance. We should replace it with the new `slice` package.
5367-
sort.Sort(cols2PosInfos)
5364+
slices.SortFunc(cols2PosInfos, func(a, b TblColPosInfo) int {
5365+
return a.Cmp(b)
5366+
})
53685367
prunedColCnt := 0
53695368
var err error
53705369
for i := range cols2PosInfos {

pkg/planner/core/rule_partition_processor.go

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -931,6 +931,10 @@ type partitionRange struct {
931931
end int
932932
}
933933

934+
func (p *partitionRange) Cmp(a partitionRange) int {
935+
return cmp.Compare(p.start, a.start)
936+
}
937+
934938
// partitionRangeOR represents OR(range1, range2, ...)
935939
type partitionRangeOR []partitionRange
936940

@@ -958,14 +962,6 @@ func (or partitionRangeOR) Len() int {
958962
return len(or)
959963
}
960964

961-
func (or partitionRangeOR) Less(i, j int) bool {
962-
return or[i].start < or[j].start
963-
}
964-
965-
func (or partitionRangeOR) Swap(i, j int) {
966-
or[i], or[j] = or[j], or[i]
967-
}
968-
969965
func (or partitionRangeOR) union(x partitionRangeOR) partitionRangeOR {
970966
or = append(or, x...)
971967
return or.simplify()
@@ -977,13 +973,14 @@ func (or partitionRangeOR) simplify() partitionRangeOR {
977973
return or
978974
}
979975
// Make the ranges order by start.
980-
sort.Sort(or)
981-
sorted := or
976+
slices.SortFunc(or, func(i, j partitionRange) int {
977+
return i.Cmp(j)
978+
})
982979

983980
// Iterate the sorted ranges, merge the adjacent two when their range overlap.
984981
// For example, [0, 1), [2, 7), [3, 5), ... => [0, 1), [2, 7) ...
985-
res := sorted[:1]
986-
for _, curr := range sorted[1:] {
982+
res := or[:1]
983+
for _, curr := range or[1:] {
987984
last := &res[len(res)-1]
988985
if curr.start > last.end {
989986
res = append(res, curr)

0 commit comments

Comments
 (0)