Skip to content

Commit

Permalink
gopls/internal/lsp/cache: fast-path for type-checking active packages
Browse files Browse the repository at this point in the history
Partially revert CL 512636, putting back the fast path to fetch the
active package. This fixes a "regression" in certain fast benchmarks,
such as Definition, Hover, or References in a local package.

The numbers are still small either way, but may matter in very large
repos.

Change-Id: Id850eaa7a2599d9fb6ad042e762b59b3d5220cf1
Reviewed-on: https://go-review.googlesource.com/c/tools/+/513101
Run-TryBot: Robert Findley <rfindley@google.com>
gopls-CI: kokoro <noreply+kokoro@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Alan Donovan <adonovan@google.com>
  • Loading branch information
findleyr committed Jul 26, 2023
1 parent da5abd3 commit e8cdaf4
Showing 1 changed file with 22 additions and 2 deletions.
24 changes: 22 additions & 2 deletions gopls/internal/lsp/cache/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,30 @@ type pkgOrErr struct {
// the type-checking operation.
func (s *snapshot) TypeCheck(ctx context.Context, ids ...PackageID) ([]source.Package, error) {
pkgs := make([]source.Package, len(ids))

var (
needIDs []PackageID // ids to type-check
indexes []int // original index of requested ids
)

// Check for existing active packages, as any package will do.
//
// This is also done inside forEachPackage, but doing it here avoids
// unnecessary set up for type checking (e.g. assembling the package handle
// graph).
for i, id := range ids {
if pkg := s.getActivePackage(id); pkg != nil {
pkgs[i] = pkg
} else {
needIDs = append(needIDs, id)
indexes = append(indexes, i)
}
}

post := func(i int, pkg *Package) {
pkgs[i] = pkg
pkgs[indexes[i]] = pkg
}
return pkgs, s.forEachPackage(ctx, ids, nil, post)
return pkgs, s.forEachPackage(ctx, needIDs, nil, post)
}

// getImportGraph returns a shared import graph use for this snapshot, or nil.
Expand Down

0 comments on commit e8cdaf4

Please sign in to comment.