Skip to content

Commit cdc9724

Browse files
committed
Automatically sort crate graph
1 parent 86441c5 commit cdc9724

File tree

2 files changed

+7
-11
lines changed

2 files changed

+7
-11
lines changed

crates/base-db/src/input.rs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -490,26 +490,24 @@ impl CrateGraph {
490490
}
491491
}
492492

493-
pub fn sort_deps(&mut self) {
494-
self.arena
495-
.iter_mut()
496-
.for_each(|(_, data)| data.dependencies.sort_by_key(|dep| dep.crate_id));
497-
}
498-
499493
/// Extends this crate graph by adding a complete second crate
500494
/// graph and adjust the ids in the [`ProcMacroPaths`] accordingly.
501495
///
502496
/// This will deduplicate the crates of the graph where possible.
503-
/// Note that for deduplication to fully work, `self`'s crate dependencies must be sorted by crate id.
504-
/// If the crate dependencies were sorted, the resulting graph from this `extend` call will also
505-
/// have the crate dependencies sorted.
497+
/// Furthermore dependencies are sorted by crate id to make deduplication easier.
506498
///
507499
/// Returns a map mapping `other`'s IDs to the new IDs in `self`.
508500
pub fn extend(
509501
&mut self,
510502
mut other: CrateGraph,
511503
proc_macros: &mut ProcMacroPaths,
512504
) -> FxHashMap<CrateId, CrateId> {
505+
// Sorting here is a bit pointless because the input is likely already sorted.
506+
// However, the overhead is small and it makes the `extend` method harder to misuse.
507+
self.arena
508+
.iter_mut()
509+
.for_each(|(_, data)| data.dependencies.sort_by_key(|dep| dep.crate_id));
510+
513511
let m = self.len();
514512
let topo = other.crates_in_topological_order();
515513
let mut id_map: FxHashMap<CrateId, CrateId> = FxHashMap::default();

crates/project-model/src/tests.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,6 @@ fn rust_project_is_proc_macro_has_proc_macro_dep() {
232232
#[test]
233233
fn crate_graph_dedup_identical() {
234234
let (mut crate_graph, proc_macros) = load_cargo("regex-metadata.json");
235-
crate_graph.sort_deps();
236235

237236
let (d_crate_graph, mut d_proc_macros) = (crate_graph.clone(), proc_macros.clone());
238237

@@ -253,7 +252,6 @@ fn crate_graph_dedup() {
253252
let (regex_crate_graph, mut regex_proc_macros) = to_crate_graph(regex_workspace, &mut file_map);
254253
assert_eq!(regex_crate_graph.iter().count(), 50);
255254

256-
crate_graph.sort_deps();
257255
crate_graph.extend(regex_crate_graph, &mut regex_proc_macros);
258256
assert_eq!(crate_graph.iter().count(), 108);
259257
}

0 commit comments

Comments
 (0)