Shrink structs before returning from queries

This commit is contained in:
oxalica 2023-02-03 03:53:51 +08:00
parent 6b2eb7b9eb
commit 2d048b7f28
2 changed files with 35 additions and 5 deletions

View file

@ -70,7 +70,9 @@ fn module_with_source_map(
file_id: FileId,
) -> (Arc<Module>, Arc<ModuleSourceMap>) {
let parse = db.parse(file_id);
let (module, source_map) = lower::lower(db, file_id, parse);
let (mut module, mut source_map) = lower::lower(db, file_id, parse);
module.shrink_to_fit();
source_map.shrink_to_fit();
(Arc::new(module), Arc::new(source_map))
}
@ -97,6 +99,7 @@ fn source_root_closure(db: &dyn DefDatabase, id: SourceRootId) -> Arc<HashSet<Fi
}
}
}
closure.shrink_to_fit();
Arc::new(closure)
}
@ -126,6 +129,12 @@ impl ops::Index<NameId> for Module {
}
impl Module {
pub fn shrink_to_fit(&mut self) {
self.exprs.shrink_to_fit();
self.names.shrink_to_fit();
self.diagnostics.shrink_to_fit();
}
pub fn entry_expr(&self) -> ExprId {
self.entry_expr
}
@ -147,7 +156,7 @@ impl Module {
file_id: FileId,
) -> Arc<HashSet<FileId>> {
let source_root = db.source_root(db.file_source_root(file_id));
let refs = db
let mut refs = db
.module(file_id)
.exprs()
.filter_map(|(_, kind)| {
@ -161,7 +170,8 @@ impl Module {
source_root.file_for_path(&vpath)
})
})
.collect();
.collect::<HashSet<_>>();
refs.shrink_to_fit();
Arc::new(refs)
}
}
@ -177,6 +187,13 @@ pub struct ModuleSourceMap {
}
impl ModuleSourceMap {
pub fn shrink_to_fit(&mut self) {
self.expr_map.shrink_to_fit();
self.expr_map_rev.shrink_to_fit();
self.name_map.shrink_to_fit();
self.name_map_rev.shrink_to_fit();
}
pub fn expr_for_node(&self, node: AstPtr) -> Option<ExprId> {
self.expr_map.get(&node).copied()
}

View file

@ -34,9 +34,15 @@ impl ModuleScopes {
kind: ScopeKind::Definitions(Default::default()),
});
this.traverse_expr(&module, module.entry_expr, root_scope);
this.shrink_to_fit();
Arc::new(this)
}
pub fn shrink_to_fit(&mut self) {
self.scopes.shrink_to_fit();
// The size of `scope_by_expr` should be precise.
}
pub fn scope_for_expr(&self, expr_id: ExprId) -> Option<ScopeId> {
self.scope_by_expr.get(expr_id).copied()
}
@ -224,7 +230,7 @@ impl NameResolution {
pub(crate) fn name_resolution_query(db: &dyn DefDatabase, file_id: FileId) -> Arc<Self> {
let module = db.module(file_id);
let scopes = db.scopes(file_id);
let resolve_map = module
let mut resolve_map = module
.exprs()
.filter_map(|(e, kind)| {
match kind {
@ -233,7 +239,8 @@ impl NameResolution {
_ => None,
}
})
.collect();
.collect::<HashMap<_, _>>();
resolve_map.shrink_to_fit();
Arc::new(Self { resolve_map })
}
@ -289,9 +296,15 @@ impl NameReference {
.for_each(|&with_expr| this.with_refs.entry(with_expr).or_default().push(expr)),
}
}
this.shrink_to_fit();
Arc::new(this)
}
pub fn shrink_to_fit(&mut self) {
// The size of `def_refs` should be precise.
self.with_refs.shrink_to_fit();
}
pub fn name_references(&self, name: NameId) -> Option<&[ExprId]> {
Some(&**self.def_refs.get(name)?)
}