Skip to content

Commit 11157cc

Browse files
committed
all: replace sort.Slice with slices.Sort
1 parent 2707d42 commit 11157cc

File tree

60 files changed

+491
-343
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

60 files changed

+491
-343
lines changed

src/cmd/compile/internal/compare/compare.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313
"cmd/compile/internal/types"
1414
"fmt"
1515
"math/bits"
16-
"sort"
16+
"slices"
1717
)
1818

1919
// IsRegularMemory reports whether t can be compared/hashed as regular memory.
@@ -236,8 +236,14 @@ func EqStruct(t *types.Type, np, nq ir.Node) ([]ir.Node, bool) {
236236
isCall := func(n ir.Node) bool {
237237
return n.Op() == ir.OCALL || n.Op() == ir.OCALLFUNC
238238
}
239-
sort.SliceStable(c, func(i, j int) bool {
240-
return !isCall(c[i]) && isCall(c[j])
239+
slices.SortStableFunc(c, func(a, b ir.Node) int {
240+
if isCall(a) && !isCall(b) {
241+
return +1
242+
}
243+
if !isCall(a) && isCall(b) {
244+
return -1
245+
}
246+
return 0
241247
})
242248
flatConds = append(flatConds, c...)
243249
}

src/cmd/compile/internal/dwarfgen/scope_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
"os"
1313
"path/filepath"
1414
"runtime"
15-
"sort"
15+
"slices"
1616
"strconv"
1717
"strings"
1818
"testing"
@@ -400,8 +400,8 @@ func readScope(ctxt *scopexplainContext, scope *lexblock, entry *dwarf.Entry) {
400400
}
401401
switch e.Tag {
402402
case 0:
403-
sort.Slice(scope.vars, func(i, j int) bool {
404-
return scope.vars[i].expr < scope.vars[j].expr
403+
slices.SortFunc(scope.vars, func(a, b variable) int {
404+
return strings.Compare(a.expr, b.expr)
405405
})
406406
return
407407
case dwarf.TagFormalParameter:

src/cmd/compile/internal/gc/compile.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@
55
package gc
66

77
import (
8+
"cmp"
89
"internal/race"
910
"math/rand"
10-
"sort"
11+
"slices"
1112
"sync"
1213

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

src/cmd/compile/internal/inline/inlheur/analyze.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,14 @@ import (
88
"cmd/compile/internal/base"
99
"cmd/compile/internal/ir"
1010
"cmd/compile/internal/types"
11+
"cmp"
1112
"encoding/json"
1213
"fmt"
1314
"internal/buildcfg"
1415
"io"
1516
"os"
1617
"path/filepath"
17-
"sort"
18+
"slices"
1819
"strings"
1920
)
2021

@@ -349,11 +350,11 @@ func dumpFnPreamble(w io.Writer, funcInlHeur *fnInlHeur, ecst encodedCallSiteTab
349350
// sortFnInlHeurSlice sorts a slice of fnInlHeur based on
350351
// the starting line of the function definition, then by name.
351352
func sortFnInlHeurSlice(sl []fnInlHeur) []fnInlHeur {
352-
sort.SliceStable(sl, func(i, j int) bool {
353-
if sl[i].line != sl[j].line {
354-
return sl[i].line < sl[j].line
353+
slices.SortStableFunc(sl, func(i, j fnInlHeur) int {
354+
if r := cmp.Compare(i.line, j.line); r != 0 {
355+
return r
355356
}
356-
return sl[i].fname < sl[j].fname
357+
return strings.Compare(i.fname, j.fname)
357358
})
358359
return sl
359360
}

src/cmd/compile/internal/inline/inlheur/scoring.go

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@ import (
99
"cmd/compile/internal/ir"
1010
"cmd/compile/internal/pgoir"
1111
"cmd/compile/internal/types"
12+
"cmp"
1213
"fmt"
1314
"os"
14-
"sort"
15+
"slices"
1516
"strconv"
1617
"strings"
1718
)
@@ -504,8 +505,8 @@ func (csa *callSiteAnalyzer) scoreCallsRegion(fn *ir.Func, region ir.Nodes, csta
504505
csl = append(csl, cs)
505506
}
506507
scoreCallsCache.csl = csl[:0]
507-
sort.Slice(csl, func(i, j int) bool {
508-
return csl[i].ID < csl[j].ID
508+
slices.SortFunc(csl, func(a, b *CallSite) int {
509+
return cmp.Compare(a.ID, b.ID)
509510
})
510511

511512
// Score each call site.
@@ -700,18 +701,18 @@ func DumpInlCallSiteScores(profile *pgoir.Profile, budgetCallback func(fn *ir.Fu
700701
for _, cs := range allCallSites {
701702
sl = append(sl, cs)
702703
}
703-
sort.Slice(sl, func(i, j int) bool {
704-
if sl[i].Score != sl[j].Score {
705-
return sl[i].Score < sl[j].Score
704+
slices.SortFunc(sl, func(a, b *CallSite) int {
705+
if a.Score != b.Score {
706+
return cmp.Compare(a.Score, b.Score)
706707
}
707-
fni := ir.PkgFuncName(sl[i].Callee)
708-
fnj := ir.PkgFuncName(sl[j].Callee)
709-
if fni != fnj {
710-
return fni < fnj
708+
fni := ir.PkgFuncName(a.Callee)
709+
fnj := ir.PkgFuncName(b.Callee)
710+
if r := strings.Compare(fni, fnj); r != 0 {
711+
return r
711712
}
712-
ecsi := EncodeCallSiteKey(sl[i])
713-
ecsj := EncodeCallSiteKey(sl[j])
714-
return ecsi < ecsj
713+
ecsi := EncodeCallSiteKey(a)
714+
ecsj := EncodeCallSiteKey(b)
715+
return strings.Compare(ecsi, ecsj)
715716
})
716717

717718
mkname := func(fn *ir.Func) string {

src/cmd/compile/internal/ir/mknode.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import (
1919
"io/fs"
2020
"log"
2121
"os"
22-
"sort"
22+
"slices"
2323
"strings"
2424
)
2525

@@ -143,8 +143,8 @@ func main() {
143143
}
144144
}
145145
// Sort for deterministic output.
146-
sort.Slice(concreteNodes, func(i, j int) bool {
147-
return concreteNodes[i].Name.Name < concreteNodes[j].Name.Name
146+
slices.SortFunc(concreteNodes, func(a, b *ast.TypeSpec) int {
147+
return strings.Compare(a.Name.Name, b.Name.Name)
148148
})
149149
// Generate code for each concrete type.
150150
for _, t := range concreteNodes {

src/cmd/compile/internal/liveness/mergelocals.go

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,11 @@ import (
1010
"cmd/compile/internal/ir"
1111
"cmd/compile/internal/ssa"
1212
"cmd/internal/src"
13+
"cmp"
1314
"fmt"
1415
"os"
1516
"path/filepath"
16-
"sort"
17+
"slices"
1718
"strings"
1819
)
1920

@@ -160,8 +161,8 @@ func (mls *MergeLocalsState) Followers(n *ir.Name, tmp []*ir.Name) []*ir.Name {
160161
for _, k := range sl[1:] {
161162
tmp = append(tmp, mls.vars[k])
162163
}
163-
sort.SliceStable(tmp, func(i, j int) bool {
164-
return tmp[i].Sym().Name < tmp[j].Sym().Name
164+
slices.SortStableFunc(tmp, func(a, b *ir.Name) int {
165+
return strings.Compare(a.Sym().Name, b.Sym().Name)
165166
})
166167
return tmp
167168
}
@@ -268,8 +269,8 @@ func (mls *MergeLocalsState) String() string {
268269
leaders = append(leaders, n)
269270
}
270271
}
271-
sort.Slice(leaders, func(i, j int) bool {
272-
return leaders[i].Sym().Name < leaders[j].Sym().Name
272+
slices.SortFunc(leaders, func(a, b *ir.Name) int {
273+
return strings.Compare(a.Sym().Name, b.Sym().Name)
273274
})
274275
var sb strings.Builder
275276
for _, n := range leaders {
@@ -312,9 +313,7 @@ func (cs *cstate) collectMergeCandidates() {
312313
}
313314

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

319318
if cs.trace > 1 {
320319
fmt.Fprintf(os.Stderr, "=-= raw cand list for func %v:\n", cs.fn)
@@ -580,7 +579,7 @@ func (cs *cstate) populateIndirectUseTable(cands []*ir.Name) ([]*ir.Name, []cand
580579
for k := range indirectUE {
581580
ids = append(ids, k)
582581
}
583-
sort.Slice(ids, func(i, j int) bool { return ids[i] < ids[j] })
582+
slices.Sort(ids)
584583
for _, id := range ids {
585584
fmt.Fprintf(os.Stderr, " v%d:", id)
586585
for _, n := range indirectUE[id] {
@@ -594,9 +593,7 @@ func (cs *cstate) populateIndirectUseTable(cands []*ir.Name) ([]*ir.Name, []cand
594593
for k := range rawcands {
595594
pruned = append(pruned, k)
596595
}
597-
sort.Slice(pruned, func(i, j int) bool {
598-
return nameLess(pruned[i], pruned[j])
599-
})
596+
slices.SortFunc(pruned, nameCmp)
600597
var regions []candRegion
601598
pruned, regions = cs.genRegions(pruned)
602599
if len(pruned) < 2 {
@@ -610,13 +607,13 @@ type nameCount struct {
610607
count int32
611608
}
612609

613-
// nameLess compares ci with cj to see if ci should be less than cj in
614-
// a relative ordering of candidate variables. This is used to sort
615-
// vars by pointerness (variables with pointers first), then in order
610+
// nameCmp compares ci with cj in a relative ordering
611+
// of candidate variables. This is used to sort vars
612+
// by pointerness (variables with pointers first), then in order
616613
// of decreasing alignment, then by decreasing size. We are assuming a
617614
// merging algorithm that merges later entries in the list into
618615
// earlier entries. An example ordered candidate list produced by
619-
// nameLess:
616+
// nameCmp:
620617
//
621618
// idx name type align size
622619
// 0: abc [10]*int 8 80
@@ -625,20 +622,24 @@ type nameCount struct {
625622
// 3: tuv [9]int 8 72
626623
// 4: wxy [9]int32 4 36
627624
// 5: jkl [8]int32 4 32
628-
func nameLess(ci, cj *ir.Name) bool {
625+
func nameCmp(ci, cj *ir.Name) int {
629626
if ci.Type().HasPointers() != cj.Type().HasPointers() {
630-
return ci.Type().HasPointers()
627+
if ci.Type().HasPointers() {
628+
return -1
629+
}
630+
return +1
631631
}
632-
if ci.Type().Alignment() != cj.Type().Alignment() {
633-
return cj.Type().Alignment() < ci.Type().Alignment()
632+
if r := cmp.Compare(cj.Type().Alignment(), ci.Type().Alignment()); r != 0 {
633+
return r
634634
}
635-
if ci.Type().Size() != cj.Type().Size() {
636-
return cj.Type().Size() < ci.Type().Size()
635+
if r := cmp.Compare(cj.Type().Size(), ci.Type().Size()); r != 0 {
636+
return r
637637
}
638-
if ci.Sym().Name != cj.Sym().Name {
639-
return ci.Sym().Name < cj.Sym().Name
638+
if r := strings.Compare(ci.Sym().Name, cj.Sym().Name); r != 0 {
639+
return r
640640
}
641-
return fmt.Sprintf("%v", ci.Pos()) < fmt.Sprintf("%v", cj.Pos())
641+
642+
return ci.Pos().Compare(cj.Pos())
642643
}
643644

644645
// nextRegion starts at location idx and walks forward in the cands

src/cmd/compile/internal/liveness/plive.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,10 @@
1515
package liveness
1616

1717
import (
18+
"cmp"
1819
"fmt"
1920
"os"
21+
"slices"
2022
"sort"
2123
"strings"
2224

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

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

14501454
// Populate the stack object data.
14511455
// Format must match runtime/stack.go:stackObjectRecord.

src/cmd/compile/internal/noder/irgen.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
"internal/buildcfg"
1010
"internal/types/errors"
1111
"regexp"
12-
"sort"
12+
"slices"
1313

1414
"cmd/compile/internal/base"
1515
"cmd/compile/internal/rangefunc"
@@ -134,9 +134,8 @@ func checkFiles(m posMap, noders []*noder) (*types2.Package, *types2.Info, map[*
134134
}
135135
}
136136
}
137-
sort.Slice(nihTargs, func(i, j int) bool {
138-
ti, tj := nihTargs[i], nihTargs[j]
139-
return ti.pos.Before(tj.pos)
137+
slices.SortFunc(nihTargs, func(a, b nihTarg) int {
138+
return a.pos.Compare(b.pos)
140139
})
141140
for _, targ := range nihTargs {
142141
base.ErrorfAt(targ.pos, 0, "cannot use incomplete (or unallocatable) type as a type argument: %v", targ.typ)

src/cmd/compile/internal/noder/unified.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,14 @@
55
package noder
66

77
import (
8+
"cmp"
89
"fmt"
910
"internal/buildcfg"
1011
"internal/pkgbits"
1112
"internal/types/errors"
1213
"io"
1314
"runtime"
14-
"sort"
15+
"slices"
1516
"strings"
1617

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

524525
w := publicRootWriter
525526

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

558561
w := privateRootWriter
559562

0 commit comments

Comments
 (0)