-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Description
hir::Semantics
is a general cache used to be able to resolve any syntax node to its semantic definition. This is generally slow given the kind of work it does, but quite often we do a lot more work than necessary, populating unnecessary DynMap
tables and more importantly, we populate it via parsing, where the parsing happens through direct database calls, even though the Semantics
itself caches these. That means for big enough files we can end up with thrashing the LRU cache a fair bit. We should fix that, a related problem is the HasSource
trait going through the database instead of Semantics
which means we can end up with nodes not cached by Semantics
ultimately ending up in panics when it tries to lookup stuff (which has happened in the past).
So the things that need to be changed are:
- Change the signature of (or add a second version)
HasSource::source
to take a reference to theSemantics
instead that does the right thingfn source(self, db: &dyn HirDatabase) -> Option<InFile<Self::Ast>>; - Change
rust-analyzer/crates/hir-def/src/src.rs
Lines 14 to 21 in 062e1b9
pub trait HasSource { type Value: AstNode; fn source(&self, db: &dyn DefDatabase) -> InFile<Self::Value> { let InFile { file_id, value } = self.ast_ptr(db); InFile::new(file_id, value.to_node(&db.parse_or_expand(file_id))) } fn ast_ptr(&self, db: &dyn DefDatabase) -> InFile<AstPtr<Self::Value>>; } - Change
rust-analyzer/crates/hir-def/src/child_by_source.rs
Lines 26 to 33 in 062e1b9
pub trait ChildBySource { fn child_by_source(&self, db: &dyn DefDatabase, file_id: HirFileId) -> DynMap { let mut res = DynMap::default(); self.child_by_source_to(db, &mut res, file_id); res } fn child_by_source_to(&self, db: &dyn DefDatabase, map: &mut DynMap, file_id: HirFileId); }
This will most likely require adding callbacks to some of the hir-def
crate APIs to abstract away the parsing (and then add / keep the current APIs as convenience APIs where Semantics won't be involved).