@@ -112,31 +112,37 @@ use syntax::{
112112
113113use crate :: { db:: HirDatabase , InFile } ;
114114
115- pub ( super ) type SourceToDefCache = FxHashMap < ( ChildContainer , HirFileId ) , DynMap > ;
115+ #[ derive( Default ) ]
116+ pub ( super ) struct SourceToDefCache {
117+ pub ( super ) dynmap_cache : FxHashMap < ( ChildContainer , HirFileId ) , DynMap > ,
118+ pub ( super ) expansion_info_cache : FxHashMap < MacroFileId , ExpansionInfo > ,
119+ pub ( super ) file_to_def_cache : FxHashMap < FileId , SmallVec < [ ModuleId ; 1 ] > > ,
120+ }
116121
117- pub ( super ) struct SourceToDefCtx < ' a , ' dyn_cache > {
118- pub ( super ) db : & ' a dyn HirDatabase ,
119- pub ( super ) dynmap_cache : & ' dyn_cache mut SourceToDefCache ,
120- pub ( super ) expansion_info_cache : & ' a mut FxHashMap < MacroFileId , ExpansionInfo > ,
122+ pub ( super ) struct SourceToDefCtx < ' db , ' cache > {
123+ pub ( super ) db : & ' db dyn HirDatabase ,
124+ pub ( super ) cache : & ' cache mut SourceToDefCache ,
121125}
122126
123127impl SourceToDefCtx < ' _ , ' _ > {
124- pub ( super ) fn file_to_def ( & self , file : FileId ) -> SmallVec < [ ModuleId ; 1 ] > {
128+ pub ( super ) fn file_to_def ( & mut self , file : FileId ) -> & SmallVec < [ ModuleId ; 1 ] > {
125129 let _p = tracing:: span!( tracing:: Level :: INFO , "SourceToDefCtx::file_to_def" ) . entered ( ) ;
126- let mut mods = SmallVec :: new ( ) ;
127- for & crate_id in self . db . relevant_crates ( file) . iter ( ) {
128- // Note: `mod` declarations in block modules cannot be supported here
129- let crate_def_map = self . db . crate_def_map ( crate_id) ;
130- mods. extend (
131- crate_def_map
132- . modules_for_file ( file)
133- . map ( |local_id| crate_def_map. module_id ( local_id) ) ,
134- )
135- }
136- if mods. is_empty ( ) {
137- // FIXME: detached file
138- }
139- mods
130+ self . cache . file_to_def_cache . entry ( file) . or_insert_with ( || {
131+ let mut mods = SmallVec :: new ( ) ;
132+ for & crate_id in self . db . relevant_crates ( file) . iter ( ) {
133+ // Note: `mod` declarations in block modules cannot be supported here
134+ let crate_def_map = self . db . crate_def_map ( crate_id) ;
135+ mods. extend (
136+ crate_def_map
137+ . modules_for_file ( file)
138+ . map ( |local_id| crate_def_map. module_id ( local_id) ) ,
139+ )
140+ }
141+ if mods. is_empty ( ) {
142+ // FIXME: detached file
143+ }
144+ mods
145+ } )
140146 }
141147
142148 pub ( super ) fn module_to_def ( & mut self , src : InFile < & ast:: Module > ) -> Option < ModuleId > {
@@ -166,7 +172,7 @@ impl SourceToDefCtx<'_, '_> {
166172 Some ( def_map. module_id ( child_id) )
167173 }
168174
169- pub ( super ) fn source_file_to_def ( & self , src : InFile < & ast:: SourceFile > ) -> Option < ModuleId > {
175+ pub ( super ) fn source_file_to_def ( & mut self , src : InFile < & ast:: SourceFile > ) -> Option < ModuleId > {
170176 let _p = tracing:: span!( tracing:: Level :: INFO , "source_file_to_def" ) . entered ( ) ;
171177 let file_id = src. file_id . original_file ( self . db . upcast ( ) ) ;
172178 self . file_to_def ( file_id) . first ( ) . copied ( )
@@ -325,7 +331,8 @@ impl SourceToDefCtx<'_, '_> {
325331
326332 fn cache_for ( & mut self , container : ChildContainer , file_id : HirFileId ) -> & DynMap {
327333 let db = self . db ;
328- self . dynmap_cache
334+ self . cache
335+ . dynmap_cache
329336 . entry ( ( container, file_id) )
330337 . or_insert_with ( || container. child_by_source ( db, file_id) )
331338 }
@@ -421,6 +428,7 @@ impl SourceToDefCtx<'_, '_> {
421428 let macro_file = node. file_id . macro_file ( ) ?;
422429
423430 let expansion_info = this
431+ . cache
424432 . expansion_info_cache
425433 . entry ( macro_file)
426434 . or_insert_with ( || macro_file. expansion_info ( this. db . upcast ( ) ) ) ;
0 commit comments