From 947dfdce0cf241fa82262e381664c3742db08436 Mon Sep 17 00:00:00 2001 From: lucasholten Date: Tue, 31 Dec 2024 15:29:09 +0100 Subject: [PATCH] Add back optimizations --- crates/base-db/src/input.rs | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/crates/base-db/src/input.rs b/crates/base-db/src/input.rs index fe5d4091e5..b263e7382d 100644 --- a/crates/base-db/src/input.rs +++ b/crates/base-db/src/input.rs @@ -499,13 +499,18 @@ impl CrateGraph { /// Extends this crate graph by adding a complete second crate /// graph and adjust the ids in the [`ProcMacroPaths`] accordingly. /// + /// This will deduplicate the crates of the graph where possible. + /// Note that for deduplication to fully work, `self`'s crate dependencies must be sorted by crate id. + /// If the crate dependencies were sorted, the resulting graph from this `extend` call will also + /// have the crate dependencies sorted. + /// /// Returns a map mapping `other`'s IDs to the new IDs in `self`. pub fn extend( &mut self, mut other: CrateGraph, proc_macros: &mut ProcMacroPaths, ) -> FxHashMap { - self.sort_deps(); + let m = self.len(); let topo = other.crates_in_topological_order(); let mut id_map: FxHashMap = FxHashMap::default(); for topo in topo { @@ -514,9 +519,8 @@ impl CrateGraph { crate_data.dependencies.iter_mut().for_each(|dep| dep.crate_id = id_map[&dep.crate_id]); crate_data.dependencies.sort_by_key(|dep| dep.crate_id); - let find = self.arena.iter().find(|(_, v)| *v == crate_data); - let new_id = - if let Some((k, _)) = find { k } else { self.arena.alloc(crate_data.clone()) }; + let find = self.arena.iter().take(m).find_map(|(k, v)| (v == crate_data).then_some(k)); + let new_id = find.unwrap_or_else(|| self.arena.alloc(crate_data.clone())); id_map.insert(topo, new_id); }