]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go: replace reflect.DeepEqual with slices.Equal and maps.Equal
authorDaniel Martí <mvdan@mvdan.cc>
Tue, 26 Mar 2024 20:59:41 +0000 (22:59 +0200)
committerDaniel Martí <mvdan@mvdan.cc>
Wed, 27 Mar 2024 21:58:12 +0000 (21:58 +0000)
All of these maps and slices are made up of comparable types,
so we can avoid the overhead of reflection entirely.

Change-Id: If77dbe648a336ba729c171e84c9ff3f7e160297d
Reviewed-on: https://go-review.googlesource.com/c/go/+/574597
Reviewed-by: Than McIntosh <thanm@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Ian Lance Taylor <iant@google.com>
src/cmd/go/internal/modload/buildlist.go
src/cmd/go/internal/modload/load.go
src/cmd/go/internal/mvs/mvs.go

index d72a24f111449ad60fd40780074a770826a27e23..9c11bd4d137f376a4d4d7bf9883d1e8f46567f30 100644 (file)
@@ -8,8 +8,8 @@ import (
        "context"
        "errors"
        "fmt"
+       "maps"
        "os"
-       "reflect"
        "runtime"
        "runtime/debug"
        "slices"
@@ -1228,7 +1228,7 @@ func updatePrunedRoots(ctx context.Context, direct map[string]bool, rs *Requirem
                }
        }
 
-       if rs.pruning == pruned && reflect.DeepEqual(roots, rs.rootModules) && reflect.DeepEqual(direct, rs.direct) {
+       if rs.pruning == pruned && slices.Equal(roots, rs.rootModules) && maps.Equal(direct, rs.direct) {
                // The root set is unchanged and rs was already pruned, so keep rs to
                // preserve its cached ModuleGraph (if any).
                return rs, nil
@@ -1469,7 +1469,7 @@ func updateUnprunedRoots(ctx context.Context, direct map[string]bool, rs *Requir
        if MainModules.Len() > 1 {
                gover.ModSort(roots)
        }
-       if rs.pruning == unpruned && reflect.DeepEqual(roots, rs.rootModules) && reflect.DeepEqual(direct, rs.direct) {
+       if rs.pruning == unpruned && slices.Equal(roots, rs.rootModules) && maps.Equal(direct, rs.direct) {
                // The root set is unchanged and rs was already unpruned, so keep rs to
                // preserve its cached ModuleGraph (if any).
                return rs, nil
index 51eb141d4b38b827d1233c33b26619d51d15181f..408c109f5b3c8e9d1deff96a285135cca01d43de 100644 (file)
@@ -99,12 +99,13 @@ import (
        "fmt"
        "go/build"
        "io/fs"
+       "maps"
        "os"
        "path"
        pathpkg "path"
        "path/filepath"
-       "reflect"
        "runtime"
+       "slices"
        "sort"
        "strings"
        "sync"
@@ -1181,7 +1182,7 @@ func loadFromRoots(ctx context.Context, params loaderParams) *loader {
                        ld.error(err)
                        break
                }
-               if reflect.DeepEqual(rs.rootModules, ld.requirements.rootModules) {
+               if slices.Equal(rs.rootModules, ld.requirements.rootModules) {
                        // Something is deeply wrong. resolveMissingImports gave us a non-empty
                        // set of modules to add to the graph, but adding those modules had no
                        // effect — either they were already in the graph, or updateRoots did not
@@ -1319,7 +1320,7 @@ func (ld *loader) updateRequirements(ctx context.Context) (changed bool, err err
        // imports.AnyTags, then we didn't necessarily load every package that
        // contributes “direct” imports — so we can't safely mark existing direct
        // dependencies in ld.requirements as indirect-only. Propagate them as direct.
-       loadedDirect := ld.allPatternIsRoot && reflect.DeepEqual(ld.Tags, imports.AnyTags())
+       loadedDirect := ld.allPatternIsRoot && maps.Equal(ld.Tags, imports.AnyTags())
        if loadedDirect {
                direct = make(map[string]bool)
        } else {
@@ -1465,7 +1466,7 @@ func (ld *loader) updateRequirements(ctx context.Context) (changed bool, err err
                // packages present in the standard library. If it has changed, it's best to
                // reload packages once more to be sure everything is stable.
                changed = true
-       } else if rs != ld.requirements && !reflect.DeepEqual(rs.rootModules, ld.requirements.rootModules) {
+       } else if rs != ld.requirements && !slices.Equal(rs.rootModules, ld.requirements.rootModules) {
                // The roots of the module graph have changed in some way (not just the
                // "direct" markings). Check whether the changes affected any of the loaded
                // packages.
@@ -1779,7 +1780,7 @@ func (ld *loader) preloadRootModules(ctx context.Context, rootPkgs []string) (ch
                ld.exitIfErrors(ctx)
                return false
        }
-       if reflect.DeepEqual(rs.rootModules, ld.requirements.rootModules) {
+       if slices.Equal(rs.rootModules, ld.requirements.rootModules) {
                // Something is deeply wrong. resolveMissingImports gave us a non-empty
                // set of modules to add to the graph, but adding those modules had no
                // effect — either they were already in the graph, or updateRoots did not
index 468a9859272ead5c245b00fc2d40f3aa3bb6164a..1ac5aeb47115b366dc6f5f6effc2084d758770e6 100644 (file)
@@ -8,7 +8,7 @@ package mvs
 
 import (
        "fmt"
-       "reflect"
+       "slices"
        "sort"
        "sync"
 
@@ -171,7 +171,7 @@ func buildList(targets []module.Version, reqs Reqs, upgrade func(module.Version)
 
        // The final list is the minimum version of each module found in the graph.
        list := g.BuildList()
-       if vs := list[:len(targets)]; !reflect.DeepEqual(vs, targets) {
+       if vs := list[:len(targets)]; !slices.Equal(vs, targets) {
                // target.Version will be "" for modload, the main client of MVS.
                // "" denotes the main module, which has no version. However, MVS treats
                // version strings as opaque, so "" is not a special value here.