Skip to content

Commit df92f17

Browse files
committed
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 <[email protected]> TryBot-Result: Gopher Robot <[email protected]> gopls-CI: kokoro <[email protected]> Run-TryBot: Robert Findley <[email protected]>
1 parent c3edf5a commit df92f17

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

gopls/internal/lsp/cmd/cmd.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"golang.org/x/tools/gopls/internal/lsp"
2525
"golang.org/x/tools/gopls/internal/lsp/cache"
2626
"golang.org/x/tools/gopls/internal/lsp/debug"
27+
"golang.org/x/tools/gopls/internal/lsp/filecache"
2728
"golang.org/x/tools/gopls/internal/lsp/lsprpc"
2829
"golang.org/x/tools/gopls/internal/lsp/protocol"
2930
"golang.org/x/tools/gopls/internal/lsp/source"
@@ -214,6 +215,11 @@ func isZeroValue(f *flag.Flag, value string) bool {
214215
// If no arguments are passed it will invoke the server sub command, as a
215216
// temporary measure for compatibility.
216217
func (app *Application) Run(ctx context.Context, args ...string) error {
218+
// In the category of "things we can do while waiting for the Go command":
219+
// Pre-initialize the filecache, which takes ~50ms to hash the gopls
220+
// executable, and immediately runs a gc.
221+
filecache.Start()
222+
217223
ctx = debug.WithInstance(ctx, app.wd, app.OCAgent)
218224
if len(args) == 0 {
219225
s := flag.NewFlagSet(app.Name(), flag.ExitOnError)

gopls/internal/lsp/filecache/filecache.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,14 @@ import (
3838
"golang.org/x/tools/internal/lockedfile"
3939
)
4040

41+
// Start causes the filecache to initialize and start garbage gollection.
42+
//
43+
// Start is automatically called by the first call to Get, but may be called
44+
// explicitly to pre-initialize the cache.
45+
func Start() {
46+
go getCacheDir()
47+
}
48+
4149
// Get retrieves from the cache and returns a newly allocated
4250
// copy of the value most recently supplied to Set(kind, key),
4351
// possibly by another process.

0 commit comments

Comments
 (0)