]> Cypherpunks repositories - gostls13.git/commitdiff
cmd: change from sort functions to slices functions
authorIan Lance Taylor <iant@golang.org>
Wed, 22 May 2024 04:07:32 +0000 (21:07 -0700)
committerGopher Robot <gobot@golang.org>
Mon, 18 Nov 2024 19:38:28 +0000 (19:38 +0000)
Doing this because the slices functions are slightly faster and
slightly easier to use. It also removes one dependency layer.

We did this outside of bootstrap tools in CL 587655.
Now that the bootstrap compiler is 1.22, we can do this in more code.

Change-Id: I9ed2dd473758cacd14f76a0639368523ccdff72f
Reviewed-on: https://go-review.googlesource.com/c/go/+/626038
Auto-Submit: Ian Lance Taylor <iant@golang.org>
Reviewed-by: Ian Lance Taylor <iant@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Damien Neil <dneil@google.com>
src/cmd/api/api_test.go
src/cmd/api/main_test.go
src/cmd/compile/internal/reflectdata/reflect.go
src/cmd/compile/internal/typecheck/subr.go
src/cmd/compile/internal/types/size.go
src/cmd/compile/internal/types/sort.go [deleted file]
src/cmd/compile/internal/types/sym.go
src/cmd/compile/internal/types/sym_test.go
src/cmd/compile/internal/types/type.go

