Skip to content

Commit a432f87

Browse files
committed
Cache macro expansion in semantics
#5497 accidentally made syntax highlighting quadratic, due to repeated tokentreeizing of macros.
1 parent b9ef6cf commit a432f87

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

crates/ra_hir/src/semantics.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ pub struct Semantics<'db, DB> {
8989
pub struct SemanticsImpl<'db> {
9090
pub db: &'db dyn HirDatabase,
9191
s2d_cache: RefCell<SourceToDefCache>,
92+
expansion_info_cache: RefCell<FxHashMap<HirFileId, Option<ExpansionInfo>>>,
9293
cache: RefCell<FxHashMap<SyntaxNode, HirFileId>>,
9394
}
9495

@@ -275,7 +276,12 @@ impl<'db, DB: HirDatabase> Semantics<'db, DB> {
275276

276277
impl<'db> SemanticsImpl<'db> {
277278
fn new(db: &'db dyn HirDatabase) -> Self {
278-
Self { db, s2d_cache: Default::default(), cache: Default::default() }
279+
SemanticsImpl {
280+
db,
281+
s2d_cache: Default::default(),
282+
cache: Default::default(),
283+
expansion_info_cache: Default::default(),
284+
}
279285
}
280286

281287
fn parse(&self, file_id: FileId) -> ast::SourceFile {
@@ -328,7 +334,13 @@ impl<'db> SemanticsImpl<'db> {
328334
return None;
329335
}
330336
let file_id = sa.expand(self.db, token.with_value(&macro_call))?;
331-
let token = file_id.expansion_info(self.db.upcast())?.map_token_down(token.as_ref())?;
337+
let token = self
338+
.expansion_info_cache
339+
.borrow_mut()
340+
.entry(file_id)
341+
.or_insert_with(|| file_id.expansion_info(self.db.upcast()))
342+
.as_ref()?
343+
.map_token_down(token.as_ref())?;
332344

333345
self.cache(find_root(&token.value.parent()), token.file_id);
334346

0 commit comments

Comments
 (0)