From ac417207efcde975adf6ee4b92eb26012835f971 Mon Sep 17 00:00:00 2001 From: Rebecca Stambler Date: Tue, 26 Nov 2019 17:47:09 -0500 Subject: [PATCH] internal/lsp: run packages.Load only if imports are added or changed Previously, we would reload if a user's import list decreased or simply changed order. This is not necessary. Now, we only re-run if a new import needs to be loaded. Updates golang/go#35388 Change-Id: I47874afe773dddb835ac27b18895e7a082950dc7 Reviewed-on: https://go-review.googlesource.com/c/tools/+/209057 Reviewed-by: Heschi Kreinick --- internal/lsp/cache/load.go | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/internal/lsp/cache/load.go b/internal/lsp/cache/load.go index 8fe6963767..8de495a14b 100644 --- a/internal/lsp/cache/load.go +++ b/internal/lsp/cache/load.go @@ -96,13 +96,17 @@ func (c *cache) shouldLoad(ctx context.Context, s *snapshot, originalFH, current if original.Name.Name != current.Name.Name { return true } - // If the package's imports have changed, re-run `go list`. - if len(original.Imports) != len(current.Imports) { + // If the package's imports have increased, definitely re-run `go list`. + if len(original.Imports) < len(current.Imports) { return true } - for i, importSpec := range original.Imports { - // TODO: Handle the case where the imports have just been re-ordered. - if importSpec.Path.Value != current.Imports[i].Path.Value { + importSet := make(map[string]struct{}) + for _, importSpec := range original.Imports { + importSet[importSpec.Path.Value] = struct{}{} + } + // If any of the current imports were not in the original imports. + for _, importSpec := range current.Imports { + if _, ok := importSet[importSpec.Path.Value]; !ok { return true } }