Skip to content

Commit f15095f

Browse files
cuonglmgopherbot
authored andcommitted
cmd/compile: use slices.{Sort,SortFunc}
Now that we're bootstrapping from a toolchain that has the slices package. Updates #64751 Change-Id: I2e63d95577d058670d3dc75bd45d6e050c6f0e25 Reviewed-on: https://go-review.googlesource.com/c/go/+/610601 Reviewed-by: Cherry Mui <[email protected]> Reviewed-by: Dmitri Shuralyov <[email protected]> Auto-Submit: Cuong Manh Le <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]>
1 parent 634363e commit f15095f

File tree

15 files changed

+83
-67
lines changed

15 files changed

+83
-67
lines changed

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,15 @@
55
package dwarfgen
66

77
import (
8+
"cmp"
89
"debug/dwarf"
910
"fmt"
1011
"internal/platform"
1112
"internal/testenv"
1213
"os"
1314
"path/filepath"
1415
"runtime"
15-
"sort"
16+
"slices"
1617
"strconv"
1718
"strings"
1819
"testing"
@@ -400,8 +401,8 @@ func readScope(ctxt *scopexplainContext, scope *lexblock, entry *dwarf.Entry) {
400401
}
401402
switch e.Tag {
402403
case 0:
403-
sort.Slice(scope.vars, func(i, j int) bool {
404-
return scope.vars[i].expr < scope.vars[j].expr
404+
slices.SortFunc(scope.vars, func(a, b variable) int {
405+
return cmp.Compare(a.expr, b.expr)
405406
})
406407
return
407408
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(b.Body), len(a.Body))
136137
})
137138
}
138139

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

