Skip to content

Commit fc57358

Browse files
Merge #4133
4133: main: eagerly prime goto-definition caches r=matklad a=BurntSushi This commit eagerly primes the caches used by goto-definition by submitting a "phantom" goto-definition request. This is perhaps a bit circuitous, but it does actually get the job done. The result of this change is that once RA is finished its initial loading of a project, goto-definition requests are instant. There don't appear to be any more surprise latency spikes. This _partially_ addresses #1650 in that it front-loads the latency of the first goto-definition request, which in turn makes it more predictable and less surprising. In particular, this addresses the use case where one opens the text editor, starts reading code for a while, and only later issues the first goto-definition request. Before this PR, that first goto-definition request is guaranteed to have high latency in any reasonably sized project. But after this PR, there's a good chance that it will now be instant. What this _doesn't_ address is that initial loading time. In fact, it makes it longer by adding a phantom goto-definition request to the initial startup sequence. However, I observed that while this did make initial loading slower, it was overall a somewhat small (but not insignificant) fraction of initial loading time. ----- At least, the above is what I _want_ to do. The actual change in this PR is just a proof-of-concept. I came up with after an evening of printf-debugging. Once I found the spot where this cache priming should go, I was unsure of how to generate a phantom input. So I just took an input I knew worked from my printf-debugging and hacked it in. Obviously, what I'd like to do is make this more general such that it will always work. I don't know whether this is the "right" approach or not. My guess is that there is perhaps a cleaner solution that more directly primes whatever cache is being lazily populated rather than fudging the issue with a phantom goto-definition request. I created this as a draft PR because I'd really like help making this general. I think whether y'all want to accept this patch is perhaps a separate question. IMO, it seems like a good idea, but to be honest, I'm happy to maintain this patch on my own since it's so trivial. But I would like to generalize it so that it will work in any project. My thinking is that all I really need to do is find a file and a token somewhere in the loaded project, and then use that as input. But I don't quite know how to connect all the data structures to do that. Any help would be appreciated! cc @matklad since I've been a worm in your ear about this problem. :-) Co-authored-by: Andrew Gallant <[email protected]>
2 parents 0598182 + 0c12b7e commit fc57358

File tree

2 files changed

+10
-11
lines changed

2 files changed

+10
-11
lines changed

crates/ra_ide/src/prime_caches.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,10 @@
33
//! request takes longer to compute. This modules implemented prepopulating of
44
//! various caches, it's not really advanced at the moment.
55
6-
use hir::Semantics;
7-
86
use crate::{FileId, RootDatabase};
97

108
pub(crate) fn prime_caches(db: &RootDatabase, files: Vec<FileId>) {
11-
let sema = Semantics::new(db);
129
for file in files {
13-
let _ = sema.to_module_def(file);
10+
let _ = crate::syntax_highlighting::highlight(db, file, None);
1411
}
1512
}

crates/rust-analyzer/src/main_loop.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,7 @@ pub fn main_loop(ws_roots: Vec<PathBuf>, config: Config, connection: Connection)
222222
libdata_receiver.into_iter().for_each(drop);
223223
log::info!("...tasks have finished");
224224
log::info!("joining threadpool...");
225+
pool.join();
225226
drop(pool);
226227
log::info!("...threadpool has finished");
227228

@@ -417,28 +418,29 @@ fn loop_turn(
417418
&& loop_state.pending_libraries.is_empty()
418419
&& loop_state.in_flight_libraries == 0
419420
{
421+
state_changed = true;
420422
loop_state.workspace_loaded = true;
421423
if let Some(flycheck) = &world_state.flycheck {
422424
flycheck.update();
423425
}
424-
pool.execute({
425-
let subs = loop_state.subscriptions.subscriptions();
426-
let snap = world_state.snapshot();
427-
move || snap.analysis().prime_caches(subs).unwrap_or_else(|_: Canceled| ())
428-
});
429426
}
430427

431428
if show_progress {
432429
send_startup_progress(&connection.sender, loop_state);
433430
}
434431

435-
if state_changed {
432+
if state_changed && loop_state.workspace_loaded {
436433
update_file_notifications_on_threadpool(
437434
pool,
438435
world_state.snapshot(),
439436
task_sender.clone(),
440437
loop_state.subscriptions.subscriptions(),
441-
)
438+
);
439+
pool.execute({
440+
let subs = loop_state.subscriptions.subscriptions();
441+
let snap = world_state.snapshot();
442+
move || snap.analysis().prime_caches(subs).unwrap_or_else(|_: Canceled| ())
443+
});
442444
}
443445

444446
let loop_duration = loop_start.elapsed();

0 commit comments

Comments
 (0)