Skip to content

Commit b289dc9

Browse files
authored
planner: use slices to eliminate branch check (pingcap#50911)
ref pingcap#47275
1 parent 9bad202 commit b289dc9

File tree

2 files changed

+8
-11
lines changed

2 files changed

+8
-11
lines changed

pkg/planner/core/flat_plan.go

+4-8
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ package core
1616

1717
import (
1818
"fmt"
19-
"sort"
19+
"slices"
2020

2121
"github.com/pingcap/tidb/pkg/kv"
2222
"github.com/pingcap/tidb/pkg/util/logutil"
@@ -456,20 +456,16 @@ func (f *FlatPhysicalPlan) flattenForeignKeyChecksAndCascadesMap(childCtx *opera
456456
for tid := range fkChecksMap {
457457
tids = append(tids, tid)
458458
}
459-
// Sort by table id for explain result stable.
460-
sort.Slice(tids, func(i, j int) bool {
461-
return tids[i] < tids[j]
462-
})
459+
// sort by table id for explain result stable.
460+
slices.Sort(tids)
463461
for i, tid := range tids {
464462
target, childIdxs = f.flattenForeignKeyChecksAndCascades(childCtx, target, childIdxs, fkChecksMap[tid], nil, len(fkCascadesMap) == 0 && i == len(tids)-1)
465463
}
466464
tids = tids[:0]
467465
for tid := range fkCascadesMap {
468466
tids = append(tids, tid)
469467
}
470-
sort.Slice(tids, func(i, j int) bool {
471-
return tids[i] < tids[j]
472-
})
468+
slices.Sort(tids)
473469
for i, tid := range tids {
474470
target, childIdxs = f.flattenForeignKeyChecksAndCascades(childCtx, target, childIdxs, nil, fkCascadesMap[tid], i == len(tids)-1)
475471
}

pkg/planner/core/rule_join_reorder_greedy.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@
1515
package core
1616

1717
import (
18+
"cmp"
1819
"math"
19-
"sort"
20+
"slices"
2021

2122
"github.com/pingcap/tidb/pkg/expression"
2223
)
@@ -57,8 +58,8 @@ func (s *joinReorderGreedySolver) solve(joinNodePlans []LogicalPlan, tracer *joi
5758
}
5859
}
5960
// Sort plans by cost
60-
sort.SliceStable(s.curJoinGroup, func(i, j int) bool {
61-
return s.curJoinGroup[i].cumCost < s.curJoinGroup[j].cumCost
61+
slices.SortStableFunc(s.curJoinGroup, func(i, j *jrNode) int {
62+
return cmp.Compare(i.cumCost, j.cumCost)
6263
})
6364

6465
// joinNodeNum indicates the number of join nodes except leading join nodes in the current join group

0 commit comments

Comments
 (0)