From c469f8abcbefb33198a9ae25589205fd020232c2 Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Thu, 9 Dec 2021 18:39:46 +0100 Subject: [PATCH] internal: Shrink TraitImpls and InherentImpls HashMaps --- crates/hir_ty/src/method_resolution.rs | 42 ++++++++++++++++++-------- 1 file changed, 30 insertions(+), 12 deletions(-) diff --git a/crates/hir_ty/src/method_resolution.rs b/crates/hir_ty/src/method_resolution.rs index 1e53d392e8e..3fbb6fea197 100644 --- a/crates/hir_ty/src/method_resolution.rs +++ b/crates/hir_ty/src/method_resolution.rs @@ -142,6 +142,7 @@ impl TraitImpls { let crate_def_map = db.crate_def_map(krate); impls.collect_def_map(db, &crate_def_map); + impls.shrink_to_fit(); Arc::new(impls) } @@ -155,10 +156,32 @@ impl TraitImpls { let block_def_map = db.block_def_map(block)?; impls.collect_def_map(db, &block_def_map); + impls.shrink_to_fit(); Some(Arc::new(impls)) } + pub(crate) fn trait_impls_in_deps_query(db: &dyn HirDatabase, krate: CrateId) -> Arc { + let _p = profile::span("trait_impls_in_deps_query"); + let crate_graph = db.crate_graph(); + let mut res = Self { map: FxHashMap::default() }; + + for krate in crate_graph.transitive_deps(krate) { + res.merge(&db.trait_impls_in_crate(krate)); + } + res.shrink_to_fit(); + + Arc::new(res) + } + + fn shrink_to_fit(&mut self) { + self.map.shrink_to_fit(); + self.map.values_mut().for_each(|map| { + map.shrink_to_fit(); + map.values_mut().for_each(Vec::shrink_to_fit); + }); + } + fn collect_def_map(&mut self, db: &dyn HirDatabase, def_map: &DefMap) { for (_module_id, module_data) in def_map.modules() { for impl_id in module_data.scope.impls() { @@ -187,18 +210,6 @@ impl TraitImpls { } } - pub(crate) fn trait_impls_in_deps_query(db: &dyn HirDatabase, krate: CrateId) -> Arc { - let _p = profile::span("trait_impls_in_deps_query"); - let crate_graph = db.crate_graph(); - let mut res = Self { map: FxHashMap::default() }; - - for krate in crate_graph.transitive_deps(krate) { - res.merge(&db.trait_impls_in_crate(krate)); - } - - Arc::new(res) - } - fn merge(&mut self, other: &Self) { for (trait_, other_map) in &other.map { let map = self.map.entry(*trait_).or_default(); @@ -264,6 +275,7 @@ impl InherentImpls { let crate_def_map = db.crate_def_map(krate); impls.collect_def_map(db, &crate_def_map); + impls.shrink_to_fit(); return Arc::new(impls); } @@ -275,11 +287,17 @@ impl InherentImpls { let mut impls = Self { map: FxHashMap::default() }; if let Some(block_def_map) = db.block_def_map(block) { impls.collect_def_map(db, &block_def_map); + impls.shrink_to_fit(); return Some(Arc::new(impls)); } return None; } + fn shrink_to_fit(&mut self) { + self.map.values_mut().for_each(Vec::shrink_to_fit); + self.map.shrink_to_fit(); + } + fn collect_def_map(&mut self, db: &dyn HirDatabase, def_map: &DefMap) { for (_module_id, module_data) in def_map.modules() { for impl_id in module_data.scope.impls() {