index ba358d364d51dce627346f9668e4f9c93cf6b5ab..78482333330bd8bce36d69e385b6e901ecd90a06 100644 (file)
@@ -11,7 +11,7 @@ import (
        "internal/testenv"
        "os"
        "path/filepath"
-       "sort"
+       "slices"
        "strings"
        "sync"
        "testing"
@@ -77,7 +77,7 @@ func TestGolden(t *testing.T) {
                        t.Fatalf("opening golden.txt for package %q: %v", fi.Name(), err)
                }
                wanted := strings.Split(string(bs), "\n")
-               sort.Strings(wanted)
+               slices.Sort(wanted)
                for _, feature := range wanted {
                        if feature == "" {
                                continue
index 10dbabb9b8bd1c405e2fbe88ccd4953cfefc49d6..a0820c2274c4787f5cb6b99ca38b558df233c074 100644 (file)
@@ -25,7 +25,7 @@ import (
        "path/filepath"
        "regexp"
        "runtime"
-       "sort"
+       "slices"
        "strconv"
        "strings"
        "sync"
@@ -232,8 +232,8 @@ func compareAPI(w io.Writer, features, required, exception []string) (ok bool) {
        featureSet := set(features)
        exceptionSet := set(exception)
 
-       sort.Strings(features)
-       sort.Strings(required)
+       slices.Sort(features)
+       slices.Sort(required)
 
        take := func(sl *[]string) string {
                s := (*sl)[0]
@@ -378,7 +378,7 @@ func (w *Walker) Features() (fs []string) {
        for f := range w.features {
                fs = append(fs, f)
        }
-       sort.Strings(fs)
+       slices.Sort(fs)
        return
 }
 
@@ -431,7 +431,7 @@ func tagKey(dir string, context *build.Context, tags []string) string {
        // an indirect imported package. See https://github.com/golang/go/issues/21181
        // for more detail.
        tags = append(tags, context.GOOS, context.GOARCH)
-       sort.Strings(tags)
+       slices.Sort(tags)
 
        for _, tag := range tags {
                if ctags[tag] {
@@ -535,7 +535,7 @@ func (w *Walker) loadImports() {
                        }
                }
 
-               sort.Strings(stdPackages)
+               slices.Sort(stdPackages)
                imports = listImports{
                        stdPackages: stdPackages,
                        importMap:   importMap,
@@ -717,7 +717,7 @@ func sortedMethodNames(typ *types.Interface) []string {
        for i := range list {
                list[i] = typ.Method(i).Name()
        }
-       sort.Strings(list)
+       slices.Sort(list)
        return list
 }
 
@@ -747,7 +747,7 @@ func (w *Walker) sortedEmbeddeds(typ *types.Interface) []string {
                        list = append(list, buf.String())
                }
        }
-       sort.Strings(list)
+       slices.Sort(list)
        return list
 }
 
@@ -1083,7 +1083,7 @@ func (w *Walker) emitIfaceType(name string, typ *types.Interface) {
                return
        }
 
-       sort.Strings(methodNames)
+       slices.Sort(methodNames)
        w.emitf("type %s interface { %s }", name, strings.Join(methodNames, ", "))
 }
 
index de7e4755d3a698804d7dbbde68f143ce5905a04d..c26ac3d74c2d50de4eb412ffb4bf2ff9402df8b8 100644 (file)
@@ -145,7 +145,7 @@ func imethods(t *types.Type) []*typeSig {
                }
                if n := len(methods); n > 0 {
                        last := methods[n-1]
-                       if !last.name.Less(f.Sym) {
+                       if types.CompareSyms(last.name, f.Sym) >= 0 {
                                base.Fatalf("sigcmp vs sortinter %v %v", last.name, f.Sym)
                        }
                }
index af7ab38638ad17724004aa34fe69105bd9ba1ab7..3b22d260bf4fafb6ca3baef0d72c7d1177d73bd6 100644 (file)
@@ -144,7 +144,7 @@ func CalcMethods(t *types.Type) {
        }
 
        ms = append(ms, t.Methods()...)
-       slices.SortFunc(ms, types.MethodsByNameCmp)
+       slices.SortFunc(ms, types.CompareFields)
        t.SetAllMethods(ms)
 }
 
index 308245d9b7299bbaeedd3c3a061e59a4abe14621..48729884df141ec82afab26384e6f01e4d0c2753 100644 (file)
@@ -7,7 +7,6 @@ package types
 import (
        "math"
        "slices"
-       "sort"
 
        "cmd/compile/internal/base"
        "cmd/internal/src"
@@ -94,21 +93,21 @@ func expandiface(t *Type) {
 
        {
                methods := t.Methods()
-               sort.SliceStable(methods, func(i, j int) bool {
-                       mi, mj := methods[i], methods[j]
-
+               slices.SortStableFunc(methods, func(a, b *Field) int {
                        // Sort embedded types by type name (if any).
-                       if mi.Sym == nil && mj.Sym == nil {
-                               return mi.Type.Sym().Less(mj.Type.Sym())
+                       if a.Sym == nil && b.Sym == nil {
+                               return CompareSyms(a.Type.Sym(), b.Type.Sym())
                        }
 
                        // Sort methods before embedded types.
-                       if mi.Sym == nil || mj.Sym == nil {
-                               return mi.Sym != nil
+                       if a.Sym == nil {
+                               return -1
+                       } else if b.Sym == nil {
+                               return +1
                        }
 
                        // Sort methods by symbol name.
-                       return mi.Sym.Less(mj.Sym)
+                       return CompareSyms(a.Sym, b.Sym)
                })
        }
 
@@ -147,7 +146,7 @@ func expandiface(t *Type) {
                m.Pos = src.NoXPos
        }
 
-       slices.SortFunc(methods, MethodsByNameCmp)
+       slices.SortFunc(methods, CompareFields)
 
        if int64(len(methods)) >= MaxWidth/int64(PtrSize) {
                base.ErrorfAt(typePos(t), 0, "interface too large")
diff --git a/src/cmd/compile/internal/types/sort.go b/src/cmd/compile/internal/types/sort.go
deleted file mode 100644 (file)
index 83b1237..0000000
+++ /dev/null
@@ -1,16 +0,0 @@
-// Copyright 2009 The Go Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style
-// license that can be found in the LICENSE file.
-
-package types
-
-// MethodsByNameCmp sorts methods by name.
-func MethodsByNameCmp(x, y *Field) int {
-       if x.Sym.Less(y.Sym) {
-               return -1
-       }
-       if y.Sym.Less(x.Sym) {
-               return +1
-       }
-       return 0
-}
index 67fa6bb1d0c1cc4a450ecf14f4cbd74e53113bdf..97175d745c1f5b331741f2946497b97c0887827f 100644 (file)
@@ -7,6 +7,7 @@ package types
 import (
        "cmd/compile/internal/base"
        "cmd/internal/obj"
+       "strings"
        "unicode"
        "unicode/utf8"
 )
@@ -92,39 +93,43 @@ func (sym *Sym) LinksymABI(abi obj.ABI) *obj.LSym {
        return base.PkgLinksym(sym.Pkg.Prefix, sym.Name, abi)
 }
 
-// Less reports whether symbol a is ordered before symbol b.
+// CompareSyms return the ordering of a and b, as for [cmp.Compare].
 //
 // Symbols are ordered exported before non-exported, then by name, and
 // finally (for non-exported symbols) by package path.
-func (a *Sym) Less(b *Sym) bool {
+func CompareSyms(a, b *Sym) int {
        if a == b {
-               return false
+               return 0
        }
 
        // Nil before non-nil.
        if a == nil {
-               return true
+               return -1
        }
        if b == nil {
-               return false
+               return +1
        }
 
        // Exported symbols before non-exported.
        ea := IsExported(a.Name)
        eb := IsExported(b.Name)
        if ea != eb {
-               return ea
+               if ea {
+                       return -1
+               } else {
+                       return +1
+               }
        }
 
        // Order by name and then (for non-exported names) by package
        // height and path.
-       if a.Name != b.Name {
-               return a.Name < b.Name
+       if r := strings.Compare(a.Name, b.Name); r != 0 {
+               return r
        }
        if !ea {
-               return a.Pkg.Path < b.Pkg.Path
+               return strings.Compare(a.Pkg.Path, b.Pkg.Path)
        }
-       return false
+       return 0
 }
 
 // IsExported reports whether name is an exported Go symbol (that is,
index 94efd42aa4ad446592c4e22706b8ce2545314eeb..cdb17c36f57cc7662e879ed5f2ec9eeeeede4e5f 100644 (file)
@@ -7,11 +7,11 @@ package types_test
 import (
        "cmd/compile/internal/types"
        "reflect"
-       "sort"
+       "slices"
        "testing"
 )
 
-func TestSymLess(t *testing.T) {
+func TestSymCompare(t *testing.T) {
        var (
                local = types.NewPkg("", "")
                abc   = types.NewPkg("abc", "")
@@ -50,7 +50,7 @@ func TestSymLess(t *testing.T) {
        if reflect.DeepEqual(data, want) {
                t.Fatal("data must be shuffled")
        }
-       sort.Slice(data, func(i, j int) bool { return data[i].Less(data[j]) })
+       slices.SortFunc(data, types.CompareSyms)
        if !reflect.DeepEqual(data, want) {
                t.Logf("want: %#v", want)
                t.Logf("data: %#v", data)
index 9d3dde8c133c7898224a73cf212d2b07754ec1a3..79c890d46cd388eb44d41375115555cf2a15b5b8 100644 (file)
@@ -458,6 +458,11 @@ func (f *Field) IsMethod() bool {
        return f.Type.kind == TFUNC && f.Type.Recv() != nil
 }
 
+// CompareFields compares two Field values by name.
+func CompareFields(a, b *Field) int {
+       return CompareSyms(a.Sym, b.Sym)
+}
+
 // fields is a pointer to a slice of *Field.
 // This saves space in Types that do not have fields or methods
 // compared to a simple slice of *Field.