cmd/go: replace reflect.DeepEqual with slices.Equal and maps.Equal

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>
This commit is contained in:
Daniel Martí 2024-03-26 22:59:41 +02:00
parent 0c5612092d
commit 2e1003e2f7
3 changed files with 11 additions and 10 deletions

View 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

View 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

View 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.