Skip to content

Commit

Permalink
planner: use slices to eliminate branch check (#50911)
Browse files Browse the repository at this point in the history
ref #47275
  • Loading branch information
hawkingrei authored Feb 1, 2024
1 parent 9bad202 commit b289dc9
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 11 deletions.
12 changes: 4 additions & 8 deletions pkg/planner/core/flat_plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ package core

import (
"fmt"
"sort"
"slices"

"github.com/pingcap/tidb/pkg/kv"
"github.com/pingcap/tidb/pkg/util/logutil"
Expand Down Expand Up @@ -456,20 +456,16 @@ func (f *FlatPhysicalPlan) flattenForeignKeyChecksAndCascadesMap(childCtx *opera
for tid := range fkChecksMap {
tids = append(tids, tid)
}
// Sort by table id for explain result stable.
sort.Slice(tids, func(i, j int) bool {
return tids[i] < tids[j]
})
// sort by table id for explain result stable.
slices.Sort(tids)
for i, tid := range tids {
target, childIdxs = f.flattenForeignKeyChecksAndCascades(childCtx, target, childIdxs, fkChecksMap[tid], nil, len(fkCascadesMap) == 0 && i == len(tids)-1)
}
tids = tids[:0]
for tid := range fkCascadesMap {
tids = append(tids, tid)
}
sort.Slice(tids, func(i, j int) bool {
return tids[i] < tids[j]
})
slices.Sort(tids)
for i, tid := range tids {
target, childIdxs = f.flattenForeignKeyChecksAndCascades(childCtx, target, childIdxs, nil, fkCascadesMap[tid], i == len(tids)-1)
}
Expand Down
7 changes: 4 additions & 3 deletions pkg/planner/core/rule_join_reorder_greedy.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@
package core

import (
"cmp"
"math"
"sort"
"slices"

"github.com/pingcap/tidb/pkg/expression"
)
Expand Down Expand Up @@ -57,8 +58,8 @@ func (s *joinReorderGreedySolver) solve(joinNodePlans []LogicalPlan, tracer *joi
}
}
// Sort plans by cost
sort.SliceStable(s.curJoinGroup, func(i, j int) bool {
return s.curJoinGroup[i].cumCost < s.curJoinGroup[j].cumCost
slices.SortStableFunc(s.curJoinGroup, func(i, j *jrNode) int {
return cmp.Compare(i.cumCost, j.cumCost)
})

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

0 comments on commit b289dc9

Please sign in to comment.