From 7d466570f47f67819edb556ccd3658b9073ec088 Mon Sep 17 00:00:00 2001 From: Junliang HU Date: Fri, 9 Dec 2022 21:11:46 +0800 Subject: [PATCH 1/2] Add numThreads in config to avoid spawning lots of threads every time --- crates/rust-analyzer/src/config.rs | 7 +++++++ crates/rust-analyzer/src/global_state.rs | 2 +- crates/rust-analyzer/src/task_pool.rs | 3 ++- 3 files changed, 10 insertions(+), 2 deletions(-) diff --git a/crates/rust-analyzer/src/config.rs b/crates/rust-analyzer/src/config.rs index 6c0d712a4f..a90775933c 100644 --- a/crates/rust-analyzer/src/config.rs +++ b/crates/rust-analyzer/src/config.rs @@ -403,6 +403,9 @@ config_data! { /// Whether to show `can't find Cargo.toml` error message. notifications_cargoTomlNotFound: bool = "true", + /// How many worker threads in the main loop. The default `null` means to pick automatically. + numThreads: Option = "null", + /// Expand attribute macros. Requires `#rust-analyzer.procMacro.enable#` to be set. procMacro_attributes_enable: bool = "true", /// Enable support for procedural macros, implies `#rust-analyzer.cargo.buildScripts.enable#`. @@ -1454,6 +1457,10 @@ impl Config { } } + pub fn main_loop_num_threads(&self) -> usize { + self.data.numThreads.unwrap_or(num_cpus::get_physical().try_into().unwrap_or(1)) + } + pub fn typing_autoclose_angle(&self) -> bool { self.data.typing_autoClosingAngleBrackets_enable } diff --git a/crates/rust-analyzer/src/global_state.rs b/crates/rust-analyzer/src/global_state.rs index 4e8bc8d646..fb9415b5dc 100644 --- a/crates/rust-analyzer/src/global_state.rs +++ b/crates/rust-analyzer/src/global_state.rs @@ -134,7 +134,7 @@ impl GlobalState { let task_pool = { let (sender, receiver) = unbounded(); - let handle = TaskPool::new(sender); + let handle = TaskPool::new_with_threads(sender, config.main_loop_num_threads()); Handle { handle, receiver } }; diff --git a/crates/rust-analyzer/src/task_pool.rs b/crates/rust-analyzer/src/task_pool.rs index aeeb3b7c58..616e449984 100644 --- a/crates/rust-analyzer/src/task_pool.rs +++ b/crates/rust-analyzer/src/task_pool.rs @@ -8,12 +8,13 @@ pub(crate) struct TaskPool { } impl TaskPool { - pub(crate) fn new(sender: Sender) -> TaskPool { + pub(crate) fn new_with_threads(sender: Sender, threads: usize) -> TaskPool { const STACK_SIZE: usize = 8 * 1024 * 1024; let inner = threadpool::Builder::new() .thread_name("Worker".into()) .thread_stack_size(STACK_SIZE) + .num_threads(threads) .build(); TaskPool { sender, inner } } From 9f5a547b3f91bee46e3015eb11cce8772820398b Mon Sep 17 00:00:00 2001 From: Junliang HU Date: Fri, 9 Dec 2022 21:36:35 +0800 Subject: [PATCH 2/2] Regenerate config --- docs/user/generated_config.adoc | 5 +++++ editors/code/package.json | 9 +++++++++ 2 files changed, 14 insertions(+) diff --git a/docs/user/generated_config.adoc b/docs/user/generated_config.adoc index db41c7bf10..f59a524364 100644 --- a/docs/user/generated_config.adoc +++ b/docs/user/generated_config.adoc @@ -604,6 +604,11 @@ Number of syntax trees rust-analyzer keeps in memory. Defaults to 128. -- Whether to show `can't find Cargo.toml` error message. -- +[[rust-analyzer.numThreads]]rust-analyzer.numThreads (default: `null`):: ++ +-- +How many worker threads in the main loop. The default `null` means to pick automatically. +-- [[rust-analyzer.procMacro.attributes.enable]]rust-analyzer.procMacro.attributes.enable (default: `true`):: + -- diff --git a/editors/code/package.json b/editors/code/package.json index ad5f97bb19..26a1e81f13 100644 --- a/editors/code/package.json +++ b/editors/code/package.json @@ -1137,6 +1137,15 @@ "default": true, "type": "boolean" }, + "rust-analyzer.numThreads": { + "markdownDescription": "How many worker threads in the main loop. The default `null` means to pick automatically.", + "default": null, + "type": [ + "null", + "integer" + ], + "minimum": 0 + }, "rust-analyzer.procMacro.attributes.enable": { "markdownDescription": "Expand attribute macros. Requires `#rust-analyzer.procMacro.enable#` to be set.", "default": true,