rust/crates/ide/src/prime_caches.rs

49 lines
1.9 KiB
Rust
Raw Normal View History

2021-07-15 21:28:30 +02:00
//! rust-analyzer is lazy and doesn't compute anything unless asked. This
2020-03-05 12:42:04 +01:00
//! sometimes is counter productive when, for example, the first goto definition
2021-07-15 21:28:30 +02:00
//! request takes longer to compute. This modules implemented prepopulation of
2020-03-05 12:42:04 +01:00
//! various caches, it's not really advanced at the moment.
use hir::db::DefDatabase;
use ide_db::base_db::{SourceDatabase, SourceDatabaseExt};
2021-11-11 14:39:20 +01:00
use rustc_hash::FxHashSet;
2020-03-05 12:42:04 +01:00
use crate::RootDatabase;
/// We started indexing a crate.
#[derive(Debug)]
pub struct PrimeCachesProgress {
pub on_crate: String,
pub n_done: usize,
pub n_total: usize,
}
pub(crate) fn prime_caches(db: &RootDatabase, cb: &(dyn Fn(PrimeCachesProgress) + Sync)) {
let _p = profile::span("prime_caches");
let graph = db.crate_graph();
// We're only interested in the workspace crates and the `ImportMap`s of their direct
// dependencies, though in practice the latter also compute the `DefMap`s.
// We don't prime transitive dependencies because they're generally not visible in
// the current workspace.
2021-11-11 14:39:20 +01:00
let to_prime: FxHashSet<_> = graph
.iter()
.filter(|&id| {
let file_id = graph[id].root_file_id;
let root_id = db.file_source_root(file_id);
!db.source_root(root_id).is_library
})
.flat_map(|id| graph[id].dependencies.iter().map(|krate| krate.crate_id))
2021-11-11 14:39:20 +01:00
.collect();
// FIXME: This would be easy to parallelize, since it's in the ideal ordering for that.
// Unfortunately rayon prevents panics from propagation out of a `scope`, which breaks
// cancellation, so we cannot use rayon.
let n_total = to_prime.len();
for (n_done, &crate_id) in to_prime.iter().enumerate() {
2021-06-11 01:27:20 +03:00
let crate_name = graph[crate_id].display_name.as_deref().unwrap_or_default().to_string();
cb(PrimeCachesProgress { on_crate: crate_name, n_done, n_total });
// This also computes the DefMap
2021-06-11 01:27:20 +03:00
db.import_map(crate_id);
2020-03-05 12:42:04 +01:00
}
}