Skip to content

Commit 2bbbc61

Browse files
authored
Merge pull request #20997 from Veykril/push-zsuorxrkpupr
perf: Only populate public items in dependency symbol index
2 parents 0788e84 + 7f82691 commit 2bbbc61

File tree

2 files changed

+41
-12
lines changed

2 files changed

+41
-12
lines changed

crates/hir/src/symbols.rs

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -66,22 +66,28 @@ pub struct SymbolCollector<'a> {
6666
symbols: FxIndexSet<FileSymbol>,
6767
work: Vec<SymbolCollectorWork>,
6868
current_container_name: Option<Symbol>,
69+
collect_pub_only: bool,
6970
}
7071

7172
/// Given a [`ModuleId`] and a [`HirDatabase`], use the DefMap for the module's crate to collect
7273
/// all symbols that should be indexed for the given module.
7374
impl<'a> SymbolCollector<'a> {
74-
pub fn new(db: &'a dyn HirDatabase) -> Self {
75+
pub fn new(db: &'a dyn HirDatabase, collect_pub_only: bool) -> Self {
7576
SymbolCollector {
7677
db,
7778
symbols: Default::default(),
7879
work: Default::default(),
7980
current_container_name: None,
81+
collect_pub_only,
8082
}
8183
}
8284

83-
pub fn new_module(db: &dyn HirDatabase, module: Module) -> Box<[FileSymbol]> {
84-
let mut symbol_collector = SymbolCollector::new(db);
85+
pub fn new_module(
86+
db: &dyn HirDatabase,
87+
module: Module,
88+
collect_pub_only: bool,
89+
) -> Box<[FileSymbol]> {
90+
let mut symbol_collector = SymbolCollector::new(db, collect_pub_only);
8591
symbol_collector.collect(module);
8692
symbol_collector.finish()
8793
}
@@ -113,7 +119,11 @@ impl<'a> SymbolCollector<'a> {
113119
}
114120

115121
fn collect_from_module(&mut self, module_id: ModuleId) {
116-
let push_decl = |this: &mut Self, def, name| {
122+
let collect_pub_only = self.collect_pub_only;
123+
let push_decl = |this: &mut Self, def: ModuleDefId, name, vis| {
124+
if collect_pub_only && vis != Visibility::Public {
125+
return;
126+
}
117127
match def {
118128
ModuleDefId::ModuleId(id) => this.push_module(id, name),
119129
ModuleDefId::FunctionId(id) => {
@@ -175,6 +185,9 @@ impl<'a> SymbolCollector<'a> {
175185
};
176186

177187
let mut push_import = |this: &mut Self, i: ImportId, name: &Name, def: ModuleDefId, vis| {
188+
if collect_pub_only && vis != Visibility::Public {
189+
return;
190+
}
178191
let source = import_child_source_cache
179192
.entry(i.use_)
180193
.or_insert_with(|| i.use_.child_source(this.db));
@@ -209,6 +222,9 @@ impl<'a> SymbolCollector<'a> {
209222

210223
let push_extern_crate =
211224
|this: &mut Self, i: ExternCrateId, name: &Name, def: ModuleDefId, vis| {
225+
if collect_pub_only && vis != Visibility::Public {
226+
return;
227+
}
212228
let loc = i.lookup(this.db);
213229
let source = loc.source(this.db);
214230
let rename = source.value.rename().and_then(|rename| rename.name());
@@ -258,7 +274,7 @@ impl<'a> SymbolCollector<'a> {
258274
continue;
259275
}
260276
// self is a declaration
261-
push_decl(self, def, name)
277+
push_decl(self, def, name, vis)
262278
}
263279

264280
for (name, Item { def, vis, import }) in scope.macros() {
@@ -271,7 +287,7 @@ impl<'a> SymbolCollector<'a> {
271287
continue;
272288
}
273289
// self is a declaration
274-
push_decl(self, def.into(), name)
290+
push_decl(self, ModuleDefId::MacroId(def), name, vis)
275291
}
276292

277293
for (name, Item { def, vis, import }) in scope.values() {
@@ -283,7 +299,7 @@ impl<'a> SymbolCollector<'a> {
283299
continue;
284300
}
285301
// self is a declaration
286-
push_decl(self, def, name)
302+
push_decl(self, def, name, vis)
287303
}
288304

289305
for const_id in scope.unnamed_consts() {
@@ -304,6 +320,9 @@ impl<'a> SymbolCollector<'a> {
304320
}
305321

306322
fn collect_from_body(&mut self, body_id: impl Into<DefWithBodyId>, name: Option<Name>) {
323+
if self.collect_pub_only {
324+
return;
325+
}
307326
let body_id = body_id.into();
308327
let body = self.db.body(body_id);
309328

@@ -330,6 +349,11 @@ impl<'a> SymbolCollector<'a> {
330349
);
331350
self.with_container_name(impl_name.as_deref().map(Symbol::intern), |s| {
332351
for &(ref name, assoc_item_id) in &impl_id.impl_items(self.db).items {
352+
if s.collect_pub_only && s.db.assoc_visibility(assoc_item_id) != Visibility::Public
353+
{
354+
continue;
355+
}
356+
333357
s.push_assoc_item(assoc_item_id, name, None)
334358
}
335359
})

crates/ide-db/src/symbol_index.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -206,13 +206,13 @@ impl SymbolIndex {
206206

207207
// We call this without attaching because this runs in parallel, so we need to attach here.
208208
hir::attach_db(db, || {
209-
let mut symbol_collector = SymbolCollector::new(db);
209+
let mut symbol_collector = SymbolCollector::new(db, true);
210210

211211
db.source_root_crates(source_root_id.id(db))
212212
.iter()
213213
.flat_map(|&krate| Crate::from(krate).modules(db))
214214
// we specifically avoid calling other SymbolsDatabase queries here, even though they do the same thing,
215-
// as the index for a library is not going to really ever change, and we do not want to store each
215+
// as the index for a library is not going to really ever change, and we do not want to store
216216
// the module or crate indices for those in salsa unless we need to.
217217
.for_each(|module| symbol_collector.collect(module));
218218

@@ -237,7 +237,12 @@ impl SymbolIndex {
237237

238238
// We call this without attaching because this runs in parallel, so we need to attach here.
239239
hir::attach_db(db, || {
240-
SymbolIndex::new(SymbolCollector::new_module(db, module.id(db).into()))
240+
let module: Module = module.id(db).into();
241+
SymbolIndex::new(SymbolCollector::new_module(
242+
db,
243+
module,
244+
!module.krate().origin(db).is_local(),
245+
))
241246
})
242247
}
243248

@@ -508,7 +513,7 @@ pub(self) use crate::Trait as IsThisJustATrait;
508513
.modules(&db)
509514
.into_iter()
510515
.map(|module_id| {
511-
let mut symbols = SymbolCollector::new_module(&db, module_id);
516+
let mut symbols = SymbolCollector::new_module(&db, module_id, false);
512517
symbols.sort_by_key(|it| it.name.as_str().to_owned());
513518
(module_id, symbols)
514519
})
@@ -535,7 +540,7 @@ struct Duplicate;
535540
.modules(&db)
536541
.into_iter()
537542
.map(|module_id| {
538-
let mut symbols = SymbolCollector::new_module(&db, module_id);
543+
let mut symbols = SymbolCollector::new_module(&db, module_id, false);
539544
symbols.sort_by_key(|it| it.name.as_str().to_owned());
540545
(module_id, symbols)
541546
})

0 commit comments

Comments
 (0)