Shrink arenas after building ItemTree

This commit is contained in:
Jonas Schievink 2020-06-24 16:14:58 +02:00
parent 94169ee504
commit d4ddec2bdf
2 changed files with 49 additions and 0 deletions

View file

@ -116,6 +116,9 @@ impl<T> Arena<T> {
) -> impl Iterator<Item = (Idx<T>, &T)> + ExactSizeIterator + DoubleEndedIterator {
self.data.iter().enumerate().map(|(idx, value)| (Idx::from_raw(RawId(idx as u32)), value))
}
pub fn shrink_to_fit(&mut self) {
self.data.shrink_to_fit();
}
}
impl<T> Default for Arena<T> {

View file

@ -184,6 +184,7 @@ impl ItemTree {
if let Some(attrs) = top_attrs {
item_tree.attrs.insert(AttrOwner::TopLevel, attrs);
}
item_tree.shrink_to_fit();
Arc::new(item_tree)
}
@ -196,6 +197,51 @@ impl ItemTree {
}
}
fn shrink_to_fit(&mut self) {
if let Some(data) = &mut self.data {
let ItemTreeData {
imports,
extern_crates,
functions,
structs,
fields,
unions,
enums,
variants,
consts,
statics,
traits,
impls,
type_aliases,
mods,
macro_calls,
exprs,
vis,
generics,
} = &mut **data;
imports.shrink_to_fit();
extern_crates.shrink_to_fit();
functions.shrink_to_fit();
structs.shrink_to_fit();
fields.shrink_to_fit();
unions.shrink_to_fit();
enums.shrink_to_fit();
variants.shrink_to_fit();
consts.shrink_to_fit();
statics.shrink_to_fit();
traits.shrink_to_fit();
impls.shrink_to_fit();
type_aliases.shrink_to_fit();
mods.shrink_to_fit();
macro_calls.shrink_to_fit();
exprs.shrink_to_fit();
vis.arena.shrink_to_fit();
generics.arena.shrink_to_fit();
}
}
/// Returns an iterator over all items located at the top level of the `HirFileId` this
/// `ItemTree` was created from.
pub fn top_level_items(&self) -> &[ModItem] {