44//! various caches, it's not really advanced at the moment.
55
66use 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
910use crate :: RootDatabase ;
1011
@@ -19,7 +20,18 @@ pub struct PrimeCachesProgress {
1920pub ( 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