Skip to content

Commit 39d631f

Browse files
committed
Debounce workspace fetching for workspace structure changes
1 parent b3e086a commit 39d631f

File tree

4 files changed

+48
-9
lines changed

4 files changed

+48
-9
lines changed

crates/rust-analyzer/src/global_state.rs

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33
//!
44
//! Each tick provides an immutable snapshot of the state as `WorldSnapshot`.
55
6-
use std::{ops::Not as _, time::Instant};
6+
use std::{
7+
ops::Not as _,
8+
time::{Duration, Instant},
9+
};
710

811
use crossbeam_channel::{Receiver, Sender, unbounded};
912
use hir::ChangeWithProcMacros;
@@ -40,6 +43,7 @@ use crate::{
4043
test_runner::{CargoTestHandle, CargoTestMessage},
4144
};
4245

46+
#[derive(Debug)]
4347
pub(crate) struct FetchWorkspaceRequest {
4448
pub(crate) path: Option<AbsPathBuf>,
4549
pub(crate) force_crate_graph_reload: bool,
@@ -114,6 +118,11 @@ pub(crate) struct GlobalState {
114118
pub(crate) discover_sender: Sender<discover::DiscoverProjectMessage>,
115119
pub(crate) discover_receiver: Receiver<discover::DiscoverProjectMessage>,
116120

121+
// Debouncing channel for fetching the workspace
122+
// we want to delay it until the VFS looks stable-ish (and thus is not currently in the middle
123+
// of a VCS operation like `git switch`)
124+
pub(crate) fetch_ws_receiver: Option<(Receiver<Instant>, FetchWorkspaceRequest)>,
125+
117126
// VFS
118127
pub(crate) loader: Handle<Box<dyn vfs::loader::Handle>, Receiver<vfs::loader::Message>>,
119128
pub(crate) vfs: Arc<RwLock<(vfs::Vfs, FxHashMap<FileId, LineEndings>)>>,
@@ -264,6 +273,8 @@ impl GlobalState {
264273
discover_sender,
265274
discover_receiver,
266275

276+
fetch_ws_receiver: None,
277+
267278
vfs: Arc::new(RwLock::new((vfs::Vfs::default(), Default::default()))),
268279
vfs_config_version: 0,
269280
vfs_progress_config_version: 0,
@@ -508,11 +519,7 @@ impl GlobalState {
508519
if let Some((path, force_crate_graph_reload)) = workspace_structure_change {
509520
let _p = span!(Level::INFO, "GlobalState::process_changes/ws_structure_change")
510521
.entered();
511-
512-
self.fetch_workspaces_queue.request_op(
513-
format!("workspace vfs file change: {path}"),
514-
FetchWorkspaceRequest { path: Some(path), force_crate_graph_reload },
515-
);
522+
self.enqueue_workspace_fetch(path, force_crate_graph_reload);
516523
}
517524
}
518525

@@ -660,6 +667,25 @@ impl GlobalState {
660667
None
661668
})
662669
}
670+
671+
fn enqueue_workspace_fetch(&mut self, path: AbsPathBuf, force_crate_graph_reload: bool) {
672+
let already_requested = self.fetch_workspaces_queue.op_requested()
673+
|| self.fetch_workspaces_queue.op_in_progress();
674+
if self.fetch_ws_receiver.is_none() && already_requested {
675+
return;
676+
}
677+
678+
self.fetch_ws_receiver = Some((
679+
crossbeam_channel::after(Duration::from_millis(100)),
680+
FetchWorkspaceRequest { path: Some(path), force_crate_graph_reload },
681+
));
682+
}
683+
684+
pub(crate) fn debounce_workspace_fetch(&mut self) {
685+
if let Some((fetch_receiver, _)) = &mut self.fetch_ws_receiver {
686+
*fetch_receiver = crossbeam_channel::after(Duration::from_millis(100));
687+
}
688+
}
663689
}
664690

