Skip to content

Commit

Permalink
all: use sort.Slice in a few more places
Browse files Browse the repository at this point in the history
Do the low-hanging fruit - tiny Less functions that are used exactly
once. This reduces the amount of code and puts the logic in a single
place.

Change-Id: I9d4544cd68de5a95e55019bdad1fca0a1dbfae9c
Reviewed-on: https://go-review.googlesource.com/63171
Run-TryBot: Daniel Martí <mvdan@mvdan.cc>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
  • Loading branch information
mvdan committed Sep 22, 2017
1 parent 67a0c78 commit 57e7d62
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 55 deletions.
20 changes: 9 additions & 11 deletions src/runtime/pprof/pprof.go
Expand Up @@ -319,7 +319,15 @@ func (p *Profile) WriteTo(w io.Writer, debug int) error {
p.mu.Unlock() p.mu.Unlock()


// Map order is non-deterministic; make output deterministic. // Map order is non-deterministic; make output deterministic.
sort.Sort(stackProfile(all)) sort.Slice(all, func(i, j int) bool {
t, u := all[i], all[j]
for k := 0; k < len(t) && k < len(u); k++ {
if t[k] != u[k] {
return t[k] < u[k]
}
}
return len(t) < len(u)
})


return printCountProfile(w, debug, p.name, stackProfile(all)) return printCountProfile(w, debug, p.name, stackProfile(all))
} }
Expand All @@ -328,16 +336,6 @@ type stackProfile [][]uintptr


func (x stackProfile) Len() int { return len(x) } func (x stackProfile) Len() int { return len(x) }
func (x stackProfile) Stack(i int) []uintptr { return x[i] } func (x stackProfile) Stack(i int) []uintptr { return x[i] }
func (x stackProfile) Swap(i, j int) { x[i], x[j] = x[j], x[i] }
func (x stackProfile) Less(i, j int) bool {
t, u := x[i], x[j]
for k := 0; k < len(t) && k < len(u); k++ {
if t[k] != u[k] {
return t[k] < u[k]
}
}
return len(t) < len(u)
}


// A countProfile is a set of stack traces to be printed as counts // A countProfile is a set of stack traces to be printed as counts
// grouped by stack trace. There are multiple implementations: // grouped by stack trace. There are multiple implementations:
Expand Down
39 changes: 12 additions & 27 deletions src/text/template/exec.go
Expand Up @@ -927,43 +927,28 @@ func printableValue(v reflect.Value) (interface{}, bool) {
return v.Interface(), true return v.Interface(), true
} }


// Types to help sort the keys in a map for reproducible output.

type rvs []reflect.Value

func (x rvs) Len() int { return len(x) }
func (x rvs) Swap(i, j int) { x[i], x[j] = x[j], x[i] }

type rvInts struct{ rvs }

func (x rvInts) Less(i, j int) bool { return x.rvs[i].Int() < x.rvs[j].Int() }

type rvUints struct{ rvs }

func (x rvUints) Less(i, j int) bool { return x.rvs[i].Uint() < x.rvs[j].Uint() }

type rvFloats struct{ rvs }

func (x rvFloats) Less(i, j int) bool { return x.rvs[i].Float() < x.rvs[j].Float() }

type rvStrings struct{ rvs }

func (x rvStrings) Less(i, j int) bool { return x.rvs[i].String() < x.rvs[j].String() }

// sortKeys sorts (if it can) the slice of reflect.Values, which is a slice of map keys. // sortKeys sorts (if it can) the slice of reflect.Values, which is a slice of map keys.
func sortKeys(v []reflect.Value) []reflect.Value { func sortKeys(v []reflect.Value) []reflect.Value {
if len(v) <= 1 { if len(v) <= 1 {
return v return v
} }
switch v[0].Kind() { switch v[0].Kind() {
case reflect.Float32, reflect.Float64: case reflect.Float32, reflect.Float64:
sort.Sort(rvFloats{v}) sort.Slice(v, func(i, j int) bool {
return v[i].Float() < v[j].Float()
})
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
sort.Sort(rvInts{v}) sort.Slice(v, func(i, j int) bool {
return v[i].Int() < v[j].Int()
})
case reflect.String: case reflect.String:
sort.Sort(rvStrings{v}) sort.Slice(v, func(i, j int) bool {
return v[i].String() < v[j].String()
})
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr: case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
sort.Sort(rvUints{v}) sort.Slice(v, func(i, j int) bool {
return v[i].Uint() < v[j].Uint()
})
} }
return v return v
} }
16 changes: 6 additions & 10 deletions src/time/genzabbrs.go
Expand Up @@ -52,12 +52,6 @@ type zone struct {
DSTime string DSTime string
} }


type zones []*zone

func (zs zones) Len() int { return len(zs) }
func (zs zones) Swap(i, j int) { zs[i], zs[j] = zs[j], zs[i] }
func (zs zones) Less(i, j int) bool { return zs[i].UnixName < zs[j].UnixName }

const wzURL = "http://unicode.org/cldr/data/common/supplemental/windowsZones.xml" const wzURL = "http://unicode.org/cldr/data/common/supplemental/windowsZones.xml"


type MapZone struct { type MapZone struct {
Expand All @@ -70,7 +64,7 @@ type SupplementalData struct {
Zones []MapZone `xml:"windowsZones>mapTimezones>mapZone"` Zones []MapZone `xml:"windowsZones>mapTimezones>mapZone"`
} }


func readWindowsZones() (zones, error) { func readWindowsZones() ([]*zone, error) {
r, err := http.Get(wzURL) r, err := http.Get(wzURL)
if err != nil { if err != nil {
return nil, err return nil, err
Expand All @@ -87,7 +81,7 @@ func readWindowsZones() (zones, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
zs := make(zones, 0) zs := make([]*zone, 0)
for _, z := range sd.Zones { for _, z := range sd.Zones {
if z.Territory != "001" { if z.Territory != "001" {
// to avoid dups. I don't know why. // to avoid dups. I don't know why.
Expand All @@ -114,10 +108,12 @@ func main() {
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
sort.Sort(zs) sort.Slice(zs, func(i, j int) bool {
return zs[i].UnixName < zs[j].UnixName
})
var v = struct { var v = struct {
URL string URL string
Zs zones Zs []*zone
}{ }{
wzURL, wzURL,
zs, zs,
Expand Down
10 changes: 3 additions & 7 deletions src/unicode/maketables.go
Expand Up @@ -1132,12 +1132,6 @@ func printLatinProperties() {
printf("}\n\n") printf("}\n\n")
} }


type runeSlice []rune

func (p runeSlice) Len() int { return len(p) }
func (p runeSlice) Less(i, j int) bool { return p[i] < p[j] }
func (p runeSlice) Swap(i, j int) { p[i], p[j] = p[j], p[i] }

func printCasefold() { func printCasefold() {
// Build list of case-folding groups attached to each canonical folded char (typically lower case). // Build list of case-folding groups attached to each canonical folded char (typically lower case).
var caseOrbit = make([][]rune, MaxChar+1) var caseOrbit = make([][]rune, MaxChar+1)
Expand Down Expand Up @@ -1184,7 +1178,9 @@ func printCasefold() {
if orb == nil { if orb == nil {
continue continue
} }
sort.Sort(runeSlice(orb)) sort.Slice(orb, func(i, j int) bool {
return orb[i] < orb[j]
})
c := orb[len(orb)-1] c := orb[len(orb)-1]
for _, d := range orb { for _, d := range orb {
chars[c].caseOrbit = d chars[c].caseOrbit = d
Expand Down

0 comments on commit 57e7d62

Please sign in to comment.