gopls: pre-initialize the file cache
It actually takes ~50ms to hash the gopls executable, which is in the
critical path of all gopls operations. Start the file cache eagerly, so
that we may initialize while we wait for the Go command.
For golang/go#57987
Change-Id: I61f9ba60c6aeab12163a0a9254c17e72335d9dba
Reviewed-on: https://go-review.googlesource.com/c/tools/+/477976
Reviewed-by: Alan Donovan <adonovan@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
gopls-CI: kokoro <noreply+kokoro@google.com>
Run-TryBot: Robert Findley <rfindley@google.com>
diff --git a/gopls/internal/lsp/cmd/cmd.go b/gopls/internal/lsp/cmd/cmd.go
index a48eb16..a021c26 100644
--- a/gopls/internal/lsp/cmd/cmd.go
+++ b/gopls/internal/lsp/cmd/cmd.go
@@ -24,6 +24,7 @@
"golang.org/x/tools/gopls/internal/lsp"
"golang.org/x/tools/gopls/internal/lsp/cache"
"golang.org/x/tools/gopls/internal/lsp/debug"
+ "golang.org/x/tools/gopls/internal/lsp/filecache"
"golang.org/x/tools/gopls/internal/lsp/lsprpc"
"golang.org/x/tools/gopls/internal/lsp/protocol"
"golang.org/x/tools/gopls/internal/lsp/source"
@@ -214,6 +215,11 @@
// If no arguments are passed it will invoke the server sub command, as a
// temporary measure for compatibility.
func (app *Application) Run(ctx context.Context, args ...string) error {
+ // In the category of "things we can do while waiting for the Go command":
+ // Pre-initialize the filecache, which takes ~50ms to hash the gopls
+ // executable, and immediately runs a gc.
+ filecache.Start()
+
ctx = debug.WithInstance(ctx, app.wd, app.OCAgent)
if len(args) == 0 {
s := flag.NewFlagSet(app.Name(), flag.ExitOnError)
diff --git a/gopls/internal/lsp/filecache/filecache.go b/gopls/internal/lsp/filecache/filecache.go
index a510985..1974541 100644
--- a/gopls/internal/lsp/filecache/filecache.go
+++ b/gopls/internal/lsp/filecache/filecache.go
@@ -38,6 +38,14 @@
"golang.org/x/tools/internal/lockedfile"
)
+// Start causes the filecache to initialize and start garbage gollection.
+//
+// Start is automatically called by the first call to Get, but may be called
+// explicitly to pre-initialize the cache.
+func Start() {
+ go getCacheDir()
+}
+
// Get retrieves from the cache and returns a newly allocated
// copy of the value most recently supplied to Set(kind, key),
// possibly by another process.