From caaa49c6ded07346b2b659902ac146cadf1cecb6 Mon Sep 17 00:00:00 2001 From: Ian Cottrell Date: Tue, 19 Nov 2019 16:45:03 -0500 Subject: [PATCH] internal/lsp: only construct a cache when we need to we only construct a cache as we build a server, rather than for each instance of Application now. Change-Id: Ic18966906f8f61b18b71fc6d6f7ccc8e9cebbd29 Reviewed-on: https://go-review.googlesource.com/c/tools/+/207904 Run-TryBot: Ian Cottrell TryBot-Result: Gobot Gobot Reviewed-by: Rebecca Stambler --- internal/lsp/cmd/cmd.go | 10 +++++----- internal/lsp/cmd/serve.go | 7 ++++--- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/internal/lsp/cmd/cmd.go b/internal/lsp/cmd/cmd.go index 8bc2128096..2f179f8af7 100644 --- a/internal/lsp/cmd/cmd.go +++ b/internal/lsp/cmd/cmd.go @@ -45,8 +45,8 @@ type Application struct { // TODO: Remove this when we stop allowing the serve verb by default. Serve Serve - // The base cache to use for sessions from this application. - cache source.Cache + // the options configuring function to invoke when building a server + options func(*source.Options) // The name of the binary, used in help and telemetry. name string @@ -77,7 +77,7 @@ func New(name, wd string, env []string, options func(*source.Options)) *Applicat wd, _ = os.Getwd() } app := &Application{ - cache: cache.New(options), + options: options, name: name, wd: wd, env: env, @@ -165,7 +165,7 @@ func (app *Application) connect(ctx context.Context) (*connection, error) { switch app.Remote { case "": connection := newConnection(app) - ctx, connection.Server = lsp.NewClientServer(ctx, app.cache, connection.Client) + ctx, connection.Server = lsp.NewClientServer(ctx, cache.New(app.options), connection.Client) return connection, connection.initialize(ctx) case "internal": internalMu.Lock() @@ -181,7 +181,7 @@ func (app *Application) connect(ctx context.Context) (*connection, error) { ctx, jc, connection.Server = protocol.NewClient(ctx, jsonrpc2.NewHeaderStream(cr, cw), connection.Client) go jc.Run(ctx) go func() { - ctx, srv := lsp.NewServer(ctx, app.cache, jsonrpc2.NewHeaderStream(sr, sw)) + ctx, srv := lsp.NewServer(ctx, cache.New(app.options), jsonrpc2.NewHeaderStream(sr, sw)) srv.Run(ctx) }() if err := connection.initialize(ctx); err != nil { diff --git a/internal/lsp/cmd/serve.go b/internal/lsp/cmd/serve.go index 79b4546ee1..2fdec959a7 100644 --- a/internal/lsp/cmd/serve.go +++ b/internal/lsp/cmd/serve.go @@ -18,6 +18,7 @@ import ( "golang.org/x/tools/internal/jsonrpc2" "golang.org/x/tools/internal/lsp" + "golang.org/x/tools/internal/lsp/cache" "golang.org/x/tools/internal/lsp/debug" "golang.org/x/tools/internal/lsp/protocol" "golang.org/x/tools/internal/lsp/telemetry" @@ -87,16 +88,16 @@ func (s *Serve) Run(ctx context.Context, args ...string) error { } run := func(ctx context.Context, srv *lsp.Server) { go prepare(ctx, srv).Run(ctx) } if s.Address != "" { - return lsp.RunServerOnAddress(ctx, s.app.cache, s.Address, run) + return lsp.RunServerOnAddress(ctx, cache.New(s.app.options), s.Address, run) } if s.Port != 0 { - return lsp.RunServerOnPort(ctx, s.app.cache, s.Port, run) + return lsp.RunServerOnPort(ctx, cache.New(s.app.options), s.Port, run) } stream := jsonrpc2.NewHeaderStream(os.Stdin, os.Stdout) if s.Trace { stream = protocol.LoggingStream(stream, out) } - ctx, srv := lsp.NewServer(ctx, s.app.cache, stream) + ctx, srv := lsp.NewServer(ctx, cache.New(s.app.options), stream) return prepare(ctx, srv).Run(ctx) }