Skip to content

Commit 99e3acd

Browse files
bors[bot]matklad
andauthored
Merge #4934
4934: Remove special casing for library symbols r=matklad a=matklad We might as well handle them internally, via queries. I am not sure, but it looks like the current LibraryData setup might even predate salsa? It's not really needed and creates a bunch of complexity. bors r+ 🤖 Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
2 parents f5af48d + d1d0b5a commit 99e3acd

File tree

6 files changed

+69
-212
lines changed

6 files changed

+69
-212
lines changed

crates/ra_ide/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ pub use ra_db::{
8282
Canceled, CrateGraph, CrateId, Edition, FileId, FilePosition, FileRange, SourceRootId,
8383
};
8484
pub use ra_ide_db::{
85-
change::{AnalysisChange, LibraryData},
85+
change::AnalysisChange,
8686
line_index::{LineCol, LineIndex},
8787
search::SearchScope,
8888
source_change::{FileSystemEdit, SourceChange, SourceFileEdit},

crates/ra_ide/src/status.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use ra_prof::{memory_usage, Bytes};
1616
use ra_syntax::{ast, Parse, SyntaxNode};
1717

1818
use crate::FileId;
19+
use rustc_hash::FxHashMap;
1920

2021
fn syntax_tree_stats(db: &RootDatabase) -> SyntaxTreeStats {
2122
db.query(ra_db::ParseQuery).entries::<SyntaxTreeStats>()
@@ -123,20 +124,24 @@ struct LibrarySymbolsStats {
123124

124125
impl fmt::Display for LibrarySymbolsStats {
125126
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
126-
write!(fmt, "{} ({}) symbols", self.total, self.size,)
127+
write!(fmt, "{} ({}) symbols", self.total, self.size)
127128
}
128129
}
129130

130-
impl FromIterator<TableEntry<SourceRootId, Arc<SymbolIndex>>> for LibrarySymbolsStats {
131+
impl FromIterator<TableEntry<(), Arc<FxHashMap<SourceRootId, SymbolIndex>>>>
132+
for LibrarySymbolsStats
133+
{
131134
fn from_iter<T>(iter: T) -> LibrarySymbolsStats
132135
where
133-
T: IntoIterator<Item = TableEntry<SourceRootId, Arc<SymbolIndex>>>,
136+
T: IntoIterator<Item = TableEntry<(), Arc<FxHashMap<SourceRootId, SymbolIndex>>>>,
134137
{
135138
let mut res = LibrarySymbolsStats::default();
136139
for entry in iter {
137140
let value = entry.value.unwrap();
138-
res.total += value.len();
139-
res.size += value.memory_size();
141+
for symbols in value.values() {
142+
res.total += symbols.len();
143+
res.size += symbols.memory_size();
144+
}
140145
}
141146
res
142147
}

crates/ra_ide_db/src/change.rs

Lines changed: 5 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,15 @@ use ra_db::{
99
SourceRootId,
1010
};
1111
use ra_prof::{memory_usage, profile, Bytes};
12-
use ra_syntax::SourceFile;
13-
#[cfg(not(feature = "wasm"))]
14-
use rayon::prelude::*;
1512
use rustc_hash::FxHashMap;
1613

17-
use crate::{
18-
symbol_index::{SymbolIndex, SymbolsDatabase},
19-
RootDatabase,
20-
};
14+
use crate::{symbol_index::SymbolsDatabase, RootDatabase};
2115

2216
#[derive(Default)]
2317
pub struct AnalysisChange {
2418
new_roots: Vec<(SourceRootId, bool)>,
2519
roots_changed: FxHashMap<SourceRootId, RootChange>,
2620
files_changed: Vec<(FileId, Arc<String>)>,
27-
libraries_added: Vec<LibraryData>,
2821
crate_graph: Option<CrateGraph>,
2922
}
3023

@@ -40,9 +33,6 @@ impl fmt::Debug for AnalysisChange {
4033
if !self.files_changed.is_empty() {
4134
d.field("files_changed", &self.files_changed.len());
4235
}
43-
if !self.libraries_added.is_empty() {
44-
d.field("libraries_added", &self.libraries_added.len());
45-
}
4636
if self.crate_graph.is_some() {
4737
d.field("crate_graph", &self.crate_graph);
4838
}
@@ -79,10 +69,6 @@ impl AnalysisChange {
7969
self.roots_changed.entry(root_id).or_default().removed.push(file);
8070
}
8171

82-
pub fn add_library(&mut self, data: LibraryData) {
83-
self.libraries_added.push(data)
84-
}
85-
8672
pub fn set_crate_graph(&mut self, graph: CrateGraph) {
8773
self.crate_graph = Some(graph);
8874
}
@@ -116,47 +102,6 @@ impl fmt::Debug for RootChange {
116102
}
117103
}
118104

119-
pub struct LibraryData {
120-
root_id: SourceRootId,
121-
root_change: RootChange,
122-
symbol_index: SymbolIndex,
123-
}
124-
125-
impl fmt::Debug for LibraryData {
126-
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
127-
f.debug_struct("LibraryData")
128-
.field("root_id", &self.root_id)
129-
.field("root_change", &self.root_change)
130-
.field("n_symbols", &self.symbol_index.len())
131-
.finish()
132-
}
133-
}
134-
135-
impl LibraryData {
136-
pub fn prepare(
137-
root_id: SourceRootId,
138-
files: Vec<(FileId, RelativePathBuf, Arc<String>)>,
139-
) -> LibraryData {
140-
let _p = profile("LibraryData::prepare");
141-
142-
#[cfg(not(feature = "wasm"))]
143-
let iter = files.par_iter();
144-
#[cfg(feature = "wasm")]
145-
let iter = files.iter();
146-
147-
let symbol_index = SymbolIndex::for_files(iter.map(|(file_id, _, text)| {
148-
let parse = SourceFile::parse(text);
149-
(*file_id, parse)
150-
}));
151-
let mut root_change = RootChange::default();
152-
root_change.added = files
153-
.into_iter()
154-
.map(|(file_id, path, text)| AddFile { file_id, path, text })
155-
.collect();
156-
LibraryData { root_id, root_change, symbol_index }
157-
}
158-
}
159-
160105
const GC_COOLDOWN: time::Duration = time::Duration::from_millis(100);
161106

162107
impl RootDatabase {
@@ -171,16 +116,20 @@ impl RootDatabase {
171116
log::info!("apply_change {:?}", change);
172117
if !change.new_roots.is_empty() {
173118
let mut local_roots = Vec::clone(&self.local_roots());
119+
let mut libraries = Vec::clone(&self.library_roots());
174120
for (root_id, is_local) in change.new_roots {
175121
let root =
176122
if is_local { SourceRoot::new_local() } else { SourceRoot::new_library() };
177123
let durability = durability(&root);
178124
self.set_source_root_with_durability(root_id, Arc::new(root), durability);
179125
if is_local {
180126
local_roots.push(root_id);
127+
} else {
128+
libraries.push(root_id)
181129
}
182130
}
183131
self.set_local_roots_with_durability(Arc::new(local_roots), Durability::HIGH);
132+
self.set_library_roots_with_durability(Arc::new(libraries), Durability::HIGH);
184133
}
185134

186135
for (root_id, root_change) in change.roots_changed {
@@ -192,24 +141,6 @@ impl RootDatabase {
192141
let durability = durability(&source_root);
193142
self.set_file_text_with_durability(file_id, text, durability)
194143
}
195-
if !change.libraries_added.is_empty() {
196-
let mut libraries = Vec::clone(&self.library_roots());
197-
for library in change.libraries_added {
198-
libraries.push(library.root_id);
199-
self.set_source_root_with_durability(
200-
library.root_id,
201-
Arc::new(SourceRoot::new_library()),
202-
Durability::HIGH,
203-
);
204-
self.set_library_symbols_with_durability(
205-
library.root_id,
206-
Arc::new(library.symbol_index),
207-
Durability::HIGH,
208-
);
209-
self.apply_root_change(library.root_id, library.root_change);
210-
}
211-
self.set_library_roots_with_durability(Arc::new(libraries), Durability::HIGH);
212-
}
213144
if let Some(crate_graph) = change.crate_graph {
214145
self.set_crate_graph_with_durability(Arc::new(crate_graph), Durability::HIGH)
215146
}

crates/ra_ide_db/src/symbol_index.rs

Lines changed: 42 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,15 @@ use ra_db::{
3434
salsa::{self, ParallelDatabase},
3535
CrateId, FileId, SourceDatabaseExt, SourceRootId,
3636
};
37+
use ra_prof::profile;
3738
use 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"))]
4444
use rayon::prelude::*;
45+
use rustc_hash::FxHashMap;
4546

4647
use 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+
103126
fn 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 {
143166
pub 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

321315
impl 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

Comments
 (0)