From d012a20f257571124bef303b2d9bef185e91c13b Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Lauren=C8=9Biu=20Nicola?= <lnicola@dend.ro>
Date: Fri, 10 Dec 2021 16:24:07 +0200
Subject: [PATCH] Only prime direct dependencies of the workspace crates

---
 crates/ide/src/prime_caches.rs | 31 ++++++++++---------------------
 1 file changed, 10 insertions(+), 21 deletions(-)

diff --git a/crates/ide/src/prime_caches.rs b/crates/ide/src/prime_caches.rs
index 41e48c4ba56..5eba1d1e276 100644
--- a/crates/ide/src/prime_caches.rs
+++ b/crates/ide/src/prime_caches.rs
@@ -4,7 +4,7 @@
 //! various caches, it's not really advanced at the moment.
 
 use hir::db::DefDatabase;
-use ide_db::base_db::{CrateGraph, CrateId, SourceDatabase, SourceDatabaseExt};
+use ide_db::base_db::{SourceDatabase, SourceDatabaseExt};
 use rustc_hash::FxHashSet;
 
 use crate::RootDatabase;
@@ -20,7 +20,10 @@ pub struct PrimeCachesProgress {
 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 transitive dependencies of all workspace crates.
+    // 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.
     let to_prime: FxHashSet<_> = graph
         .iter()
         .filter(|&id| {
@@ -28,32 +31,18 @@ pub(crate) fn prime_caches(db: &RootDatabase, cb: &(dyn Fn(PrimeCachesProgress)
             let root_id = db.file_source_root(file_id);
             !db.source_root(root_id).is_library
         })
-        .flat_map(|id| graph.transitive_deps(id))
+        .flat_map(|id| graph[id].dependencies.iter().map(|krate| krate.crate_id))
         .collect();
 
-    let topo = toposort(&graph, &to_prime);
-
     // 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.
-    for (i, &crate_id) in topo.iter().enumerate() {
+    let n_total = to_prime.len();
+    for (n_done, &crate_id) in to_prime.iter().enumerate() {
         let crate_name = graph[crate_id].display_name.as_deref().unwrap_or_default().to_string();
 
-        cb(PrimeCachesProgress { on_crate: crate_name, n_done: i, n_total: topo.len() });
-        db.crate_def_map(crate_id);
+        cb(PrimeCachesProgress { on_crate: crate_name, n_done, n_total });
+        // This also computes the DefMap
         db.import_map(crate_id);
     }
 }
-
-fn toposort(graph: &CrateGraph, crates: &FxHashSet<CrateId>) -> Vec<CrateId> {
-    // Just subset the full topologically sorted set for simplicity.
-
-    let all = graph.crates_in_topological_order();
-    let mut result = Vec::with_capacity(crates.len());
-    for krate in all {
-        if crates.contains(&krate) {
-            result.push(krate);
-        }
-    }
-    result
-}