From d8f48f7e77dded574ca1bd1e901ef0164373a088 Mon Sep 17 00:00:00 2001
From: Cristian Maglie <c.maglie@arduino.cc>
Date: Mon, 5 Feb 2024 16:38:43 +0100
Subject: [PATCH 1/2] Limit parallel jobs to 1 / use in-memeory pch storage

---
 ls/lsp_client_clangd.go | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/ls/lsp_client_clangd.go b/ls/lsp_client_clangd.go
index 83789bb..165dec3 100644
--- a/ls/lsp_client_clangd.go
+++ b/ls/lsp_client_clangd.go
@@ -51,6 +51,8 @@ func newClangdLSPClient(logger jsonrpc.FunctionLogger, dataFolder *paths.Path, l
 	args := []string{
 		ls.config.ClangdPath.String(),
 		"-log=verbose",
+		"-j", "1", // Limit parallel build jobs to 1
+		"--pch-storage=memory",
 		fmt.Sprintf(`--compile-commands-dir=%s`, ls.buildPath),
 	}
 	if dataFolder != nil {

From 22c33aec6bbf183765d2cff6857ed34c2c81f8eb Mon Sep 17 00:00:00 2001
From: Cristian Maglie <c.maglie@arduino.cc>
Date: Tue, 6 Feb 2024 13:48:54 +0100
Subject: [PATCH 2/2] Allow jobs tuning via -jobs N flag

---
 ls/ls.go                | 1 +
 ls/lsp_client_clangd.go | 9 ++++++++-
 main.go                 | 2 ++
 3 files changed, 11 insertions(+), 1 deletion(-)

diff --git a/ls/ls.go b/ls/ls.go
index 17e4f56..794cd5c 100644
--- a/ls/ls.go
+++ b/ls/ls.go
@@ -77,6 +77,7 @@ type Config struct {
 	EnableLogging                   bool
 	SkipLibrariesDiscoveryOnRebuild bool
 	DisableRealTimeDiagnostics      bool
+	Jobs                            int
 }
 
 var yellow = color.New(color.FgHiYellow)
diff --git a/ls/lsp_client_clangd.go b/ls/lsp_client_clangd.go
index 165dec3..f39b3dd 100644
--- a/ls/lsp_client_clangd.go
+++ b/ls/lsp_client_clangd.go
@@ -51,10 +51,17 @@ func newClangdLSPClient(logger jsonrpc.FunctionLogger, dataFolder *paths.Path, l
 	args := []string{
 		ls.config.ClangdPath.String(),
 		"-log=verbose",
-		"-j", "1", // Limit parallel build jobs to 1
 		"--pch-storage=memory",
 		fmt.Sprintf(`--compile-commands-dir=%s`, ls.buildPath),
 	}
+	if jobs := ls.config.Jobs; jobs == -1 {
+		// default: limit parallel build jobs to 1
+		args = append(args, "-j", "1")
+	} else if jobs == 0 {
+		// no args: clangd will max out the available cores
+	} else {
+		args = append(args, "-j", fmt.Sprintf("%d", jobs))
+	}
 	if dataFolder != nil {
 		args = append(args, fmt.Sprintf("-query-driver=%s", dataFolder.Join("packages", "**").Canonical()))
 	}
diff --git a/main.go b/main.go
index 3333672..b1ed671 100644
--- a/main.go
+++ b/main.go
@@ -56,6 +56,7 @@ func main() {
 	noRealTimeDiagnostics := flag.Bool(
 		"no-real-time-diagnostics", false,
 		"Disable real time diagnostics")
+	jobs := flag.Int("jobs", -1, "Max number of parallel jobs. Default is 1. Use 0 to match the number of available CPU cores.")
 	flag.Parse()
 
 	if *loggingBasePath != "" {
@@ -127,6 +128,7 @@ func main() {
 		CliInstanceNumber:               *cliDaemonInstanceNumber,
 		SkipLibrariesDiscoveryOnRebuild: *skipLibrariesDiscoveryOnRebuild,
 		DisableRealTimeDiagnostics:      *noRealTimeDiagnostics,
+		Jobs:                            *jobs,
 	}
 
 	stdio := streams.NewReadWriteCloser(os.Stdin, os.Stdout)