Skip to content

Commit 2534b7d

Browse files
bors[bot]Veykril
andauthored
Merge #10975
10975: internal: Shrink TraitImpls and InherentImpls HashMaps r=Veykril a=Veykril bors r+ Co-authored-by: Lukas Wirth <[email protected]>
2 parents 9eb5a96 + c469f8a commit 2534b7d

File tree

1 file changed

+30
-12
lines changed

1 file changed

+30
-12
lines changed

crates/hir_ty/src/method_resolution.rs

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ impl TraitImpls {
142142

143143
let crate_def_map = db.crate_def_map(krate);
144144
impls.collect_def_map(db, &crate_def_map);
145+
impls.shrink_to_fit();
145146

146147
Arc::new(impls)
147148
}
@@ -155,10 +156,32 @@ impl TraitImpls {
155156

156157
let block_def_map = db.block_def_map(block)?;
157158
impls.collect_def_map(db, &block_def_map);
159+
impls.shrink_to_fit();
158160

159161
Some(Arc::new(impls))
160162
}
161163

164+
pub(crate) fn trait_impls_in_deps_query(db: &dyn HirDatabase, krate: CrateId) -> Arc<Self> {
165+
let _p = profile::span("trait_impls_in_deps_query");
166+
let crate_graph = db.crate_graph();
167+
let mut res = Self { map: FxHashMap::default() };
168+
169+
for krate in crate_graph.transitive_deps(krate) {
170+
res.merge(&db.trait_impls_in_crate(krate));
171+
}
172+
res.shrink_to_fit();
173+
174+
Arc::new(res)
175+
}
176+
177+
fn shrink_to_fit(&mut self) {
178+
self.map.shrink_to_fit();
179+
self.map.values_mut().for_each(|map| {
180+
map.shrink_to_fit();
181+
map.values_mut().for_each(Vec::shrink_to_fit);
182+
});
183+
}
184+
162185
fn collect_def_map(&mut self, db: &dyn HirDatabase, def_map: &DefMap) {
163186
for (_module_id, module_data) in def_map.modules() {
164187
for impl_id in module_data.scope.impls() {
@@ -187,18 +210,6 @@ impl TraitImpls {
187210
}
188211
}
189212

190-
pub(crate) fn trait_impls_in_deps_query(db: &dyn HirDatabase, krate: CrateId) -> Arc<Self> {
191-
let _p = profile::span("trait_impls_in_deps_query");
192-
let crate_graph = db.crate_graph();
193-
let mut res = Self { map: FxHashMap::default() };
194-
195-
for krate in crate_graph.transitive_deps(krate) {
196-
res.merge(&db.trait_impls_in_crate(krate));
197-
}
198-
199-
Arc::new(res)
200-
}
201-
202213
fn merge(&mut self, other: &Self) {
203214
for (trait_, other_map) in &other.map {
204215
let map = self.map.entry(*trait_).or_default();
@@ -264,6 +275,7 @@ impl InherentImpls {
264275

265276
let crate_def_map = db.crate_def_map(krate);
266277
impls.collect_def_map(db, &crate_def_map);
278+
impls.shrink_to_fit();
267279

268280
return Arc::new(impls);
269281
}
@@ -275,11 +287,17 @@ impl InherentImpls {
275287
let mut impls = Self { map: FxHashMap::default() };
276288
if let Some(block_def_map) = db.block_def_map(block) {
277289
impls.collect_def_map(db, &block_def_map);
290+
impls.shrink_to_fit();
278291
return Some(Arc::new(impls));
279292
}
280293
return None;
281294
}
282295

296+
fn shrink_to_fit(&mut self) {
297+
self.map.values_mut().for_each(Vec::shrink_to_fit);
298+
self.map.shrink_to_fit();
299+
}
300+
283301
fn collect_def_map(&mut self, db: &dyn HirDatabase, def_map: &DefMap) {
284302
for (_module_id, module_data) in def_map.modules() {
285303
for impl_id in module_data.scope.impls() {

0 commit comments

Comments
 (0)