@@ -34,14 +34,15 @@ use ra_db::{
34
34
salsa:: { self , ParallelDatabase } ,
35
35
CrateId , FileId , SourceDatabaseExt , SourceRootId ,
36
36
} ;
37
+ use ra_prof:: profile;
37
38
use ra_syntax:: {
38
39
ast:: { self , NameOwner } ,
39
40
match_ast, AstNode , Parse , SmolStr , SourceFile ,
40
41
SyntaxKind :: { self , * } ,
41
42
SyntaxNode , SyntaxNodePtr , TextRange , WalkEvent ,
42
43
} ;
43
- #[ cfg( not( feature = "wasm" ) ) ]
44
44
use rayon:: prelude:: * ;
45
+ use rustc_hash:: FxHashMap ;
45
46
46
47
use crate :: RootDatabase ;
47
48
@@ -86,10 +87,9 @@ impl Query {
86
87
}
87
88
88
89
#[ salsa:: query_group( SymbolsDatabaseStorage ) ]
89
- pub trait SymbolsDatabase : hir:: db:: HirDatabase {
90
+ pub trait SymbolsDatabase : hir:: db:: HirDatabase + SourceDatabaseExt + ParallelDatabase {
90
91
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 > > ;
93
93
/// The set of "local" (that is, from the current workspace) roots.
94
94
/// Files in local roots are assumed to change frequently.
95
95
#[ salsa:: input]
@@ -100,6 +100,29 @@ pub trait SymbolsDatabase: hir::db::HirDatabase {
100
100
fn library_roots ( & self ) -> Arc < Vec < SourceRootId > > ;
101
101
}
102
102
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
+
103
126
fn file_symbols ( db : & impl SymbolsDatabase , file_id : FileId ) -> Arc < SymbolIndex > {
104
127
db. check_canceled ( ) ;
105
128
let parse = db. parse ( file_id) ;
@@ -112,9 +135,9 @@ fn file_symbols(db: &impl SymbolsDatabase, file_id: FileId) -> Arc<SymbolIndex>
112
135
}
113
136
114
137
/// 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 > > {
118
141
Snap ( self . 0 . snapshot ( ) )
119
142
}
120
143
}
@@ -143,19 +166,11 @@ impl Clone for Snap {
143
166
pub fn world_symbols ( db : & RootDatabase , query : Query ) -> Vec < FileSymbol > {
144
167
let _p = ra_prof:: profile ( "world_symbols" ) . detail ( || query. query . clone ( ) ) ;
145
168
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 ( )
159
174
} else {
160
175
let mut files = Vec :: new ( ) ;
161
176
for & root in db. local_roots ( ) . iter ( ) {
@@ -164,14 +179,11 @@ pub fn world_symbols(db: &RootDatabase, query: Query) -> Vec<FileSymbol> {
164
179
}
165
180
166
181
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 ( )
175
187
} ;
176
188
query. search ( & buf)
177
189
}
@@ -191,14 +203,11 @@ pub fn crate_symbols(db: &RootDatabase, krate: CrateId, query: Query) -> Vec<Fil
191
203
192
204
let snap = Snap ( db. snapshot ( ) ) ;
193
205
194
- #[ cfg( not( feature = "wasm" ) ) ]
195
206
let buf = files
196
207
. par_iter ( )
197
208
. map_with ( snap, |db, & file_id| db. 0 . file_symbols ( file_id) )
198
209
. 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 < _ > > ( ) ;
202
211
203
212
query. search ( & buf)
204
213
}
@@ -245,12 +254,8 @@ impl SymbolIndex {
245
254
lhs_chars. cmp ( rhs_chars)
246
255
}
247
256
248
- #[ cfg( not( feature = "wasm" ) ) ]
249
257
symbols. par_sort_by ( cmp) ;
250
258
251
- #[ cfg( feature = "wasm" ) ]
252
- symbols. sort_by ( cmp) ;
253
-
254
259
let mut builder = fst:: MapBuilder :: memory ( ) ;
255
260
256
261
let mut last_batch_start = 0 ;
@@ -284,7 +289,6 @@ impl SymbolIndex {
284
289
self . map . as_fst ( ) . size ( ) + self . symbols . len ( ) * mem:: size_of :: < FileSymbol > ( )
285
290
}
286
291
287
- #[ cfg( not( feature = "wasm" ) ) ]
288
292
pub ( crate ) fn for_files (
289
293
files : impl ParallelIterator < Item = ( FileId , Parse < ast:: SourceFile > ) > ,
290
294
) -> SymbolIndex {
@@ -294,16 +298,6 @@ impl SymbolIndex {
294
298
SymbolIndex :: new ( symbols)
295
299
}
296
300
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
-
307
301
fn range_to_map_value ( start : usize , end : usize ) -> u64 {
308
302
debug_assert ! [ start <= ( std:: u32 :: MAX as usize ) ] ;
309
303
debug_assert ! [ end <= ( std:: u32 :: MAX as usize ) ] ;
@@ -319,7 +313,7 @@ impl SymbolIndex {
319
313
}
320
314
321
315
impl Query {
322
- pub ( crate ) fn search ( self , indices : & [ Arc < SymbolIndex > ] ) -> Vec < FileSymbol > {
316
+ pub ( crate ) fn search ( self , indices : & [ & SymbolIndex ] ) -> Vec < FileSymbol > {
323
317
let mut op = fst:: map:: OpBuilder :: new ( ) ;
324
318
for file_symbols in indices. iter ( ) {
325
319
let automaton = fst:: automaton:: Subsequence :: new ( & self . lowercased ) ;
0 commit comments