Skip to content

Commit 69ab8c2

Browse files
committed
Remove crate graph deduplication logic
1 parent 7d8c5ad commit 69ab8c2

File tree

9 files changed

+6
-19658
lines changed

9 files changed

+6
-19658
lines changed

src/tools/rust-analyzer/crates/base-db/src/input.rs

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -491,43 +491,29 @@ impl CrateGraph {
491491
.for_each(|(_, data)| data.dependencies.sort_by_key(|dep| dep.crate_id));
492492
}
493493

494-
/// Extends this crate graph by adding a complete disjoint second crate
494+
/// Extends this crate graph by adding a complete second crate
495495
/// graph and adjust the ids in the [`ProcMacroPaths`] accordingly.
496496
///
497-
/// This will deduplicate the crates of the graph where possible.
498-
/// Note that for deduplication to fully work, `self`'s crate dependencies must be sorted by crate id.
499-
/// If the crate dependencies were sorted, the resulting graph from this `extend` call will also
500-
/// have the crate dependencies sorted.
501-
///
502-
/// Returns a mapping from `other`'s crate ids to the new crate ids in `self`.
497+
/// Returns a map mapping `other`'s IDs to the new IDs in `self`.
503498
pub fn extend(
504499
&mut self,
505500
mut other: CrateGraph,
506501
proc_macros: &mut ProcMacroPaths,
507-
merge: impl Fn((CrateId, &mut CrateData), (CrateId, &CrateData)) -> bool,
508502
) -> FxHashMap<CrateId, CrateId> {
509-
let m = self.len();
510503
let topo = other.crates_in_topological_order();
511504
let mut id_map: FxHashMap<CrateId, CrateId> = FxHashMap::default();
512505
for topo in topo {
513506
let crate_data = &mut other.arena[topo];
514507

515508
crate_data.dependencies.iter_mut().for_each(|dep| dep.crate_id = id_map[&dep.crate_id]);
516509
crate_data.dependencies.sort_by_key(|dep| dep.crate_id);
517-
let res = self
518-
.arena
519-
.iter_mut()
520-
.take(m)
521-
.find_map(|(id, data)| merge((id, data), (topo, crate_data)).then_some(id));
522-
523-
let new_id =
524-
if let Some(res) = res { res } else { self.arena.alloc(crate_data.clone()) };
510+
511+
let new_id = self.arena.alloc(crate_data.clone());
525512
id_map.insert(topo, new_id);
526513
}
527514

528515
*proc_macros =
529516
mem::take(proc_macros).into_iter().map(|(id, macros)| (id_map[&id], macros)).collect();
530-
531517
id_map
532518
}
533519

src/tools/rust-analyzer/crates/project-model/src/tests.rs

Lines changed: 0 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -45,39 +45,6 @@ fn load_cargo_with_overrides(
4545
to_crate_graph(project_workspace)
4646
}
4747

48-
fn load_cargo_with_fake_sysroot(
49-
file_map: &mut FxHashMap<AbsPathBuf, FileId>,
50-
file: &str,
51-
) -> (CrateGraph, ProcMacroPaths) {
52-
let meta: Metadata = get_test_json_file(file);
53-
let manifest_path =
54-
ManifestPath::try_from(AbsPathBuf::try_from(meta.workspace_root.clone()).unwrap()).unwrap();
55-
let cargo_workspace = CargoWorkspace::new(meta, manifest_path);
56-
let project_workspace = ProjectWorkspace {
57-
kind: ProjectWorkspaceKind::Cargo {
58-
cargo: cargo_workspace,
59-
build_scripts: WorkspaceBuildScripts::default(),
60-
rustc: Err(None),
61-
cargo_config_extra_env: Default::default(),
62-
error: None,
63-
},
64-
sysroot: get_fake_sysroot(),
65-
rustc_cfg: Vec::new(),
66-
cfg_overrides: Default::default(),
67-
toolchain: None,
68-
target_layout: Err("target_data_layout not loaded".into()),
69-
};
70-
project_workspace.to_crate_graph(
71-
&mut {
72-
|path| {
73-
let len = file_map.len();
74-
Some(*file_map.entry(path.to_path_buf()).or_insert(FileId::from_raw(len as u32)))
75-
}
76-
},
77-
&Default::default(),
78-
)
79-
}
80-
8148
fn load_rust_project(file: &str) -> (CrateGraph, ProcMacroPaths) {
8249
let data = get_test_json_file(file);
8350
let project = rooted_project_json(data);
@@ -253,34 +220,6 @@ fn rust_project_is_proc_macro_has_proc_macro_dep() {
253220
crate_data.dependencies.iter().find(|&dep| dep.name.deref() == "proc_macro").unwrap();
254221
}
255222

256-
#[test]
257-
fn crate_graph_dedup_identical() {
258-
let (mut crate_graph, proc_macros) =
259-
load_cargo_with_fake_sysroot(&mut Default::default(), "regex-metadata.json");
260-
crate_graph.sort_deps();
261-
262-
let (d_crate_graph, mut d_proc_macros) = (crate_graph.clone(), proc_macros.clone());
263-
264-
crate_graph.extend(d_crate_graph.clone(), &mut d_proc_macros, |(_, a), (_, b)| a == b);
265-
assert!(crate_graph.iter().eq(d_crate_graph.iter()));
266-
assert_eq!(proc_macros, d_proc_macros);
267-
}
268-
269-
#[test]
270-
fn crate_graph_dedup() {
271-
let path_map = &mut Default::default();
272-
let (mut crate_graph, _proc_macros) =
273-
load_cargo_with_fake_sysroot(path_map, "ripgrep-metadata.json");
274-
assert_eq!(crate_graph.iter().count(), 81);
275-
crate_graph.sort_deps();
276-
let (regex_crate_graph, mut regex_proc_macros) =
277-
load_cargo_with_fake_sysroot(path_map, "regex-metadata.json");
278-
assert_eq!(regex_crate_graph.iter().count(), 60);
279-
280-
crate_graph.extend(regex_crate_graph, &mut regex_proc_macros, |(_, a), (_, b)| a == b);
281-
assert_eq!(crate_graph.iter().count(), 118);
282-
}
283-
284223
#[test]
285224
// FIXME Remove the ignore
286225
#[ignore = "requires nightly until the sysroot ships a cargo workspace for library on stable"]

src/tools/rust-analyzer/crates/project-model/src/workspace.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1456,7 +1456,7 @@ fn sysroot_to_crate_graph(
14561456

14571457
// Remove all crates except the ones we are interested in to keep the sysroot graph small.
14581458
let removed_mapping = cg.remove_crates_except(&marker_set);
1459-
let mapping = crate_graph.extend(cg, &mut pm, |(_, a), (_, b)| a == b);
1459+
let mapping = crate_graph.extend(cg, &mut pm);
14601460

14611461
// Map the id through the removal mapping first, then through the crate graph extension mapping.
14621462
pub_deps.iter_mut().for_each(|(_, cid, _)| {

0 commit comments

Comments
 (0)