Skip to content

Commit 567c98b

Browse files
committed
internal/lsp/cache: don't walk URIs to invalidate metadata
Since ids is derived from metadata, we should not have to walk ids to see which metadata is still active. Just compute metadata updates directly. Benchmark (didChange in k8s): ~45ms->41ms For golang/go#45686 Change-Id: Id557ed3f2e05c903e4bb3f3f6a4af864751c4546 Reviewed-on: https://go-review.googlesource.com/c/tools/+/340854 TryBot-Result: Gopher Robot <[email protected]> Run-TryBot: Robert Findley <[email protected]> Reviewed-by: Alan Donovan <[email protected]> gopls-CI: kokoro <[email protected]>
1 parent dffd645 commit 567c98b

File tree

1 file changed

+15
-27
lines changed

1 file changed

+15
-27
lines changed

internal/lsp/cache/snapshot.go

Lines changed: 15 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1933,6 +1933,12 @@ func (s *snapshot) clone(ctx, bgCtx context.Context, changes map[span.URI]*fileC
19331933
// If a file has been deleted, we must delete metadata for all packages
19341934
// containing that file.
19351935
workspaceModeChanged := s.workspaceMode() != result.workspaceMode()
1936+
1937+
// Don't keep package metadata for packages that have lost files.
1938+
//
1939+
// TODO(rfindley): why not keep invalid metadata in this case? If we
1940+
// otherwise allow operate on invalid metadata, why not continue to do so,
1941+
// skipping the missing file?
19361942
skipID := map[PackageID]bool{}
19371943
for _, c := range changes {
19381944
if c.exists {
@@ -1967,41 +1973,23 @@ func (s *snapshot) clone(ctx, bgCtx context.Context, changes map[span.URI]*fileC
19671973
addForwardDeps(id)
19681974
}
19691975

1970-
// Compute which IDs are in the snapshot.
1971-
//
1972-
// TODO(rfindley): this step shouldn't be necessary, since we compute skipID
1973-
// above based on meta.ids.
1974-
deleteInvalidMetadata := forceReloadMetadata || workspaceModeChanged
1975-
idsInSnapshot := map[PackageID]bool{} // track all known IDs
1976-
for _, ids := range s.meta.ids {
1977-
for _, id := range ids {
1978-
if skipID[id] || deleteInvalidMetadata && idsToInvalidate[id] {
1979-
continue
1980-
}
1981-
// The ID is not reachable from any workspace package, so it should
1982-
// be deleted.
1983-
if !reachableID[id] {
1984-
continue
1985-
}
1986-
idsInSnapshot[id] = true
1987-
}
1988-
}
1989-
// TODO(adonovan): opt: represent PackageID as an index into a process-global
1990-
// dup-free list of all package names ever seen, then use a bitmap instead of
1991-
// a hash table for "PackageSet" (e.g. idsInSnapshot).
1992-
19931976
// Compute which metadata updates are required. We only need to invalidate
19941977
// packages directly containing the affected file, and only if it changed in
19951978
// a relevant way.
19961979
metadataUpdates := make(map[PackageID]*KnownMetadata)
1980+
deleteInvalidMetadata := forceReloadMetadata || workspaceModeChanged
19971981
for k, v := range s.meta.metadata {
1998-
if !idsInSnapshot[k] {
1999-
// Delete metadata for IDs that are no longer reachable from files
2000-
// in the snapshot.
1982+
invalidateMetadata := idsToInvalidate[k]
1983+
if skipID[k] || (invalidateMetadata && deleteInvalidMetadata) {
1984+
metadataUpdates[k] = nil
1985+
continue
1986+
}
1987+
// The ID is not reachable from any workspace package, so it should
1988+
// be deleted.
1989+
if !reachableID[k] {
20011990
metadataUpdates[k] = nil
20021991
continue
20031992
}
2004-
invalidateMetadata := idsToInvalidate[k]
20051993
valid := v.Valid && !invalidateMetadata
20061994
pkgFilesChanged := v.PkgFilesChanged || changedPkgFiles[k]
20071995
shouldLoad := v.ShouldLoad || invalidateMetadata

0 commit comments

Comments
 (0)