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.
7 use hir::db::DefDatabase;
10 salsa::{Database, ParallelDatabase, Snapshot},
11 CrateGraph, CrateId, SourceDatabase, SourceDatabaseExt,
15 use rustc_hash::FxHashSet;
17 use crate::RootDatabase;
19 /// We're indexing many crates.
21 pub struct ParallelPrimeCachesProgress {
22 /// the crates that we are currently priming.
23 pub crates_currently_indexing: Vec<String>,
24 /// the total number of crates we want to prime.
25 pub crates_total: usize,
26 /// the total number of crates that have finished priming
27 pub crates_done: usize,
30 pub(crate) fn parallel_prime_caches(
32 num_worker_threads: u8,
33 cb: &(dyn Fn(ParallelPrimeCachesProgress) + Sync),
35 let _p = profile::span("prime_caches");
37 let graph = db.crate_graph();
38 let mut crates_to_prime = {
39 let crate_ids = compute_crates_to_prime(db, &graph);
41 let mut builder = topologic_sort::TopologicalSortIter::builder();
43 for &crate_id in &crate_ids {
44 let crate_data = &graph[crate_id];
45 let dependencies = crate_data
49 .filter(|i| crate_ids.contains(i));
51 builder.add(crate_id, dependencies);
57 crossbeam_utils::thread::scope(move |s| {
58 let (work_sender, work_receiver) = crossbeam_channel::unbounded();
59 let (progress_sender, progress_receiver) = crossbeam_channel::unbounded();
61 enum ParallelPrimeCacheWorkerProgress {
62 BeginCrate { crate_id: CrateId, crate_name: String },
63 EndCrate { crate_id: CrateId },
66 let prime_caches_worker = move |db: Snapshot<RootDatabase>| {
67 while let Ok((crate_id, crate_name)) = work_receiver.recv() {
69 .send(ParallelPrimeCacheWorkerProgress::BeginCrate { crate_id, crate_name })?;
71 // This also computes the DefMap
72 db.import_map(crate_id);
74 progress_sender.send(ParallelPrimeCacheWorkerProgress::EndCrate { crate_id })?;
77 Ok::<_, crossbeam_channel::SendError<_>>(())
80 for _ in 0..num_worker_threads {
81 let worker = prime_caches_worker.clone();
82 let db = db.snapshot();
83 s.spawn(move |_| worker(db));
86 let crates_total = crates_to_prime.len();
87 let mut crates_done = 0;
89 // an index map is used to preserve ordering so we can sort the progress report in order of
90 // "longest crate to index" first
91 let mut crates_currently_indexing =
92 FxIndexMap::with_capacity_and_hasher(num_worker_threads as _, Default::default());
94 while !crates_to_prime.is_empty() {
95 db.unwind_if_cancelled();
97 for crate_id in &mut crates_to_prime {
101 graph[crate_id].display_name.as_deref().unwrap_or_default().to_string(),
106 let worker_progress = match progress_receiver.recv() {
109 // our workers may have died from a cancelled task, so we'll check and re-raise here.
110 db.unwind_if_cancelled();
114 match worker_progress {
115 ParallelPrimeCacheWorkerProgress::BeginCrate { crate_id, crate_name } => {
116 crates_currently_indexing.insert(crate_id, crate_name);
118 ParallelPrimeCacheWorkerProgress::EndCrate { crate_id } => {
119 crates_currently_indexing.remove(&crate_id);
120 crates_to_prime.mark_done(crate_id);
125 let progress = ParallelPrimeCachesProgress {
126 crates_currently_indexing: crates_currently_indexing.values().cloned().collect(),
137 fn compute_crates_to_prime(db: &RootDatabase, graph: &CrateGraph) -> FxHashSet<CrateId> {
138 // We're only interested in the workspace crates and the `ImportMap`s of their direct
139 // dependencies, though in practice the latter also compute the `DefMap`s.
140 // We don't prime transitive dependencies because they're generally not visible in
141 // the current workspace.
145 let file_id = graph[id].root_file_id;
146 let root_id = db.file_source_root(file_id);
147 !db.source_root(root_id).is_library
149 .flat_map(|id| graph[id].dependencies.iter().map(|krate| krate.crate_id))