diff options
author | Roger Peppe <rogpeppe@gmail.com> | 2023-01-30 17:27:42 +0000 |
---|---|---|
committer | roger peppe <rogpeppe@gmail.com> | 2023-02-03 09:56:24 +0000 |
commit | 2f2c5e41e7728cae99abd14e8bd5ffcfebf9df8e (patch) | |
tree | 4efc28e3178daff73df7bfed4c70056c6b2d3c66 /src/cmd/go/internal/modload/load.go | |
parent | 9222a01e651f5968a1c66013f7094c265f4011e7 (diff) | |
download | go-2f2c5e41e7728cae99abd14e8bd5ffcfebf9df8e.tar.gz go-2f2c5e41e7728cae99abd14e8bd5ffcfebf9df8e.zip |
cmd/go/internal/par: use generic Cache
Using generics here makes the code easier to understand,
as the contract is clearly specified. It also makes the
code a little more concise, as it's easy to write a wrapper
for the cache that adds an error value, meaning that
a bunch of auxilliary types no longer need to be defined
for this common case.
The load.cachingRepo code has been changed to use a separate
cache for each key-value type combination, which seems a bit less
sleazy, but might have some knock-on effect on memory usage,
and could easily be changed back if desired.
Because there's no longer an unambiguous way to find out
whether there's an entry in the cache, the Cache.Get method
now returns a bool as well as the value itself.
Change-Id: I28443125bab0b3720cc95d750e72d28e9b96257d
Reviewed-on: https://go-review.googlesource.com/c/go/+/463843
Reviewed-by: Bryan Mills <bcmills@google.com>
Run-TryBot: roger peppe <rogpeppe@gmail.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Michael Knyszek <mknyszek@google.com>
Diffstat (limited to 'src/cmd/go/internal/modload/load.go')
-rw-r--r-- | src/cmd/go/internal/modload/load.go | 16 |
1 files changed, 8 insertions, 8 deletions
diff --git a/src/cmd/go/internal/modload/load.go b/src/cmd/go/internal/modload/load.go index f450ced299..405b7935e0 100644 --- a/src/cmd/go/internal/modload/load.go +++ b/src/cmd/go/internal/modload/load.go @@ -772,7 +772,7 @@ func (mms *MainModuleSet) DirImportPath(ctx context.Context, dir string) (path s // PackageModule returns the module providing the package named by the import path. func PackageModule(path string) module.Version { - pkg, ok := loaded.pkgCache.Get(path).(*loadPkg) + pkg, ok := loaded.pkgCache.Get(path) if !ok { return module.Version{} } @@ -791,7 +791,7 @@ func Lookup(parentPath string, parentIsStd bool, path string) (dir, realPath str if parentIsStd { path = loaded.stdVendor(parentPath, path) } - pkg, ok := loaded.pkgCache.Get(path).(*loadPkg) + pkg, ok := loaded.pkgCache.Get(path) if !ok { // The loader should have found all the relevant paths. // There are a few exceptions, though: @@ -827,7 +827,7 @@ type loader struct { // reset on each iteration roots []*loadPkg - pkgCache *par.Cache // package path (string) → *loadPkg + pkgCache *par.Cache[string, *loadPkg] pkgs []*loadPkg // transitive closure of loaded packages and tests; populated in buildStacks } @@ -850,7 +850,7 @@ func (ld *loader) reset() { } ld.roots = nil - ld.pkgCache = new(par.Cache) + ld.pkgCache = new(par.Cache[string, *loadPkg]) ld.pkgs = nil } @@ -1504,7 +1504,7 @@ func (ld *loader) pkg(ctx context.Context, path string, flags loadPkgFlags) *loa panic("internal error: (*loader).pkg called with pkgImportsLoaded flag set") } - pkg := ld.pkgCache.Do(path, func() any { + pkg := ld.pkgCache.Do(path, func() *loadPkg { pkg := &loadPkg{ path: path, } @@ -1512,7 +1512,7 @@ func (ld *loader) pkg(ctx context.Context, path string, flags loadPkgFlags) *loa ld.work.Add(func() { ld.load(ctx, pkg) }) return pkg - }).(*loadPkg) + }) ld.applyPkgFlags(ctx, pkg, flags) return pkg @@ -2214,7 +2214,7 @@ func (pkg *loadPkg) why() string { // If there is no reason for the package to be in the current build, // Why returns an empty string. func Why(path string) string { - pkg, ok := loaded.pkgCache.Get(path).(*loadPkg) + pkg, ok := loaded.pkgCache.Get(path) if !ok { return "" } @@ -2226,7 +2226,7 @@ func Why(path string) string { // WhyDepth returns 0. func WhyDepth(path string) int { n := 0 - pkg, _ := loaded.pkgCache.Get(path).(*loadPkg) + pkg, _ := loaded.pkgCache.Get(path) for p := pkg; p != nil; p = p.stack { n++ } |