From 5c410385fc2780238864e3e090fc4cff37b5c3fb Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 29 May 2019 16:05:14 +0300 Subject: optimization: cancel backlog in onEnter --- crates/ra_lsp_server/src/main_loop.rs | 15 ++++++++++++--- crates/ra_lsp_server/src/server_world.rs | 4 ++++ 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/crates/ra_lsp_server/src/main_loop.rs b/crates/ra_lsp_server/src/main_loop.rs index e65bb0972..d29ba94e7 100644 --- a/crates/ra_lsp_server/src/main_loop.rs +++ b/crates/ra_lsp_server/src/main_loop.rs @@ -1,7 +1,7 @@ mod handlers; mod subscriptions; -use std::{fmt, path::PathBuf, sync::Arc, time::Instant}; +use std::{fmt, path::PathBuf, sync::Arc, time::Instant, any::TypeId}; use crossbeam_channel::{select, unbounded, Receiver, RecvError, Sender}; use failure::{bail, format_err}; @@ -447,14 +447,14 @@ struct PoolDispatcher<'a> { req: Option, res: Option, pool: &'a ThreadPool, - world: &'a ServerWorldState, + world: &'a mut ServerWorldState, sender: &'a Sender, } impl<'a> PoolDispatcher<'a> { fn on(&mut self, f: fn(ServerWorld, R::Params) -> Result) -> Result<&mut Self> where - R: req::Request, + R: req::Request + 'static, R::Params: DeserializeOwned + Send + 'static, R::Result: Serialize + 'static, { @@ -464,6 +464,15 @@ impl<'a> PoolDispatcher<'a> { }; match req.cast::() { Ok((id, params)) => { + // Real time requests block user typing, so we should react quickly to them. + // Currently this means that we try to cancel background jobs if we don't have + // a spare thread. + let is_real_time = TypeId::of::() == TypeId::of::() + || TypeId::of::() == TypeId::of::(); + if self.pool.queued_count() > 0 && is_real_time { + self.world.cancel_requests(); + } + let world = self.world.snapshot(); let sender = self.sender.clone(); self.pool.execute(move || { diff --git a/crates/ra_lsp_server/src/server_world.rs b/crates/ra_lsp_server/src/server_world.rs index b63927a4f..7eb4d3e56 100644 --- a/crates/ra_lsp_server/src/server_world.rs +++ b/crates/ra_lsp_server/src/server_world.rs @@ -149,6 +149,10 @@ impl ServerWorldState { self.analysis_host.apply_change(change); } + pub fn cancel_requests(&mut self) { + self.analysis_host.apply_change(AnalysisChange::new()); + } + pub fn snapshot(&self) -> ServerWorld { ServerWorld { workspaces: Arc::clone(&self.workspaces), -- cgit v1.2.3