internal/lsp/cache: simplify modwhy cache

As with mod tidy in CL 417116, we can rely on invalidation
rather than cache keys to avoid reusing stale results, and
there's no need to save these handles in the global store.

Change-Id: I3763c01fa21c6114248c1d541e3c168fc6a128c9
Reviewed-on: https://go-review.googlesource.com/c/tools/+/417416
Reviewed-by: Robert Findley <rfindley@google.com>
Auto-Submit: Alan Donovan <adonovan@google.com>
gopls-CI: kokoro <noreply+kokoro@google.com>
Run-TryBot: Alan Donovan <adonovan@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
This commit is contained in:
Alan Donovan 2022-07-13 15:05:28 -04:00 committed by Gopher Robot
parent dcb576d3b6
commit b2eae76267
2 changed files with 2 additions and 30 deletions

View File

@ -249,15 +249,6 @@ func computePackageKey(id PackageID, files []source.FileHandle, m *KnownMetadata
return packageHandleKey(source.HashOf(b.Bytes()))
}
// hashEnv returns a hash of the snapshot's configuration.
func hashEnv(s *snapshot) source.Hash {
s.view.optionsMu.Lock()
env := s.view.options.EnvSlice()
s.view.optionsMu.Unlock()
return source.Hashf("%s", env)
}
// hashConfig returns the hash for the *packages.Config.
func hashConfig(config *packages.Config) source.Hash {
// TODO(adonovan): opt: don't materialize the bytes; hash them directly.

View File

@ -223,33 +223,14 @@ func (s *snapshot) ModWhy(ctx context.Context, fh source.FileHandle) (map[string
// cache miss?
if !hit {
// TODO(adonovan): use a simpler cache of promises that
// is shared across snapshots. See comment at modTidyKey.
// We can then delete hashEnv too.
type modWhyKey struct {
// TODO(rfindley): is sessionID used to identify overlays because modWhy
// looks at overlay state? In that case, I am not sure that this key
// is actually correct. The key should probably just be URI, and
// invalidated in clone when any import changes.
sessionID string
env source.Hash
view string
mod source.FileIdentity
}
key := modWhyKey{
sessionID: s.view.session.id,
env: hashEnv(s),
mod: fh.FileIdentity(),
view: s.view.rootURI.Filename(),
}
handle, release := s.store.Handle(key, func(ctx context.Context, arg interface{}) interface{} {
handle := memoize.NewHandle("modWhy", func(ctx context.Context, arg interface{}) interface{} {
why, err := modWhyImpl(ctx, arg.(*snapshot), fh)
return modWhyResult{why, err}
})
entry = handle
s.mu.Lock()
s.modWhyHandles.Set(uri, entry, func(_, _ interface{}) { release() })
s.modWhyHandles.Set(uri, entry, nil)
s.mu.Unlock()
}