]> git.lizzy.rs Git - rust.git/blob - crates/ide/src/prime_caches.rs
Merge #10877
[rust.git] / crates / ide / src / prime_caches.rs
1 //! rust-analyzer is lazy and doesn't compute anything unless asked. This
2 //! sometimes is counter productive when, for example, the first goto definition
3 //! request takes longer to compute. This modules implemented prepopulation of
4 //! various caches, it's not really advanced at the moment.
5
6 use hir::db::DefDatabase;
7 use ide_db::base_db::{SourceDatabase, SourceDatabaseExt};
8 use rustc_hash::FxHashSet;
9
10 use crate::RootDatabase;
11
12 /// We started indexing a crate.
13 #[derive(Debug)]
14 pub struct PrimeCachesProgress {
15     pub on_crate: String,
16     pub n_done: usize,
17     pub n_total: usize,
18 }
19
20 pub(crate) fn prime_caches(db: &RootDatabase, cb: &(dyn Fn(PrimeCachesProgress) + Sync)) {
21     let _p = profile::span("prime_caches");
22     let graph = db.crate_graph();
23     // We're only interested in the workspace crates and the `ImportMap`s of their direct
24     // dependencies, though in practice the latter also compute the `DefMap`s.
25     // We don't prime transitive dependencies because they're generally not visible in
26     // the current workspace.
27     let to_prime: FxHashSet<_> = graph
28         .iter()
29         .filter(|&id| {
30             let file_id = graph[id].root_file_id;
31             let root_id = db.file_source_root(file_id);
32             !db.source_root(root_id).is_library
33         })
34         .flat_map(|id| graph[id].dependencies.iter().map(|krate| krate.crate_id))
35         .collect();
36
37     // FIXME: This would be easy to parallelize, since it's in the ideal ordering for that.
38     // Unfortunately rayon prevents panics from propagation out of a `scope`, which breaks
39     // cancellation, so we cannot use rayon.
40     let n_total = to_prime.len();
41     for (n_done, &crate_id) in to_prime.iter().enumerate() {
42         let crate_name = graph[crate_id].display_name.as_deref().unwrap_or_default().to_string();
43
44         cb(PrimeCachesProgress { on_crate: crate_name, n_done, n_total });
45         // This also computes the DefMap
46         db.import_map(crate_id);
47     }
48 }