Skip to content

all: replace sort.Slice with slices.Sort #69263

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions src/cmd/compile/internal/compare/compare.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"cmd/compile/internal/types"
"fmt"
"math/bits"
"sort"
"slices"
)

// IsRegularMemory reports whether t can be compared/hashed as regular memory.
Expand Down Expand Up @@ -236,8 +236,14 @@ func EqStruct(t *types.Type, np, nq ir.Node) ([]ir.Node, bool) {
isCall := func(n ir.Node) bool {
return n.Op() == ir.OCALL || n.Op() == ir.OCALLFUNC
}
sort.SliceStable(c, func(i, j int) bool {
return !isCall(c[i]) && isCall(c[j])
slices.SortStableFunc(c, func(a, b ir.Node) int {
if isCall(a) && !isCall(b) {
return +1
}
if !isCall(a) && isCall(b) {
return -1
}
return 0
})
flatConds = append(flatConds, c...)
}
Expand Down
6 changes: 3 additions & 3 deletions src/cmd/compile/internal/dwarfgen/scope_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"os"
"path/filepath"
"runtime"
"sort"
"slices"
"strconv"
"strings"
"testing"
Expand Down Expand Up @@ -400,8 +400,8 @@ func readScope(ctxt *scopexplainContext, scope *lexblock, entry *dwarf.Entry) {
}
switch e.Tag {
case 0:
sort.Slice(scope.vars, func(i, j int) bool {
return scope.vars[i].expr < scope.vars[j].expr
slices.SortFunc(scope.vars, func(a, b variable) int {
return strings.Compare(a.expr, b.expr)
})
return
case dwarf.TagFormalParameter:
Expand Down
7 changes: 4 additions & 3 deletions src/cmd/compile/internal/gc/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
package gc

import (
"cmp"
"internal/race"
"math/rand"
"sort"
"slices"
"sync"

"cmd/compile/internal/base"
Expand Down Expand Up @@ -131,8 +132,8 @@ func compileFunctions(profile *pgoir.Profile) {
// Compile the longest functions first,
// since they're most likely to be the slowest.
// This helps avoid stragglers.
sort.Slice(compilequeue, func(i, j int) bool {
return len(compilequeue[i].Body) > len(compilequeue[j].Body)
slices.SortFunc(compilequeue, func(a, b *ir.Func) int {
return cmp.Compare(len(a.Body), len(b.Body))
})
}

Expand Down
11 changes: 6 additions & 5 deletions src/cmd/compile/internal/inline/inlheur/analyze.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@ import (
"cmd/compile/internal/base"
"cmd/compile/internal/ir"
"cmd/compile/internal/types"
"cmp"
"encoding/json"
"fmt"
"internal/buildcfg"
"io"
"os"
"path/filepath"
"sort"
"slices"
"strings"
)

Expand Down Expand Up @@ -349,11 +350,11 @@ func dumpFnPreamble(w io.Writer, funcInlHeur *fnInlHeur, ecst encodedCallSiteTab
// sortFnInlHeurSlice sorts a slice of fnInlHeur based on
// the starting line of the function definition, then by name.
func sortFnInlHeurSlice(sl []fnInlHeur) []fnInlHeur {
sort.SliceStable(sl, func(i, j int) bool {
if sl[i].line != sl[j].line {
return sl[i].line < sl[j].line
slices.SortStableFunc(sl, func(i, j fnInlHeur) int {
if r := cmp.Compare(i.line, j.line); r != 0 {
return r
}
return sl[i].fname < sl[j].fname
return strings.Compare(i.fname, j.fname)
})
return sl
}
Expand Down
27 changes: 14 additions & 13 deletions src/cmd/compile/internal/inline/inlheur/scoring.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ import (
"cmd/compile/internal/ir"
"cmd/compile/internal/pgoir"
"cmd/compile/internal/types"
"cmp"
"fmt"
"os"
"sort"
"slices"
"strconv"
"strings"
)
Expand Down Expand Up @@ -504,8 +505,8 @@ func (csa *callSiteAnalyzer) scoreCallsRegion(fn *ir.Func, region ir.Nodes, csta
csl = append(csl, cs)
}
scoreCallsCache.csl = csl[:0]
sort.Slice(csl, func(i, j int) bool {
return csl[i].ID < csl[j].ID
slices.SortFunc(csl, func(a, b *CallSite) int {
return cmp.Compare(a.ID, b.ID)
})

// Score each call site.
Expand Down Expand Up @@ -700,18 +701,18 @@ func DumpInlCallSiteScores(profile *pgoir.Profile, budgetCallback func(fn *ir.Fu
for _, cs := range allCallSites {
sl = append(sl, cs)
}
sort.Slice(sl, func(i, j int) bool {
if sl[i].Score != sl[j].Score {
return sl[i].Score < sl[j].Score
slices.SortFunc(sl, func(a, b *CallSite) int {
if a.Score != b.Score {
return cmp.Compare(a.Score, b.Score)
}
fni := ir.PkgFuncName(sl[i].Callee)
fnj := ir.PkgFuncName(sl[j].Callee)
if fni != fnj {
return fni < fnj
fni := ir.PkgFuncName(a.Callee)
fnj := ir.PkgFuncName(b.Callee)
if r := strings.Compare(fni, fnj); r != 0 {
return r
}
ecsi := EncodeCallSiteKey(sl[i])
ecsj := EncodeCallSiteKey(sl[j])
return ecsi < ecsj
ecsi := EncodeCallSiteKey(a)
ecsj := EncodeCallSiteKey(b)
return strings.Compare(ecsi, ecsj)
})

mkname := func(fn *ir.Func) string {
Expand Down
6 changes: 3 additions & 3 deletions src/cmd/compile/internal/ir/mknode.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
"io/fs"
"log"
"os"
"sort"
"slices"
"strings"
)

Expand Down Expand Up @@ -143,8 +143,8 @@ func main() {
}
}
// Sort for deterministic output.
sort.Slice(concreteNodes, func(i, j int) bool {
return concreteNodes[i].Name.Name < concreteNodes[j].Name.Name
slices.SortFunc(concreteNodes, func(a, b *ast.TypeSpec) int {
return strings.Compare(a.Name.Name, b.Name.Name)
})
// Generate code for each concrete type.
for _, t := range concreteNodes {
Expand Down
51 changes: 26 additions & 25 deletions src/cmd/compile/internal/liveness/mergelocals.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ import (
"cmd/compile/internal/ir"
"cmd/compile/internal/ssa"
"cmd/internal/src"
"cmp"
"fmt"
"os"
"path/filepath"
"sort"
"slices"
"strings"
)

Expand Down Expand Up @@ -160,8 +161,8 @@ func (mls *MergeLocalsState) Followers(n *ir.Name, tmp []*ir.Name) []*ir.Name {
for _, k := range sl[1:] {
tmp = append(tmp, mls.vars[k])
}
sort.SliceStable(tmp, func(i, j int) bool {
return tmp[i].Sym().Name < tmp[j].Sym().Name
slices.SortStableFunc(tmp, func(a, b *ir.Name) int {
return strings.Compare(a.Sym().Name, b.Sym().Name)
})
return tmp
}
Expand Down Expand Up @@ -268,8 +269,8 @@ func (mls *MergeLocalsState) String() string {
leaders = append(leaders, n)
}
}
sort.Slice(leaders, func(i, j int) bool {
return leaders[i].Sym().Name < leaders[j].Sym().Name
slices.SortFunc(leaders, func(a, b *ir.Name) int {
return strings.Compare(a.Sym().Name, b.Sym().Name)
})
var sb strings.Builder
for _, n := range leaders {
Expand Down Expand Up @@ -312,9 +313,7 @@ func (cs *cstate) collectMergeCandidates() {
}

// Sort by pointerness, size, and then name.
sort.SliceStable(cands, func(i, j int) bool {
return nameLess(cands[i], cands[j])
})
slices.SortStableFunc(cands, nameCmp)

if cs.trace > 1 {
fmt.Fprintf(os.Stderr, "=-= raw cand list for func %v:\n", cs.fn)
Expand Down Expand Up @@ -580,7 +579,7 @@ func (cs *cstate) populateIndirectUseTable(cands []*ir.Name) ([]*ir.Name, []cand
for k := range indirectUE {
ids = append(ids, k)
}
sort.Slice(ids, func(i, j int) bool { return ids[i] < ids[j] })
slices.Sort(ids)
for _, id := range ids {
fmt.Fprintf(os.Stderr, " v%d:", id)
for _, n := range indirectUE[id] {
Expand All @@ -594,9 +593,7 @@ func (cs *cstate) populateIndirectUseTable(cands []*ir.Name) ([]*ir.Name, []cand
for k := range rawcands {
pruned = append(pruned, k)
}
sort.Slice(pruned, func(i, j int) bool {
return nameLess(pruned[i], pruned[j])
})
slices.SortFunc(pruned, nameCmp)
var regions []candRegion
pruned, regions = cs.genRegions(pruned)
if len(pruned) < 2 {
Expand All @@ -610,13 +607,13 @@ type nameCount struct {
count int32
}

// nameLess compares ci with cj to see if ci should be less than cj in
// a relative ordering of candidate variables. This is used to sort
// vars by pointerness (variables with pointers first), then in order
// nameCmp compares ci with cj in a relative ordering
// of candidate variables. This is used to sort vars
// by pointerness (variables with pointers first), then in order
// of decreasing alignment, then by decreasing size. We are assuming a
// merging algorithm that merges later entries in the list into
// earlier entries. An example ordered candidate list produced by
// nameLess:
// nameCmp:
//
// idx name type align size
// 0: abc [10]*int 8 80
Expand All @@ -625,20 +622,24 @@ type nameCount struct {
// 3: tuv [9]int 8 72
// 4: wxy [9]int32 4 36
// 5: jkl [8]int32 4 32
func nameLess(ci, cj *ir.Name) bool {
func nameCmp(ci, cj *ir.Name) int {
if ci.Type().HasPointers() != cj.Type().HasPointers() {
return ci.Type().HasPointers()
if ci.Type().HasPointers() {
return -1
}
return +1
}
if ci.Type().Alignment() != cj.Type().Alignment() {
return cj.Type().Alignment() < ci.Type().Alignment()
if r := cmp.Compare(cj.Type().Alignment(), ci.Type().Alignment()); r != 0 {
return r
}
if ci.Type().Size() != cj.Type().Size() {
return cj.Type().Size() < ci.Type().Size()
if r := cmp.Compare(cj.Type().Size(), ci.Type().Size()); r != 0 {
return r
}
if ci.Sym().Name != cj.Sym().Name {
return ci.Sym().Name < cj.Sym().Name
if r := strings.Compare(ci.Sym().Name, cj.Sym().Name); r != 0 {
return r
}
return fmt.Sprintf("%v", ci.Pos()) < fmt.Sprintf("%v", cj.Pos())

return ci.Pos().Compare(cj.Pos())
}

// nextRegion starts at location idx and walks forward in the cands
Expand Down
6 changes: 5 additions & 1 deletion src/cmd/compile/internal/liveness/plive.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@
package liveness

import (
"cmp"
"fmt"
"os"
"slices"
"sort"
"strings"

Expand Down Expand Up @@ -1445,7 +1447,9 @@ func (lv *liveness) emitStackObjects() *obj.LSym {
}

// Sort variables from lowest to highest address.
sort.Slice(vars, func(i, j int) bool { return vars[i].FrameOffset() < vars[j].FrameOffset() })
slices.SortFunc(vars, func(a, b *ir.Name) int {
return cmp.Compare(a.FrameOffset(), b.FrameOffset())
})

// Populate the stack object data.
// Format must match runtime/stack.go:stackObjectRecord.
Expand Down
7 changes: 3 additions & 4 deletions src/cmd/compile/internal/noder/irgen.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"internal/buildcfg"
"internal/types/errors"
"regexp"
"sort"
"slices"

"cmd/compile/internal/base"
"cmd/compile/internal/rangefunc"
Expand Down Expand Up @@ -134,9 +134,8 @@ func checkFiles(m posMap, noders []*noder) (*types2.Package, *types2.Info, map[*
}
}
}
sort.Slice(nihTargs, func(i, j int) bool {
ti, tj := nihTargs[i], nihTargs[j]
return ti.pos.Before(tj.pos)
slices.SortFunc(nihTargs, func(a, b nihTarg) int {
return a.pos.Compare(b.pos)
})
for _, targ := range nihTargs {
base.ErrorfAt(targ.pos, 0, "cannot use incomplete (or unallocatable) type as a type argument: %v", targ.typ)
Expand Down
9 changes: 6 additions & 3 deletions src/cmd/compile/internal/noder/unified.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@
package noder

import (
"cmp"
"fmt"
"internal/buildcfg"
"internal/pkgbits"
"internal/types/errors"
"io"
"runtime"
"sort"
"slices"
"strings"

"cmd/compile/internal/base"
Expand Down Expand Up @@ -519,7 +520,7 @@ func writeUnifiedExport(out io.Writer) {
for _, idx := range l.decls {
idxs = append(idxs, idx)
}
sort.Slice(idxs, func(i, j int) bool { return idxs[i] < idxs[j] })
slices.Sort(idxs)

w := publicRootWriter

Expand Down Expand Up @@ -553,7 +554,9 @@ func writeUnifiedExport(out io.Writer) {
for sym, idx := range l.bodies {
bodies = append(bodies, symIdx{sym, idx})
}
sort.Slice(bodies, func(i, j int) bool { return bodies[i].idx < bodies[j].idx })
slices.SortFunc(bodies, func(a, b symIdx) int {
return cmp.Compare(a.idx, b.idx)
})

w := privateRootWriter

Expand Down
Loading