Skip to content

Commit

Permalink
pkg: refactor sort.Sort to slices.SortFunc
Browse files Browse the repository at this point in the history
  • Loading branch information
alexandear committed Dec 20, 2024
1 parent 84c99e5 commit b654272
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 100 deletions.
9 changes: 5 additions & 4 deletions pkg/proc/bininfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package proc

import (
"bytes"
"cmp"
"debug/dwarf"
"debug/elf"
"debug/macho"
Expand Down Expand Up @@ -2305,7 +2306,7 @@ func loadBinaryInfoGoRuntimeCommon(bi *BinaryInfo, image *Image, cu *compileUnit
for i := range inlFuncs {
bi.Functions = append(bi.Functions, *inlFuncs[i])
}
sort.Sort(functionsDebugInfoByEntry(bi.Functions))
slices.SortFunc(bi.Functions, func(a, b Function) int { return cmp.Compare(a.Entry, b.Entry) })
for f := range image.symTable.Files {
bi.Sources = append(bi.Sources, f)
}
Expand Down Expand Up @@ -2534,9 +2535,9 @@ func (bi *BinaryInfo) loadDebugInfoMaps(image *Image, debugInfoBytes, debugLineB
}
}

sort.Sort(compileUnitsByOffset(image.compileUnits))
sort.Sort(functionsDebugInfoByEntry(bi.Functions))
sort.Sort(packageVarsByAddr(bi.packageVars))
slices.SortFunc(image.compileUnits, func(a, b *compileUnit) int { return cmp.Compare(a.offset, b.offset) })
slices.SortFunc(bi.Functions, func(a, b Function) int { return cmp.Compare(a.Entry, b.Entry) })
slices.SortFunc(bi.packageVars, func(a, b packageVar) int { return cmp.Compare(a.addr, b.addr) })

bi.lookupFunc = nil
bi.lookupGenericFunc = nil
Expand Down
7 changes: 6 additions & 1 deletion pkg/proc/eval.go
Original file line number Diff line number Diff line change
Expand Up @@ -496,7 +496,12 @@ func (scope *EvalScope) simpleLocals(flags localsFlags, wantedName string) ([]*V
return vars, nil
}

sort.Stable(&variablesByDepthAndDeclLine{vars, depths})
sort.SliceStable(vars, func(i, j int) bool {
if depths[i] == depths[j] {
return vars[i].DeclLine < vars[j].DeclLine
}
return depths[i] < depths[j]
})

lvn := map[string]*Variable{} // lvn[n] is the last variable we saw named n

Expand Down
18 changes: 0 additions & 18 deletions pkg/proc/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,24 +38,6 @@ func pointerTo(typ godwarf.Type, arch *Arch) godwarf.Type {
}
}

type functionsDebugInfoByEntry []Function

func (v functionsDebugInfoByEntry) Len() int { return len(v) }
func (v functionsDebugInfoByEntry) Less(i, j int) bool { return v[i].Entry < v[j].Entry }
func (v functionsDebugInfoByEntry) Swap(i, j int) { v[i], v[j] = v[j], v[i] }

type compileUnitsByOffset []*compileUnit

func (v compileUnitsByOffset) Len() int { return len(v) }
func (v compileUnitsByOffset) Less(i int, j int) bool { return v[i].offset < v[j].offset }
func (v compileUnitsByOffset) Swap(i int, j int) { v[i], v[j] = v[j], v[i] }

type packageVarsByAddr []packageVar

func (v packageVarsByAddr) Len() int { return len(v) }
func (v packageVarsByAddr) Less(i int, j int) bool { return v[i].addr < v[j].addr }
func (v packageVarsByAddr) Swap(i int, j int) { v[i], v[j] = v[j], v[i] }