Lines changed: 13 additions & 12 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)
708+
fni := ir.PkgFuncName(a.Callee)
709+
fnj := ir.PkgFuncName(b.Callee)
709710
if fni != fnj {
710-
return fni < fnj
711+
return cmp.Compare(fni, fnj)
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 cmp.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: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"fmt"
1414
"os"
1515
"path/filepath"
16+
"slices"
1617
"sort"
1718
"strings"
1819
)
@@ -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 {
@@ -580,7 +581,7 @@ func (cs *cstate) populateIndirectUseTable(cands []*ir.Name) ([]*ir.Name, []cand
580581
for k := range indirectUE {
581582
ids = append(ids, k)
582583
}
583-
sort.Slice(ids, func(i, j int) bool { return ids[i] < ids[j] })
584+
slices.Sort(ids)
584585
for _, id := range ids {
585586
fmt.Fprintf(os.Stderr, " v%d:", id)
586587
for _, n := range indirectUE[id] {

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

Lines changed: 3 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,7 @@ 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 { return cmp.Compare(a.FrameOffset(), b.FrameOffset()) })
14491451

14501452
// Populate the stack object data.
14511453
// Format must match runtime/stack.go:stackObjectRecord.

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

Lines changed: 4 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,7 @@ 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 { return cmp.Compare(a.idx, b.idx) })
557558

558559
w := privateRootWriter
559560

src/cmd/compile/internal/ssa/debug_lines_test.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package ssa_test
77
import (
88
"bufio"
99
"bytes"
10+
"cmp"
1011
"flag"
1112
"fmt"
1213
"internal/testenv"
@@ -15,7 +16,7 @@ import (
1516
"reflect"
1617
"regexp"
1718
"runtime"
18-
"sort"
19+
"slices"
1920
"strconv"
2021
"strings"
2122
"testing"
@@ -167,16 +168,16 @@ func compileAndDump(t *testing.T, file, function, moreGCFlags string) []byte {
167168
}
168169

169170
func sortInlineStacks(x [][]int) {
170-
sort.Slice(x, func(i, j int) bool {
171-
if len(x[i]) != len(x[j]) {
172-
return len(x[i]) < len(x[j])
171+
slices.SortFunc(x, func(a, b []int) int {
172+
if len(a) != len(b) {
173+
return cmp.Compare(len(a), len(b))
173174
}
174-
for k := range x[i] {
175-
if x[i][k] != x[j][k] {
176-
return x[i][k] < x[j][k]
175+
for k := range a {
176+
if a[k] != b[k] {
177+
return cmp.Compare(a[k], b[k])
177178
}
178179
}
179-
return false
180+
return 0
180181
})
181182
}
182183

src/cmd/compile/internal/ssa/decompose.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ package ssa
66

77
import (
88
"cmd/compile/internal/types"
9-
"sort"
9+
"cmp"
10+
"slices"
1011
)
1112

1213
// decompose converts phi ops on compound builtin types into phi
@@ -433,12 +434,11 @@ type namedVal struct {
433434
// removes all values with OpInvalid, and re-sorts the list of Names.
434435
func deleteNamedVals(f *Func, toDelete []namedVal) {
435436
// Arrange to delete from larger indices to smaller, to ensure swap-with-end deletion does not invalidate pending indices.
436-
sort.Slice(toDelete, func(i, j int) bool {
437-
if toDelete[i].locIndex != toDelete[j].locIndex {
438-
return toDelete[i].locIndex > toDelete[j].locIndex
437+
slices.SortFunc(toDelete, func(a, b namedVal) int {
438+
if a.locIndex != b.locIndex {
439+
return cmp.Compare(b.locIndex, a.locIndex)
439440
}
440-
return toDelete[i].valIndex > toDelete[j].valIndex
441-
441+
return cmp.Compare(b.valIndex, a.valIndex)
442442
})
443443

444444
// Get rid of obsolete names

src/cmd/compile/internal/ssa/memcombine.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ import (
88
"cmd/compile/internal/base"
99
"cmd/compile/internal/types"
1010
"cmd/internal/src"
11-
"sort"
11+
"cmp"
12+
"slices"
1213
)
1314

1415
// memcombine combines smaller loads and stores into larger ones.
@@ -232,8 +233,8 @@ func combineLoads(root *Value, n int64) bool {
232233
}
233234

234235
// Sort in memory address order.
235-
sort.Slice(r, func(i, j int) bool {
236-
return r[i].offset < r[j].offset
236+
slices.SortFunc(r, func(a, b LoadRecord) int {
237+
return cmp.Compare(a.offset, b.offset)
237238
})
238239

239240
// Check that we have contiguous offsets.
@@ -516,8 +517,8 @@ func combineStores(root *Value, n int64) bool {
516517
pos := a[n-1].store.Pos
517518

518519
// Sort stores in increasing address order.
519-
sort.Slice(a, func(i, j int) bool {
520-
return a[i].offset < a[j].offset
520+
slices.SortFunc(a, func(sr1, sr2 StoreRecord) int {
521+
return cmp.Compare(sr1.offset, sr2.offset)
521522
})
522523

523524
// Check that everything is written to sequential locations.

src/cmd/compile/internal/ssa/schedule.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package ssa
77
import (
88
"cmd/compile/internal/base"
99
"cmd/compile/internal/types"
10+
"cmp"
1011
"container/heap"
1112
"slices"
1213
"sort"
@@ -261,8 +262,8 @@ func schedule(f *Func) {
261262
}
262263

263264
// Sort all the edges by source Value ID.
264-
sort.Slice(edges, func(i, j int) bool {
265-
return edges[i].x.ID < edges[j].x.ID
265+
slices.SortFunc(edges, func(a, b edge) int {
266+
return cmp.Compare(a.x.ID, b.x.ID)
266267
})
267268
// Compute inEdges for values in this block.
268269
for _, e := range edges {

src/cmd/compile/internal/ssa/stmtlines_test.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package ssa_test
77
import (
88
cmddwarf "cmd/internal/dwarf"
99
"cmd/internal/quoted"
10+
"cmp"
1011
"debug/dwarf"
1112
"debug/elf"
1213
"debug/macho"
@@ -18,7 +19,8 @@ import (
1819
"io"
1920
"os"
2021
"runtime"
21-
"sort"
22+
"slices"
23+
"strings"
2224
"testing"
2325
)
2426

@@ -144,11 +146,11 @@ func TestStmtLines(t *testing.T) {
144146
}
145147
t.Logf("Saw %d out of %d lines without statement marks", len(nonStmtLines), len(lines))
146148
if testing.Verbose() {
147-
sort.Slice(nonStmtLines, func(i, j int) bool {
148-
if nonStmtLines[i].File != nonStmtLines[j].File {
149-
return nonStmtLines[i].File < nonStmtLines[j].File
149+
slices.SortFunc(nonStmtLines, func(a, b Line) int {
150+
if a.File != b.File {
151+
return strings.Compare(a.File, b.File)
150152
}
151-
return nonStmtLines[i].Line < nonStmtLines[j].Line
153+
return cmp.Compare(a.Line, b.Line)
152154
})
153155
for _, l := range nonStmtLines {
154156
t.Logf("%s:%d has no DWARF is_stmt mark\n", l.File, l.Line)

src/cmd/compile/internal/ssagen/pgen.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ import (
88
"fmt"
99
"internal/buildcfg"
1010
"os"
11+
"slices"
1112
"sort"
13+
"strings"
1214
"sync"
1315

1416
"cmd/compile/internal/base"
@@ -414,7 +416,7 @@ func fieldtrack(fnsym *obj.LSym, tracked map[*obj.LSym]struct{}) {
414416
for sym := range tracked {
415417
trackSyms = append(trackSyms, sym)
416418
}
417-
sort.Slice(trackSyms, func(i, j int) bool { return trackSyms[i].Name < trackSyms[j].Name })
419+
slices.SortFunc(trackSyms, func(a, b *obj.LSym) int { return strings.Compare(a.Name, b.Name) })
418420
for _, sym := range trackSyms {
419421
r := obj.Addrel(fnsym)
420422
r.Sym = sym

src/cmd/compile/internal/staticdata/data.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@ import (
1010
"go/constant"
1111
"io"
1212
"os"
13-
"sort"
13+
"slices"
1414
"strconv"
15+
"strings"
1516
"sync"
1617

1718
"cmd/compile/internal/base"
@@ -264,8 +265,8 @@ func GlobalLinksym(n *ir.Name) *obj.LSym {
264265
}
265266

266267
func WriteFuncSyms() {
267-
sort.Slice(funcsyms, func(i, j int) bool {
268-
return funcsyms[i].Linksym().Name < funcsyms[j].Linksym().Name
268+
slices.SortFunc(funcsyms, func(a, b *ir.Name) int {
269+
return strings.Compare(a.Linksym().Name, b.Linksym().Name)
269270
})
270271
for _, nam := range funcsyms {
271272
s := nam.Sym()

0 commit comments

Comments
 (0)