Skip to content

Commit e42ff91

Browse files
Merge #10743
10743: feat: index fewer crates on startup/reload r=jonas-schievink a=jonas-schievink Before this PR, we used to index every crate in the `CrateGraph`, which includes every test, benchmark and example of all packages everywhere. The point of indexing is to speed up future queries, so indexing lots of tiny crates users are unlikely to open isn't really helpful. This PR instead makes us index only the transitive dependencies of all workspace crates. This reduces the number of crates we index in the rust-analyzer repo from 617 to 177 (!). Time is not impacted by that much, because most of the skipped crates are tiny. bors r+ Co-authored-by: Jonas Schievink <[email protected]>
2 parents 3b3063f + acbe8bc commit e42ff91

File tree

1 file changed

+27
-2
lines changed

1 file changed

+27
-2
lines changed

crates/ide/src/prime_caches.rs

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
//! various caches, it's not really advanced at the moment.
55
66
use hir::db::DefDatabase;
7-
use ide_db::base_db::SourceDatabase;
7+
use ide_db::base_db::{CrateGraph, CrateId, SourceDatabase, SourceDatabaseExt};
8+
use rustc_hash::FxHashSet;
89

910
use crate::RootDatabase;
1011

@@ -19,7 +20,18 @@ pub struct PrimeCachesProgress {
1920
pub(crate) fn prime_caches(db: &RootDatabase, cb: &(dyn Fn(PrimeCachesProgress) + Sync)) {
2021
let _p = profile::span("prime_caches");
2122
let graph = db.crate_graph();
22-
let topo = &graph.crates_in_topological_order();
23+
// We're only interested in the transitive dependencies of all workspace crates.
24+
let to_prime: FxHashSet<_> = graph
25+
.iter()
26+
.filter(|&id| {
27+
let file_id = graph[id].root_file_id;
28+
let root_id = db.file_source_root(file_id);
29+
!db.source_root(root_id).is_library
30+
})
31+
.flat_map(|id| graph.transitive_deps(id))
32+
.collect();
33+
34+
let topo = toposort(&graph, &to_prime);
2335

2436
// FIXME: This would be easy to parallelize, since it's in the ideal ordering for that.
2537
// Unfortunately rayon prevents panics from propagation out of a `scope`, which breaks
@@ -32,3 +44,16 @@ pub(crate) fn prime_caches(db: &RootDatabase, cb: &(dyn Fn(PrimeCachesProgress)
3244
db.import_map(crate_id);
3345
}
3446
}
47+
48+
fn toposort(graph: &CrateGraph, crates: &FxHashSet<CrateId>) -> Vec<CrateId> {
49+
// Just subset the full topologically sorted set for simplicity.
50+
51+
let all = graph.crates_in_topological_order();
52+
let mut result = Vec::with_capacity(crates.len());
53+
for krate in all {
54+
if crates.contains(&krate) {
55+
result.push(krate);
56+
}
57+
}
58+
result
59+
}

0 commit comments

Comments
 (0)