type loadDebugInfoMapsContext struct {
ardr *reader.Reader
abstractOriginTable map[dwarf.Offset]int
Expand Down
32 changes: 5 additions & 27 deletions pkg/proc/variables.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package proc

import (
"bytes"
"cmp"
"debug/dwarf"
"encoding/binary"
"errors"
Expand All @@ -11,7 +12,7 @@ import (
"math"
"math/bits"
"reflect"
"sort"
"slices"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -2278,7 +2279,9 @@ func (cm constantsMap) Get(typ godwarf.Type) *constantType {
typepkg := packageName(typ.String()) + "."
if !ctyp.initialized {
ctyp.initialized = true
sort.Sort(constantValuesByValue(ctyp.values))
slices.SortFunc(ctyp.values, func(a, b constantValue) int {
return cmp.Compare(a.value, b.value)
})
for i := range ctyp.values {
ctyp.values[i].name = strings.TrimPrefix(ctyp.values[i].name, typepkg)
if bits.OnesCount64(uint64(ctyp.values[i].value)) == 1 {
Expand Down Expand Up @@ -2319,31 +2322,6 @@ func (ctyp *constantType) describe(n int64) string {
return ""
}

type variablesByDepthAndDeclLine struct {
vars []*Variable
depths []int
}

func (v *variablesByDepthAndDeclLine) Len() int { return len(v.vars) }

func (v *variablesByDepthAndDeclLine) Less(i int, j int) bool {
if v.depths[i] == v.depths[j] {
return v.vars[i].DeclLine < v.vars[j].DeclLine
}
return v.depths[i] < v.depths[j]
}

func (v *variablesByDepthAndDeclLine) Swap(i int, j int) {
v.depths[i], v.depths[j] = v.depths[j], v.depths[i]
v.vars[i], v.vars[j] = v.vars[j], v.vars[i]
}

type constantValuesByValue []constantValue

func (v constantValuesByValue) Len() int { return len(v) }
func (v constantValuesByValue) Less(i int, j int) bool { return v[i].value < v[j].value }
func (v constantValuesByValue) Swap(i int, j int) { v[i], v[j] = v[j], v[i] }

const (
timeTimeWallHasMonotonicBit uint64 = (1 << 63) // hasMonotonic bit of time.Time.wall

Expand Down
23 changes: 4 additions & 19 deletions pkg/proc/variables_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"reflect"
"regexp"
"runtime"
"sort"
"slices"
"strconv"
"strings"
"testing"
Expand Down Expand Up @@ -450,23 +450,6 @@ func TestMultilineVariableEvaluation(t *testing.T) {
})
}

type varArray []*proc.Variable

// Len is part of sort.Interface.
func (s varArray) Len() int {
return len(s)
}

// Swap is part of sort.Interface.
func (s varArray) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}

// Less is part of sort.Interface. It is implemented by calling the "by" closure in the sorter.
func (s varArray) Less(i, j int) bool {
return s[i].Name < s[j].Name
}

func TestLocalVariables(t *testing.T) {
testcases := []struct {
fn func(*proc.EvalScope, proc.LoadConfig) ([]*proc.Variable, error)
Expand Down Expand Up @@ -534,7 +517,9 @@ func TestLocalVariables(t *testing.T) {
vars, err := tc.fn(scope, pnormalLoadConfig)
assertNoError(err, t, "LocalVariables() returned an error")

sort.Sort(varArray(vars))
slices.SortFunc(vars, func(a, b *proc.Variable) int {
return strings.Compare(a.Name, b.Name)
})

if len(tc.output) != len(vars) {
t.Fatalf("Invalid variable count. Expected %d got %d.", len(tc.output), len(vars))
Expand Down
39 changes: 8 additions & 31 deletions pkg/terminal/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package terminal
import (
"bufio"
"bytes"
"cmp"
"errors"
"fmt"
"go/parser"
Expand All @@ -19,6 +20,7 @@ import (
"reflect"
"regexp"
"runtime"
"slices"
"sort"
"strconv"
"strings"
Expand Down Expand Up @@ -102,14 +104,6 @@ var (
ShortLoadConfig = api.LoadConfig{MaxStringLen: 64, MaxStructFields: 3}
)

// byFirstAlias will sort by the first
// alias of a command.
type byFirstAlias []command

func (a byFirstAlias) Len() int { return len(a) }
func (a byFirstAlias) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a byFirstAlias) Less(i, j int) bool { return a[i].aliases[0] < a[j].aliases[0] }

// DebugCommands returns a Commands struct with default commands defined.
func DebugCommands(client service.Client) *Commands {
c := &Commands{client: client}
Expand Down Expand Up @@ -688,7 +682,9 @@ Currently, rev next, step, step-instruction and stepout commands are supported.`
})
}

sort.Sort(byFirstAlias(c.cmds))
slices.SortFunc(c.cmds, func(a, b command) int {
return strings.Compare(a.aliases[0], b.aliases[0])
})
return c
}

Expand Down Expand Up @@ -814,12 +810,6 @@ func (c *Commands) help(t *Term, ctx callContext, args string) error {
return nil
}

type byThreadID []*api.Thread

func (a byThreadID) Len() int { return len(a) }
func (a byThreadID) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a byThreadID) Less(i, j int) bool { return a[i].ID < a[j].ID }

func threads(t *Term, ctx callContext, args string) error {
threads, err := t.client.ListThreads()
if err != nil {
Expand All @@ -829,7 +819,7 @@ func threads(t *Term, ctx callContext, args string) error {
if err != nil {
return err
}
sort.Sort(byThreadID(threads))
slices.SortFunc(threads, func(a, b *api.Thread) int { return cmp.Compare(a.ID, b.ID) })
done := false
t.stdout.pw.PageMaybe(func() { done = false })
for _, th := range threads {
Expand Down Expand Up @@ -880,12 +870,6 @@ func thread(t *Term, ctx callContext, args string) error {
return nil
}

type byGoroutineID []*api.Goroutine

func (a byGoroutineID) Len() int { return len(a) }
func (a byGoroutineID) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a byGoroutineID) Less(i, j int) bool { return a[i].ID < a[j].ID }

func (c *Commands) printGoroutines(t *Term, ctx callContext, indent string, gs []*api.Goroutine, fgl api.FormatGoroutineLoc, flags api.PrintGoroutinesFlags, depth int, cmd string, pdone *bool, state *api.DebuggerState) error {
for _, g := range gs {
if t.longCommandCanceled() || (pdone != nil && *pdone) {
Expand Down Expand Up @@ -961,7 +945,7 @@ func (c *Commands) goroutines(t *Term, ctx callContext, argstr string) error {
fmt.Fprintf(t.stdout, "Too many groups\n")
}
} else {
sort.Sort(byGoroutineID(gs))
slices.SortFunc(gs, func(a, b *api.Goroutine) int { return cmp.Compare(a.ID, b.ID) })
err = c.printGoroutines(t, ctx, "", gs, fgl, flags, depth, cmd, &done, state)
if err != nil {
return err
Expand Down Expand Up @@ -1741,19 +1725,12 @@ func toggle(t *Term, ctx callContext, args string) error {
return nil
}

// byID sorts breakpoints by ID.
type byID []*api.Breakpoint

func (a byID) Len() int { return len(a) }
func (a byID) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a byID) Less(i, j int) bool { return a[i].ID < a[j].ID }

func breakpoints(t *Term, ctx callContext, args string) error {
breakPoints, err := t.client.ListBreakpoints(args == "-a")
if err != nil {
return err
}
sort.Sort(byID(breakPoints))
slices.SortFunc(breakPoints, func(a, b *api.Breakpoint) int { return cmp.Compare(a.ID, b.ID) })
for _, bp := range breakPoints {
enabled := "(enabled)"
if bp.Disabled {
Expand Down

0 comments on commit b654272

Please sign in to comment.