From 40109941db20180eb71b70c23c578fed5244bd74 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 25 Jan 2020 13:27:36 +0100 Subject: Use default threadpool size --- crates/ra_lsp_server/src/main_loop.rs | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) (limited to 'crates/ra_lsp_server') diff --git a/crates/ra_lsp_server/src/main_loop.rs b/crates/ra_lsp_server/src/main_loop.rs index 83adf9711..315f4a4d6 100644 --- a/crates/ra_lsp_server/src/main_loop.rs +++ b/crates/ra_lsp_server/src/main_loop.rs @@ -29,9 +29,6 @@ use crate::{ Result, ServerConfig, }; -const THREADPOOL_SIZE: usize = 8; -const MAX_IN_FLIGHT_LIBS: usize = THREADPOOL_SIZE - 3; - #[derive(Debug)] pub struct LspError { pub code: i32, @@ -168,7 +165,7 @@ pub fn main_loop( ) }; - let pool = ThreadPool::new(THREADPOOL_SIZE); + let pool = ThreadPool::default(); let (task_sender, task_receiver) = unbounded::(); let (libdata_sender, libdata_receiver) = unbounded::(); @@ -371,7 +368,8 @@ fn loop_turn( loop_state.pending_libraries.extend(changes); } - while loop_state.in_flight_libraries < MAX_IN_FLIGHT_LIBS + let max_in_flight_libs = pool.max_count().saturating_sub(2).max(1); + while loop_state.in_flight_libraries < max_in_flight_libs && !loop_state.pending_libraries.is_empty() { let (root, files) = loop_state.pending_libraries.pop().unwrap(); -- cgit v1.2.3 From 493a903f226d148fec4b539f65b78a408e4dcb2c Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sun, 26 Jan 2020 12:02:56 +0100 Subject: Bump main thread priority on windows --- crates/ra_lsp_server/Cargo.toml | 3 +++ crates/ra_lsp_server/src/main_loop.rs | 19 +++++++++++++++++++ 2 files changed, 22 insertions(+) (limited to 'crates/ra_lsp_server') diff --git a/crates/ra_lsp_server/Cargo.toml b/crates/ra_lsp_server/Cargo.toml index 5df0496dd..fdf81ed87 100644 --- a/crates/ra_lsp_server/Cargo.toml +++ b/crates/ra_lsp_server/Cargo.toml @@ -30,6 +30,9 @@ env_logger = { version = "0.7.1", default-features = false } ra_cargo_watch = { path = "../ra_cargo_watch" } either = "1.5" +[target.'cfg(windows)'.dependencies] +winapi = "0.3" + [dev-dependencies] tempfile = "3" test_utils = { path = "../test_utils" } diff --git a/crates/ra_lsp_server/src/main_loop.rs b/crates/ra_lsp_server/src/main_loop.rs index 315f4a4d6..15bf519c9 100644 --- a/crates/ra_lsp_server/src/main_loop.rs +++ b/crates/ra_lsp_server/src/main_loop.rs @@ -57,6 +57,25 @@ pub fn main_loop( ) -> Result<()> { log::info!("server_config: {:#?}", config); + // Windows scheduler implements priority boosts: if thread waits for an + // event (like a condvar), and event fires, priority of the thread is + // temporary bumped. This optimization backfires in our case: each time the + // `main_loop` schedules a task to run on a threadpool, the worker threads + // gets a higher priority, and (on a machine with fewer cores) displaces the + // main loop! We work-around this by marking the main loop as a + // higher-priority thread. + // + // https://docs.microsoft.com/en-us/windows/win32/procthread/scheduling-priorities + // https://docs.microsoft.com/en-us/windows/win32/procthread/priority-boosts + // https://github.com/rust-analyzer/rust-analyzer/issues/2835 + #[cfg(windows)] + unsafe { + use winapi::um::processthreadsapi::*; + let thread = GetCurrentThread(); + let thread_priority_above_normal = 1; + SetThreadPriority(thread, thread_priority_above_normal); + } + let mut loop_state = LoopState::default(); let mut world_state = { let feature_flags = { -- cgit v1.2.3