@@ -121,7 +121,10 @@ pub struct SemanticsImpl<'db> {
121
121
pub db : & ' db dyn HirDatabase ,
122
122
s2d_cache : RefCell < SourceToDefCache > ,
123
123
expansion_info_cache : RefCell < FxHashMap < HirFileId , Option < ExpansionInfo > > > ,
124
+ // Rootnode to HirFileId cache
124
125
cache : RefCell < FxHashMap < SyntaxNode , HirFileId > > ,
126
+ // MacroCall to its expansion's HirFileId cache
127
+ macro_call_cache : RefCell < FxHashMap < InFile < ast:: MacroCall > , HirFileId > > ,
125
128
}
126
129
127
130
impl < DB > fmt:: Debug for Semantics < ' _ , DB > {
@@ -396,6 +399,7 @@ impl<'db> SemanticsImpl<'db> {
396
399
s2d_cache : Default :: default ( ) ,
397
400
cache : Default :: default ( ) ,
398
401
expansion_info_cache : Default :: default ( ) ,
402
+ macro_call_cache : Default :: default ( ) ,
399
403
}
400
404
}
401
405
@@ -554,6 +558,7 @@ impl<'db> SemanticsImpl<'db> {
554
558
let sa = self . analyze ( & parent) ;
555
559
let mut stack: SmallVec < [ _ ; 1 ] > = smallvec ! [ InFile :: new( sa. file_id, token) ] ;
556
560
let mut cache = self . expansion_info_cache . borrow_mut ( ) ;
561
+ let mut mcache = self . macro_call_cache . borrow_mut ( ) ;
557
562
558
563
let mut process_expansion_for_token =
559
564
|stack : & mut SmallVec < _ > , file_id, item, token : InFile < & _ > | {
@@ -582,14 +587,10 @@ impl<'db> SemanticsImpl<'db> {
582
587
let was_not_remapped = ( || {
583
588
// are we inside an attribute macro call
584
589
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
+ } )
593
594
} ) ;
594
595
if let Some ( ( call_id, item) ) = containing_attribute_macro_call {
595
596
let file_id = call_id. as_file ( ) ;
@@ -616,7 +617,15 @@ impl<'db> SemanticsImpl<'db> {
616
617
return None ;
617
618
}
618
619
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
+ } ;
620
629
return process_expansion_for_token ( & mut stack, file_id, None , token. as_ref ( ) ) ;
621
630
}
622
631
0 commit comments