Skip to content

internal: Only prime direct dependencies of the workspace crates #10978

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 10, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 10 additions & 21 deletions crates/ide/src/prime_caches.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -20,40 +20,29 @@ 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| {
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.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();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this method used elsewhere?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's still used in a text fixture, but I'm not sure the order really matters there.

let mut result = Vec::with_capacity(crates.len());
for krate in all {
if crates.contains(&krate) {
result.push(krate);
}
}
result
}