665691
impl Drop for GlobalState {

crates/rust-analyzer/src/main_loop.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use std::{
88
time::{Duration, Instant},
99
};
1010

11-
use crossbeam_channel::{Receiver, select};
11+
use crossbeam_channel::{Receiver, never, select};
1212
use ide_db::base_db::{SourceDatabase, VfsPath, salsa::Database as _};
1313
use lsp_server::{Connection, Notification, Request};
1414
use lsp_types::{TextDocumentIdentifier, notification::Notification as _};
@@ -71,6 +71,7 @@ enum Event {
7171
Flycheck(FlycheckMessage),
7272
TestResult(CargoTestMessage),
7373
DiscoverProject(DiscoverProjectMessage),
74+
FetchWorkspaces(FetchWorkspaceRequest),
7475
}
7576

7677
impl fmt::Display for Event {
@@ -83,6 +84,7 @@ impl fmt::Display for Event {
8384
Event::QueuedTask(_) => write!(f, "Event::QueuedTask"),
8485
Event::TestResult(_) => write!(f, "Event::TestResult"),
8586
Event::DiscoverProject(_) => write!(f, "Event::DiscoverProject"),
87+
Event::FetchWorkspaces(_) => write!(f, "Event::SwitchWorkspaces"),
8688
}
8789
}
8890
}
@@ -150,6 +152,7 @@ impl fmt::Debug for Event {
150152
}
151153
_ => (),
152154
}
155+
153156
match self {
154157
Event::Lsp(it) => fmt::Debug::fmt(it, f),
155158
Event::Task(it) => fmt::Debug::fmt(it, f),
@@ -158,6 +161,7 @@ impl fmt::Debug for Event {
158161
Event::Flycheck(it) => fmt::Debug::fmt(it, f),
159162
Event::TestResult(it) => fmt::Debug::fmt(it, f),
160163
Event::DiscoverProject(it) => fmt::Debug::fmt(it, f),
164+
Event::FetchWorkspaces(_) => Ok(()),
161165
}
162166
}
163167
}
@@ -251,7 +255,7 @@ impl GlobalState {
251255
}
252256

253257
fn next_event(
254-
&self,
258+
&mut self,
255259
inbox: &Receiver<lsp_server::Message>,
256260
) -> Result<Option<Event>, crossbeam_channel::RecvError> {
257261
// Make sure we reply to formatting requests ASAP so the editor doesn't block
@@ -283,6 +287,10 @@ impl GlobalState {
283287

284288
recv(self.discover_receiver) -> task =>
285289
task.map(Event::DiscoverProject),
290+
291+
recv(self.fetch_ws_receiver.as_ref().map_or(&never(), |(chan, _)| chan)) -> _instant => {
292+
Ok(Event::FetchWorkspaces(self.fetch_ws_receiver.take().unwrap().1))
293+
},
286294
}
287295
.map(Some)
288296
}
@@ -412,6 +420,9 @@ impl GlobalState {
412420
self.handle_discover_msg(message);
413421
}
414422
}
423+
Event::FetchWorkspaces(req) => {
424+
self.fetch_workspaces_queue.request_op("vfs change".to_owned(), req)
425+
}
415426
}
416427
let event_handling_duration = loop_start.elapsed();
417428
let (state_changed, memdocs_added_or_removed) = if self.vfs_done {
@@ -830,6 +841,7 @@ impl GlobalState {
830841
match message {
831842
vfs::loader::Message::Changed { files } | vfs::loader::Message::Loaded { files } => {
832843
let _p = tracing::info_span!("GlobalState::handle_vfs_msg{changed/load}").entered();
844+
self.debounce_workspace_fetch();
833845
let vfs = &mut self.vfs.write().0;
834846
for (path, contents) in files {
835847
let path = VfsPath::from(path);

crates/rust-analyzer/src/op_queue.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ impl<Args, Output> Default for OpQueue<Args, Output> {
3636
}
3737
}
3838

39-
impl<Args, Output> OpQueue<Args, Output> {
39+
impl<Args: std::fmt::Debug, Output> OpQueue<Args, Output> {
4040
/// Request an operation to start.
4141
pub(crate) fn request_op(&mut self, reason: Cause, args: Args) {
4242
self.op_requested = Some((reason, args));

crates/rust-analyzer/src/reload.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ impl GlobalState {
6969
/// are ready to do semantic work.
7070
pub(crate) fn is_quiescent(&self) -> bool {
7171
self.vfs_done
72+
&& self.fetch_ws_receiver.is_none()
7273
&& !self.fetch_workspaces_queue.op_in_progress()
7374
&& !self.fetch_build_data_queue.op_in_progress()
7475
&& !self.fetch_proc_macros_queue.op_in_progress()

0 commit comments

Comments
 (0)