Skip to content

Commit d1e449e

Browse files
bors[bot]Veykril
andauthored
Merge #10701
10701: internal: Cache ast::MacroCalls to their expansions in Semantics::descend_into_macros_impl r=Veykril a=Veykril Saves ~45ms when highlighting `rust-analyzer/src/config.rs` for me bors r+ Co-authored-by: Lukas Wirth <[email protected]>
2 parents 10782bb + af4d244 commit d1e449e

File tree

1 file changed

+18
-9
lines changed

1 file changed

+18
-9
lines changed

crates/hir/src/semantics.rs

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,10 @@ pub struct SemanticsImpl<'db> {
121121
pub db: &'db dyn HirDatabase,
122122
s2d_cache: RefCell<SourceToDefCache>,
123123
expansion_info_cache: RefCell<FxHashMap<HirFileId, Option<ExpansionInfo>>>,
124+
// Rootnode to HirFileId cache
124125
cache: RefCell<FxHashMap<SyntaxNode, HirFileId>>,
126+
// MacroCall to its expansion's HirFileId cache
127+
macro_call_cache: RefCell<FxHashMap<InFile<ast::MacroCall>, HirFileId>>,
125128
}
126129

127130
impl<DB> fmt::Debug for Semantics<'_, DB> {
@@ -396,6 +399,7 @@ impl<'db> SemanticsImpl<'db> {
396399
s2d_cache: Default::default(),
397400
cache: Default::default(),
398401
expansion_info_cache: Default::default(),
402+
macro_call_cache: Default::default(),
399403
}
400404
}
401405

@@ -554,6 +558,7 @@ impl<'db> SemanticsImpl<'db> {
554558
let sa = self.analyze(&parent);
555559
let mut stack: SmallVec<[_; 1]> = smallvec![InFile::new(sa.file_id, token)];
556560
let mut cache = self.expansion_info_cache.borrow_mut();
561+
let mut mcache = self.macro_call_cache.borrow_mut();
557562

558563
let mut process_expansion_for_token =
559564
|stack: &mut SmallVec<_>, file_id, item, token: InFile<&_>| {
@@ -582,14 +587,10 @@ impl<'db> SemanticsImpl<'db> {
582587
let was_not_remapped = (|| {
583588
// are we inside an attribute macro call
584589
let containing_attribute_macro_call = self.with_ctx(|ctx| {
585-
token
586-
.value
587-
.ancestors()
588-
.filter_map(ast::Item::cast)
589-
.filter_map(|item| {
590-
Some((ctx.item_to_macro_call(token.with_value(item.clone()))?, item))
591-
})
592-
.last()
590+
token.value.ancestors().filter_map(ast::Item::cast).find_map(|item| {
591+
// investigate this, seems to be VERY(250ms) expensive in rust-analyzer/src/config.rs?
592+
Some((ctx.item_to_macro_call(token.with_value(item.clone()))?, item))
593+
})
593594
});
594595
if let Some((call_id, item)) = containing_attribute_macro_call {
595596
let file_id = call_id.as_file();
@@ -616,7 +617,15 @@ impl<'db> SemanticsImpl<'db> {
616617
return None;
617618
}
618619

619-
let file_id = sa.expand(self.db, token.with_value(&macro_call))?;
620+
let mcall = token.with_value(macro_call);
621+
let file_id = match mcache.get(&mcall) {
622+
Some(&it) => it,
623+
None => {
624+
let it = sa.expand(self.db, mcall.as_ref())?;
625+
mcache.insert(mcall, it);
626+
it
627+
}
628+
};
620629
return process_expansion_for_token(&mut stack, file_id, None, token.as_ref());
621630
}
622631

0 commit comments

Comments
 (0)