Skip to content

Commit a3dc049

Browse files
Merge #8318
8318: Use shrink_to_fit to reduce DefMap sizes r=jonas-schievink a=jonas-schievink Especially `block_def_map` can overallocate when there's not a lot of items in the `DefMap`. This saves around 10 MB during analysis-stats. Not too much, but a cheap win. bors r+ Co-authored-by: Jonas Schievink <[email protected]>
2 parents b78f1a0 + d1bce60 commit a3dc049

File tree

4 files changed

+48
-1
lines changed

4 files changed

+48
-1
lines changed

crates/hir_def/src/item_scope.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,17 @@ impl ItemScope {
285285
buf.push('\n');
286286
}
287287
}
288+
289+
pub(crate) fn shrink_to_fit(&mut self) {
290+
self.types.shrink_to_fit();
291+
self.values.shrink_to_fit();
292+
self.macros.shrink_to_fit();
293+
self.unresolved.shrink_to_fit();
294+
self.defs.shrink_to_fit();
295+
self.impls.shrink_to_fit();
296+
self.unnamed_trait_imports.shrink_to_fit();
297+
self.legacy_macros.shrink_to_fit();
298+
}
288299
}
289300

290301
impl PerNs {

crates/hir_def/src/nameres.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,17 @@ impl DefMap {
409409
}
410410
}
411411
}
412+
413+
fn shrink_to_fit(&mut self) {
414+
self.extern_prelude.shrink_to_fit();
415+
self.exported_proc_macros.shrink_to_fit();
416+
self.diagnostics.shrink_to_fit();
417+
self.modules.shrink_to_fit();
418+
for (_, module) in self.modules.iter_mut() {
419+
module.children.shrink_to_fit();
420+
module.scope.shrink_to_fit();
421+
}
422+
}
412423
}
413424

414425
impl ModuleData {

crates/hir_def/src/nameres/collector.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,9 @@ pub(super) fn collect_defs(
109109
}
110110
}
111111
collector.collect();
112-
collector.finish()
112+
let mut def_map = collector.finish();
113+
def_map.shrink_to_fit();
114+
def_map
113115
}
114116

115117
#[derive(Copy, Clone, Debug, Eq, PartialEq)]

lib/arena/src/lib.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,29 @@ impl<T> Arena<T> {
194194
self.data.iter().enumerate().map(|(idx, value)| (Idx::from_raw(RawIdx(idx as u32)), value))
195195
}
196196

197+
/// Returns an iterator over the arena’s mutable elements.
198+
///
199+
/// ```
200+
/// let mut arena = la_arena::Arena::new();
201+
/// let idx1 = arena.alloc(20);
202+
///
203+
/// assert_eq!(arena[idx1], 20);
204+
///
205+
/// let mut iterator = arena.iter_mut();
206+
/// *iterator.next().unwrap().1 = 10;
207+
/// drop(iterator);
208+
///
209+
/// assert_eq!(arena[idx1], 10);
210+
/// ```
211+
pub fn iter_mut(
212+
&mut self,
213+
) -> impl Iterator<Item = (Idx<T>, &mut T)> + ExactSizeIterator + DoubleEndedIterator {
214+
self.data
215+
.iter_mut()
216+
.enumerate()
217+
.map(|(idx, value)| (Idx::from_raw(RawIdx(idx as u32)), value))
218+
}
219+
197220
/// Reallocates the arena to make it take up as little space as possible.
198221
pub fn shrink_to_fit(&mut self) {
199222
self.data.shrink_to_fit();

0 commit comments

Comments
 (0)