@@ -34,14 +34,15 @@ use ra_db::{
3434 salsa:: { self , ParallelDatabase } ,
3535 CrateId , FileId , SourceDatabaseExt , SourceRootId ,
3636} ;
37+ use ra_prof:: profile;
3738use ra_syntax:: {
3839 ast:: { self , NameOwner } ,
3940 match_ast, AstNode , Parse , SmolStr , SourceFile ,
4041 SyntaxKind :: { self , * } ,
4142 SyntaxNode , SyntaxNodePtr , TextRange , WalkEvent ,
4243} ;
43- #[ cfg( not( feature = "wasm" ) ) ]
4444use rayon:: prelude:: * ;
45+ use rustc_hash:: FxHashMap ;
4546
4647use crate :: RootDatabase ;
4748
@@ -86,10 +87,9 @@ impl Query {
8687}
8788
8889#[ salsa:: query_group( SymbolsDatabaseStorage ) ]
89- pub trait SymbolsDatabase : hir:: db:: HirDatabase {
90+ pub trait SymbolsDatabase : hir:: db:: HirDatabase + SourceDatabaseExt + ParallelDatabase {
9091 fn file_symbols ( & self , file_id : FileId ) -> Arc < SymbolIndex > ;
91- #[ salsa:: input]
92- fn library_symbols ( & self , id : SourceRootId ) -> Arc < SymbolIndex > ;
92+ fn library_symbols ( & self ) -> Arc < FxHashMap < SourceRootId , SymbolIndex > > ;
9393 /// The set of "local" (that is, from the current workspace) roots.
9494 /// Files in local roots are assumed to change frequently.
9595 #[ salsa:: input]
@@ -100,6 +100,29 @@ pub trait SymbolsDatabase: hir::db::HirDatabase {
100100 fn library_roots ( & self ) -> Arc < Vec < SourceRootId > > ;
101101}
102102
103+ fn library_symbols (
104+ db : & ( impl SymbolsDatabase + ParallelDatabase ) ,
105+ ) -> Arc < FxHashMap < SourceRootId , SymbolIndex > > {
106+ let _p = profile ( "library_symbols" ) ;
107+
108+ let roots = db. library_roots ( ) ;
109+ let res = roots
110+ . iter ( )
111+ . map ( |& root_id| {
112+ let root = db. source_root ( root_id) ;
113+ let files = root
114+ . walk ( )
115+ . map ( |it| ( it, SourceDatabaseExt :: file_text ( db, it) ) )
116+ . collect :: < Vec < _ > > ( ) ;
117+ let symbol_index = SymbolIndex :: for_files (
118+ files. into_par_iter ( ) . map ( |( file, text) | ( file, SourceFile :: parse ( & text) ) ) ,
119+ ) ;
120+ ( root_id, symbol_index)
121+ } )
122+ . collect ( ) ;
123+ Arc :: new ( res)
124+ }
125+
103126fn file_symbols ( db : & impl SymbolsDatabase , file_id : FileId ) -> Arc < SymbolIndex > {
104127 db. check_canceled ( ) ;
105128 let parse = db. parse ( file_id) ;
@@ -112,9 +135,9 @@ fn file_symbols(db: &impl SymbolsDatabase, file_id: FileId) -> Arc<SymbolIndex>
112135}
113136
114137/// Need to wrap Snapshot to provide `Clone` impl for `map_with`
115- struct Snap ( salsa :: Snapshot < RootDatabase > ) ;
116- impl Clone for Snap {
117- fn clone ( & self ) -> Snap {
138+ struct Snap < DB > ( DB ) ;
139+ impl < DB : ParallelDatabase > Clone for Snap < salsa :: Snapshot < DB > > {
140+ fn clone ( & self ) -> Snap < salsa :: Snapshot < DB > > {
118141 Snap ( self . 0 . snapshot ( ) )
119142 }
120143}
@@ -143,19 +166,11 @@ impl Clone for Snap {
143166pub fn world_symbols ( db : & RootDatabase , query : Query ) -> Vec < FileSymbol > {
144167 let _p = ra_prof:: profile ( "world_symbols" ) . detail ( || query. query . clone ( ) ) ;
145168
146- let buf: Vec < Arc < SymbolIndex > > = if query. libs {
147- let snap = Snap ( db. snapshot ( ) ) ;
148- #[ cfg( not( feature = "wasm" ) ) ]
149- let buf = db
150- . library_roots ( )
151- . par_iter ( )
152- . map_with ( snap, |db, & lib_id| db. 0 . library_symbols ( lib_id) )
153- . collect ( ) ;
154-
155- #[ cfg( feature = "wasm" ) ]
156- let buf = db. library_roots ( ) . iter ( ) . map ( |& lib_id| snap. 0 . library_symbols ( lib_id) ) . collect ( ) ;
157-
158- buf
169+ let tmp1;
170+ let tmp2;
171+ let buf: Vec < & SymbolIndex > = if query. libs {
172+ tmp1 = db. library_symbols ( ) ;
173+ tmp1. values ( ) . collect ( )
159174 } else {
160175 let mut files = Vec :: new ( ) ;
161176 for & root in db. local_roots ( ) . iter ( ) {
@@ -164,14 +179,11 @@ pub fn world_symbols(db: &RootDatabase, query: Query) -> Vec<FileSymbol> {
164179 }
165180
166181 let snap = Snap ( db. snapshot ( ) ) ;
167- #[ cfg( not( feature = "wasm" ) ) ]
168- let buf =
169- files. par_iter ( ) . map_with ( snap, |db, & file_id| db. 0 . file_symbols ( file_id) ) . collect ( ) ;
170-
171- #[ cfg( feature = "wasm" ) ]
172- let buf = files. iter ( ) . map ( |& file_id| snap. 0 . file_symbols ( file_id) ) . collect ( ) ;
173-
174- buf
182+ tmp2 = files
183+ . par_iter ( )
184+ . map_with ( snap, |db, & file_id| db. 0 . file_symbols ( file_id) )
185+ . collect :: < Vec < _ > > ( ) ;
186+ tmp2. iter ( ) . map ( |it| & * * it) . collect ( )
175187 } ;
176188 query. search ( & buf)
177189}
@@ -191,14 +203,11 @@ pub fn crate_symbols(db: &RootDatabase, krate: CrateId, query: Query) -> Vec<Fil
191203
192204 let snap = Snap ( db. snapshot ( ) ) ;
193205
194- #[ cfg( not( feature = "wasm" ) ) ]
195206 let buf = files
196207 . par_iter ( )
197208 . map_with ( snap, |db, & file_id| db. 0 . file_symbols ( file_id) )
198209 . collect :: < Vec < _ > > ( ) ;
199-
200- #[ cfg( feature = "wasm" ) ]
201- let buf = files. iter ( ) . map ( |& file_id| snap. 0 . file_symbols ( file_id) ) . collect :: < Vec < _ > > ( ) ;
210+ let buf = buf. iter ( ) . map ( |it| & * * it) . collect :: < Vec < _ > > ( ) ;
202211
203212 query. search ( & buf)
204213}
@@ -245,12 +254,8 @@ impl SymbolIndex {
245254 lhs_chars. cmp ( rhs_chars)
246255 }
247256
248- #[ cfg( not( feature = "wasm" ) ) ]
249257 symbols. par_sort_by ( cmp) ;
250258
251- #[ cfg( feature = "wasm" ) ]
252- symbols. sort_by ( cmp) ;
253-
254259 let mut builder = fst:: MapBuilder :: memory ( ) ;
255260
256261 let mut last_batch_start = 0 ;
@@ -284,7 +289,6 @@ impl SymbolIndex {
284289 self . map . as_fst ( ) . size ( ) + self . symbols . len ( ) * mem:: size_of :: < FileSymbol > ( )
285290 }
286291
287- #[ cfg( not( feature = "wasm" ) ) ]
288292 pub ( crate ) fn for_files (
289293 files : impl ParallelIterator < Item = ( FileId , Parse < ast:: SourceFile > ) > ,
290294 ) -> SymbolIndex {
@@ -294,16 +298,6 @@ impl SymbolIndex {
294298 SymbolIndex :: new ( symbols)
295299 }
296300
297- #[ cfg( feature = "wasm" ) ]
298- pub ( crate ) fn for_files (
299- files : impl Iterator < Item = ( FileId , Parse < ast:: SourceFile > ) > ,
300- ) -> SymbolIndex {
301- let symbols = files
302- . flat_map ( |( file_id, file) | source_file_to_file_symbols ( & file. tree ( ) , file_id) )
303- . collect :: < Vec < _ > > ( ) ;
304- SymbolIndex :: new ( symbols)
305- }
306-
307301 fn range_to_map_value ( start : usize , end : usize ) -> u64 {
308302 debug_assert ! [ start <= ( std:: u32 :: MAX as usize ) ] ;
309303 debug_assert ! [ end <= ( std:: u32 :: MAX as usize ) ] ;
@@ -319,7 +313,7 @@ impl SymbolIndex {
319313}
320314
321315impl Query {
322- pub ( crate ) fn search ( self , indices : & [ Arc < SymbolIndex > ] ) -> Vec < FileSymbol > {
316+ pub ( crate ) fn search ( self , indices : & [ & SymbolIndex ] ) -> Vec < FileSymbol > {
323317 let mut op = fst:: map:: OpBuilder :: new ( ) ;
324318 for file_symbols in indices. iter ( ) {
325319 let automaton = fst:: automaton:: Subsequence :: new ( & self . lowercased ) ;
0 commit comments