Skip to content
This repository was archived by the owner on Oct 12, 2022. It is now read-only.

Commit fcaddf9

Browse files
author
Stephen Gutekanst
committed
add -maxparallelism flag (default 1/2 NumCPU)
This change adds a `-maxparallelism` flag which defaults to 1/2 the NumCPU on the machine. It is used to control the language server's use of parallelism in editor environments where using all available resources to answer a query is not most ideal.
1 parent b109be4 commit fcaddf9

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

langserver/symbol.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,12 @@ func (h *LangHandler) handleWorkspaceSymbol(ctx context.Context, conn jsonrpc2.J
340340
return h.handleSymbol(ctx, conn, req, q, params.Limit)
341341
}
342342

343+
// MaxParallelism controls the maximum number of goroutines that should be used
344+
// to fulfill requests. This is useful in editor environments where users do
345+
// not want results ASAP, but rather just semi quickly without eating all of
346+
// their CPU.
347+
var MaxParallelism = 8
348+
343349
func (h *LangHandler) handleSymbol(ctx context.Context, conn jsonrpc2.JSONRPC2, req *jsonrpc2.Request, query Query, limit int) ([]lsp.SymbolInformation, error) {
344350
results := resultSorter{Query: query, results: make([]scoredSymbol, 0)}
345351
{

main.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"net"
1010
"net/http"
1111
"os"
12+
"runtime"
1213
"runtime/debug"
1314
"time"
1415

@@ -27,6 +28,7 @@ var (
2728
pprof = flag.String("pprof", ":6060", "start a pprof http server (https://golang.org/pkg/net/http/pprof/)")
2829
freeosmemory = flag.Bool("freeosmemory", true, "aggressively free memory back to the OS")
2930
usebinarypkgcache = flag.Bool("usebinarypkgcache", true, "use $GOPATH/pkg binary .a files (improves performance)")
31+
maxparallelism = flag.Int("maxparallelism", -1, "use at max N parallel goroutines to fulfill requests")
3032
)
3133

3234
// version is the version field we report back. If you are releasing a new version:
@@ -52,6 +54,15 @@ func main() {
5254
}
5355
langserver.UseBinaryPkgCache = *usebinarypkgcache
5456

57+
// Default max parallelism to half the CPU cores, but at least always one.
58+
if *maxparallelism <= 0 {
59+
*maxparallelism = runtime.NumCPU() / 2
60+
if *maxparallelism <= 0 {
61+
*maxparallelism = 1
62+
}
63+
}
64+
langserver.MaxParallelism = *maxparallelism
65+
5566
if err := run(); err != nil {
5667
fmt.Fprintln(os.Stderr, err)
5768
os.Exit(1)

0 commit comments

Comments
 (0)