From b2eae762671e8ccd6d473aca4239e46ff3a0f108 Mon Sep 17 00:00:00 2001 From: Alan Donovan Date: Wed, 13 Jul 2022 15:05:28 -0400 Subject: [PATCH] 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 Auto-Submit: Alan Donovan gopls-CI: kokoro Run-TryBot: Alan Donovan TryBot-Result: Gopher Robot --- internal/lsp/cache/check.go | 9 --------- internal/lsp/cache/mod.go | 23 ++--------------------- 2 files changed, 2 insertions(+), 30 deletions(-) diff --git a/internal/lsp/cache/check.go b/internal/lsp/cache/check.go index a830cc7f59..366a7afe13 100644 --- a/internal/lsp/cache/check.go +++ b/internal/lsp/cache/check.go @@ -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. diff --git a/internal/lsp/cache/mod.go b/internal/lsp/cache/mod.go index 8e6017648c..f9d148b737 100644 --- a/internal/lsp/cache/mod.go +++ b/internal/lsp/cache/mod.go @@ -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() }