Skip to content

Commit

Permalink
cmd/compile: use slices.{Sort,SortFunc}
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
cuonglm authored and gopherbot committed Sep 5, 2024
1 parent 634363e commit f15095f
Show file tree
Hide file tree
Showing 15 changed files with 83 additions and 67 deletions.
7 changes: 4 additions & 3 deletions src/cmd/compile/internal/dwarfgen/scope_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@
package dwarfgen

import (
"cmp"
"debug/dwarf"
"fmt"
"internal/platform"
"internal/testenv"
"os"
"path/filepath"
"runtime"
"sort"
"slices"
"strconv"
"strings"
"testing"
Expand Down Expand Up @@ -400,8 +401,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 cmp.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(b.Body), len(a.Body))
})
}

Expand Down
25 changes: 13 additions & 12 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)
fni := ir.PkgFuncName(a.Callee)
fnj := ir.PkgFuncName(b.Callee)
if fni != fnj {
return fni < fnj
return cmp.Compare(fni, fnj)
}
ecsi := EncodeCallSiteKey(sl[i])
ecsj := EncodeCallSiteKey(sl[j])
return ecsi < ecsj
ecsi := EncodeCallSiteKey(a)
ecsj := EncodeCallSiteKey(b)
return cmp.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
7 changes: 4 additions & 3 deletions src/cmd/compile/internal/liveness/mergelocals.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"fmt"
"os"
"path/filepath"
"slices"
"sort"
"strings"
)
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 @@ -580,7 +581,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 Down
4 changes: 3 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,7 @@ 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: 4 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,7 @@ 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
17 changes: 9 additions & 8 deletions src/cmd/compile/internal/ssa/debug_lines_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package ssa_test
import (
"bufio"
"bytes"
"cmp"
"flag"
"fmt"
"internal/testenv"
Expand All @@ -15,7 +16,7 @@ import (
"reflect"
"regexp"
"runtime"
"sort"
"slices"
"strconv"
"strings"
"testing"
Expand Down Expand Up @@ -167,16 +168,16 @@ func compileAndDump(t *testing.T, file, function, moreGCFlags string) []byte {
}

func sortInlineStacks(x [][]int) {
sort.Slice(x, func(i, j int) bool {
if len(x[i]) != len(x[j]) {
return len(x[i]) < len(x[j])
slices.SortFunc(x, func(a, b []int) int {
if len(a) != len(b) {
return cmp.Compare(len(a), len(b))
}
for k := range x[i] {
if x[i][k] != x[j][k] {
return x[i][k] < x[j][k]
for k := range a {
if a[k] != b[k] {
return cmp.Compare(a[k], b[k])
}
}
return false
return 0
})
}

Expand Down
12 changes: 6 additions & 6 deletions src/cmd/compile/internal/ssa/decompose.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ package ssa

import (
"cmd/compile/internal/types"
"sort"
"cmp"
"slices"
)

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

return cmp.Compare(b.valIndex, a.valIndex)
})

// Get rid of obsolete names
Expand Down
11 changes: 6 additions & 5 deletions src/cmd/compile/internal/ssa/memcombine.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import (
"cmd/compile/internal/base"
"cmd/compile/internal/types"
"cmd/internal/src"
"sort"
"cmp"
"slices"
)

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

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

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

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

// Check that everything is written to sequential locations.
Expand Down
5 changes: 3 additions & 2 deletions src/cmd/compile/internal/ssa/schedule.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package ssa
import (
"cmd/compile/internal/base"
"cmd/compile/internal/types"
"cmp"
"container/heap"
"slices"
"sort"
Expand Down Expand Up @@ -261,8 +262,8 @@ func schedule(f *Func) {
}

// Sort all the edges by source Value ID.
sort.Slice(edges, func(i, j int) bool {
return edges[i].x.ID < edges[j].x.ID
slices.SortFunc(edges, func(a, b edge) int {
return cmp.Compare(a.x.ID, b.x.ID)
})
// Compute inEdges for values in this block.
for _, e := range edges {
Expand Down
12 changes: 7 additions & 5 deletions src/cmd/compile/internal/ssa/stmtlines_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package ssa_test
import (
cmddwarf "cmd/internal/dwarf"
"cmd/internal/quoted"
"cmp"
"debug/dwarf"
"debug/elf"
"debug/macho"
Expand All @@ -18,7 +19,8 @@ import (
"io"
"os"
"runtime"
"sort"
"slices"
"strings"
"testing"
)

Expand Down Expand Up @@ -144,11 +146,11 @@ func TestStmtLines(t *testing.T) {
}
t.Logf("Saw %d out of %d lines without statement marks", len(nonStmtLines), len(lines))
if testing.Verbose() {
sort.Slice(nonStmtLines, func(i, j int) bool {
if nonStmtLines[i].File != nonStmtLines[j].File {
return nonStmtLines[i].File < nonStmtLines[j].File
slices.SortFunc(nonStmtLines, func(a, b Line) int {
if a.File != b.File {
return strings.Compare(a.File, b.File)
}
return nonStmtLines[i].Line < nonStmtLines[j].Line
return cmp.Compare(a.Line, b.Line)
})
for _, l := range nonStmtLines {
t.Logf("%s:%d has no DWARF is_stmt mark\n", l.File, l.Line)
Expand Down
4 changes: 3 additions & 1 deletion src/cmd/compile/internal/ssagen/pgen.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ import (
"fmt"
"internal/buildcfg"
"os"
"slices"
"sort"
"strings"
"sync"

"cmd/compile/internal/base"
Expand Down Expand Up @@ -414,7 +416,7 @@ func fieldtrack(fnsym *obj.LSym, tracked map[*obj.LSym]struct{}) {
for sym := range tracked {
trackSyms = append(trackSyms, sym)
}
sort.Slice(trackSyms, func(i, j int) bool { return trackSyms[i].Name < trackSyms[j].Name })
slices.SortFunc(trackSyms, func(a, b *obj.LSym) int { return strings.Compare(a.Name, b.Name) })
for _, sym := range trackSyms {
r := obj.Addrel(fnsym)
r.Sym = sym
Expand Down
7 changes: 4 additions & 3 deletions src/cmd/compile/internal/staticdata/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ import (
"go/constant"
"io"
"os"
"sort"
"slices"
"strconv"
"strings"
"sync"

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

func WriteFuncSyms() {
sort.Slice(funcsyms, func(i, j int) bool {
return funcsyms[i].Linksym().Name < funcsyms[j].Linksym().Name
slices.SortFunc(funcsyms, func(a, b *ir.Name) int {
return strings.Compare(a.Linksym().Name, b.Linksym().Name)
})
for _, nam := range funcsyms {
s := nam.Sym()
Expand Down
Loading

0 comments on commit f15095f

Please sign in to comment.