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 <jonasschievink@gmail.com>
This commit is contained in:
bors[bot]
2021-04-03 21:47:39 +00:00
committed by GitHub
4 changed files with 48 additions and 1 deletions
+11
View File
@@ -285,6 +285,17 @@ pub(crate) fn dump(&self, buf: &mut String) {
buf.push('\n');
}
}
pub(crate) fn shrink_to_fit(&mut self) {
self.types.shrink_to_fit();
self.values.shrink_to_fit();
self.macros.shrink_to_fit();
self.unresolved.shrink_to_fit();
self.defs.shrink_to_fit();
self.impls.shrink_to_fit();
self.unnamed_trait_imports.shrink_to_fit();
self.legacy_macros.shrink_to_fit();
}
}
impl PerNs {
+11
View File
@@ -409,6 +409,17 @@ fn go(buf: &mut String, map: &DefMap, path: &str, module: LocalModuleId) {
}
}
}
fn shrink_to_fit(&mut self) {
self.extern_prelude.shrink_to_fit();
self.exported_proc_macros.shrink_to_fit();
self.diagnostics.shrink_to_fit();
self.modules.shrink_to_fit();
for (_, module) in self.modules.iter_mut() {
module.children.shrink_to_fit();
module.scope.shrink_to_fit();
}
}
}
impl ModuleData {
+3 -1
View File
@@ -109,7 +109,9 @@ pub(super) fn collect_defs(
}
}
collector.collect();
collector.finish()
let mut def_map = collector.finish();
def_map.shrink_to_fit();
def_map
}
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
+23
View File
@@ -194,6 +194,29 @@ pub fn iter(
self.data.iter().enumerate().map(|(idx, value)| (Idx::from_raw(RawIdx(idx as u32)), value))
}
/// Returns an iterator over the arenas mutable elements.
///
/// ```
/// let mut arena = la_arena::Arena::new();
/// let idx1 = arena.alloc(20);
///
/// assert_eq!(arena[idx1], 20);
///
/// let mut iterator = arena.iter_mut();
/// *iterator.next().unwrap().1 = 10;
/// drop(iterator);
///
/// assert_eq!(arena[idx1], 10);
/// ```
pub fn iter_mut(
&mut self,
) -> impl Iterator<Item = (Idx<T>, &mut T)> + ExactSizeIterator + DoubleEndedIterator {
self.data
.iter_mut()
.enumerate()
.map(|(idx, value)| (Idx::from_raw(RawIdx(idx as u32)), value))
}
/// Reallocates the arena to make it take up as little space as possible.
pub fn shrink_to_fit(&mut self) {
self.data.shrink_to_fit();