From 478ba65f8da6ffd4a3fea09c6a4a1f0fb92e1c85 Mon Sep 17 00:00:00 2001 From: Emil Lauridsen Date: Wed, 15 Jan 2020 15:50:49 +0100 Subject: Manage check state updates in main_loop to reduce lock contention --- crates/ra_lsp_server/src/main_loop.rs | 28 ++++++++++++++++++++++++++-- crates/ra_lsp_server/src/world.rs | 6 +++--- 2 files changed, 29 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 84012b99d..e087c4f7e 100644 --- a/crates/ra_lsp_server/src/main_loop.rs +++ b/crates/ra_lsp_server/src/main_loop.rs @@ -337,11 +337,34 @@ fn loop_turn( loop_state.in_flight_libraries -= 1; } Event::CheckWatcher(task) => match task { - CheckTask::Update(uri) => { + CheckTask::ClearDiagnostics => { + let cleared_files = world_state.check_watcher.state.write().clear(); + + // Send updated diagnostics for each cleared file + for url in cleared_files { + let path = url.to_file_path().map_err(|()| format!("invalid uri: {}", url))?; + if let Some(file_id) = world_state.vfs.read().path2file(&path) { + let params = handlers::publish_diagnostics( + &world_state.snapshot(), + FileId(file_id.0), + )?; + let not = notification_new::(params); + task_sender.send(Task::Notify(not)).unwrap(); + } + } + } + + CheckTask::AddDiagnostic(url, diagnostic) => { + world_state + .check_watcher + .state + .write() + .add_diagnostic_with_fixes(url.clone(), diagnostic); + // We manually send a diagnostic update when the watcher asks // us to, to avoid the issue of having to change the file to // receive updated diagnostics. - let path = uri.to_file_path().map_err(|()| format!("invalid uri: {}", uri))?; + let path = url.to_file_path().map_err(|()| format!("invalid uri: {}", url))?; if let Some(file_id) = world_state.vfs.read().path2file(&path) { let params = handlers::publish_diagnostics(&world_state.snapshot(), FileId(file_id.0))?; @@ -349,6 +372,7 @@ fn loop_turn( task_sender.send(Task::Notify(not)).unwrap(); } } + CheckTask::Status(progress) => { let params = req::ProgressParams { token: req::ProgressToken::String("rustAnalyzer/cargoWatcher".to_string()), diff --git a/crates/ra_lsp_server/src/world.rs b/crates/ra_lsp_server/src/world.rs index c0175c726..7a3030a51 100644 --- a/crates/ra_lsp_server/src/world.rs +++ b/crates/ra_lsp_server/src/world.rs @@ -13,7 +13,7 @@ use lsp_server::ErrorCode; use lsp_types::Url; use parking_lot::RwLock; use ra_cargo_watch::{ - url_from_path_with_drive_lowercasing, CheckOptions, CheckWatcher, CheckWatcherSharedState, + url_from_path_with_drive_lowercasing, CheckOptions, CheckState, CheckWatcher, }; use ra_ide::{ Analysis, AnalysisChange, AnalysisHost, CrateGraph, FeatureFlags, FileId, LibraryData, @@ -64,7 +64,7 @@ pub struct WorldSnapshot { pub analysis: Analysis, pub vfs: Arc>, pub latest_requests: Arc>, - pub check_watcher: Arc>, + pub check_watcher: Arc>, } impl WorldState { @@ -220,7 +220,7 @@ impl WorldState { analysis: self.analysis_host.analysis(), vfs: Arc::clone(&self.vfs), latest_requests: Arc::clone(&self.latest_requests), - check_watcher: self.check_watcher.shared.clone(), + check_watcher: self.check_watcher.state.clone(), } } -- cgit v1.2.3 From 7a8c6351bf50c1dcfb111be9f91da3c1f9cf2ec3 Mon Sep 17 00:00:00 2001 From: Emil Lauridsen Date: Wed, 15 Jan 2020 16:33:58 +0100 Subject: Extract check task handling into function --- crates/ra_lsp_server/src/main_loop.rs | 103 ++++++++++++++++++---------------- 1 file changed, 56 insertions(+), 47 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 e087c4f7e..7822be2e2 100644 --- a/crates/ra_lsp_server/src/main_loop.rs +++ b/crates/ra_lsp_server/src/main_loop.rs @@ -9,7 +9,7 @@ use std::{error::Error, fmt, panic, path::PathBuf, sync::Arc, time::Instant}; use crossbeam_channel::{select, unbounded, RecvError, Sender}; use lsp_server::{Connection, ErrorCode, Message, Notification, Request, RequestId, Response}; -use lsp_types::{ClientCapabilities, NumberOrString}; +use lsp_types::{ClientCapabilities, NumberOrString, Url}; use ra_cargo_watch::{CheckOptions, CheckTask}; use ra_ide::{Canceled, FeatureFlags, FileId, LibraryData, SourceRootId}; use ra_prof::profile; @@ -336,52 +336,7 @@ fn loop_turn( world_state.maybe_collect_garbage(); loop_state.in_flight_libraries -= 1; } - Event::CheckWatcher(task) => match task { - CheckTask::ClearDiagnostics => { - let cleared_files = world_state.check_watcher.state.write().clear(); - - // Send updated diagnostics for each cleared file - for url in cleared_files { - let path = url.to_file_path().map_err(|()| format!("invalid uri: {}", url))?; - if let Some(file_id) = world_state.vfs.read().path2file(&path) { - let params = handlers::publish_diagnostics( - &world_state.snapshot(), - FileId(file_id.0), - )?; - let not = notification_new::(params); - task_sender.send(Task::Notify(not)).unwrap(); - } - } - } - - CheckTask::AddDiagnostic(url, diagnostic) => { - world_state - .check_watcher - .state - .write() - .add_diagnostic_with_fixes(url.clone(), diagnostic); - - // We manually send a diagnostic update when the watcher asks - // us to, to avoid the issue of having to change the file to - // receive updated diagnostics. - let path = url.to_file_path().map_err(|()| format!("invalid uri: {}", url))?; - if let Some(file_id) = world_state.vfs.read().path2file(&path) { - let params = - handlers::publish_diagnostics(&world_state.snapshot(), FileId(file_id.0))?; - let not = notification_new::(params); - task_sender.send(Task::Notify(not)).unwrap(); - } - } - - CheckTask::Status(progress) => { - let params = req::ProgressParams { - token: req::ProgressToken::String("rustAnalyzer/cargoWatcher".to_string()), - value: req::ProgressParamsValue::WorkDone(progress), - }; - let not = notification_new::(params); - task_sender.send(Task::Notify(not)).unwrap(); - } - }, + Event::CheckWatcher(task) => on_check_task(task, world_state, task_sender)?, Event::Msg(msg) => match msg { Message::Request(req) => on_request( world_state, @@ -629,6 +584,60 @@ fn on_notification( Ok(()) } +fn on_check_task( + task: CheckTask, + world_state: &WorldState, + task_sender: &Sender, +) -> Result<()> { + match task { + CheckTask::ClearDiagnostics => { + let cleared_files = world_state.check_watcher.state.write().clear(); + + // Send updated diagnostics for each cleared file + for url in cleared_files { + publish_diagnostics_for_url(&url, world_state, task_sender)?; + } + } + + CheckTask::AddDiagnostic(url, diagnostic) => { + world_state + .check_watcher + .state + .write() + .add_diagnostic_with_fixes(url.clone(), diagnostic); + + // We manually send a diagnostic update when the watcher asks + // us to, to avoid the issue of having to change the file to + // receive updated diagnostics. + publish_diagnostics_for_url(&url, world_state, task_sender)?; + } + + CheckTask::Status(progress) => { + let params = req::ProgressParams { + token: req::ProgressToken::String("rustAnalyzer/cargoWatcher".to_string()), + value: req::ProgressParamsValue::WorkDone(progress), + }; + let not = notification_new::(params); + task_sender.send(Task::Notify(not)).unwrap(); + } + } + Ok(()) +} + +fn publish_diagnostics_for_url( + url: &Url, + world_state: &WorldState, + task_sender: &Sender, +) -> Result<()> { + let path = url.to_file_path().map_err(|()| format!("invalid uri: {}", url))?; + if let Some(file_id) = world_state.vfs.read().path2file(&path) { + let params = handlers::publish_diagnostics(&world_state.snapshot(), FileId(file_id.0))?; + let not = notification_new::(params); + task_sender.send(Task::Notify(not)).unwrap(); + } + Ok(()) +} + struct PoolDispatcher<'a> { req: Option, pool: &'a ThreadPool, -- cgit v1.2.3