Skip to content

Commit 38e8f35

Browse files
Merge #4501
4501: Querify `importable_locations_in_crate` r=jonas-schievink a=jonas-schievink This brings the time needed to compute the `add_missing_impl_members` assist down from ~5 minutes to 20 seconds on my test workload (which is editing within an impl of a MIR [`MutVisitor`](https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/mir/visit/trait.MutVisitor.html)) cc #4498 Co-authored-by: Jonas Schievink <[email protected]>
2 parents 9bdedbb + 8f80df1 commit 38e8f35

File tree

2 files changed

+18
-5
lines changed

2 files changed

+18
-5
lines changed

crates/ra_hir_def/src/db.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! Defines database & queries for name resolution.
22
use std::sync::Arc;
33

4-
use hir_expand::{db::AstDatabase, HirFileId};
4+
use hir_expand::{db::AstDatabase, name::Name, HirFileId};
55
use ra_db::{salsa, CrateId, SourceDatabase, Upcast};
66
use ra_prof::profile;
77
use ra_syntax::SmolStr;
@@ -12,9 +12,12 @@ use crate::{
1212
body::{scope::ExprScopes, Body, BodySourceMap},
1313
data::{ConstData, FunctionData, ImplData, StaticData, TraitData, TypeAliasData},
1414
docs::Documentation,
15+
find_path,
1516
generics::GenericParams,
17+
item_scope::ItemInNs,
1618
lang_item::{LangItemTarget, LangItems},
1719
nameres::{raw::RawItems, CrateDefMap},
20+
visibility::Visibility,
1821
AttrDefId, ConstId, ConstLoc, DefWithBodyId, EnumId, EnumLoc, FunctionId, FunctionLoc,
1922
GenericDefId, ImplId, ImplLoc, ModuleId, StaticId, StaticLoc, StructId, StructLoc, TraitId,
2023
TraitLoc, TypeAliasId, TypeAliasLoc, UnionId, UnionLoc,
@@ -108,6 +111,13 @@ pub trait DefDatabase: InternDatabase + AstDatabase + Upcast<dyn AstDatabase> {
108111
// Remove this query completely, in favor of `Attrs::docs` method
109112
#[salsa::invoke(Documentation::documentation_query)]
110113
fn documentation(&self, def: AttrDefId) -> Option<Documentation>;
114+
115+
#[salsa::invoke(find_path::importable_locations_in_crate)]
116+
fn importable_locations_of(
117+
&self,
118+
item: ItemInNs,
119+
krate: CrateId,
120+
) -> Arc<[(ModuleId, Name, Visibility)]>;
111121
}
112122

113123
fn crate_def_map_wait(db: &impl DefDatabase, krate: CrateId) -> Arc<CrateDefMap> {

crates/ra_hir_def/src/find_path.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use crate::{
88
CrateId, ModuleDefId, ModuleId,
99
};
1010
use hir_expand::name::{known, AsName, Name};
11+
use std::sync::Arc;
1112
use test_utils::tested_by;
1213

1314
const MAX_PATH_LEN: usize = 15;
@@ -45,6 +46,7 @@ impl ModPath {
4546
/// Find a path that can be used to refer to a certain item. This can depend on
4647
/// *from where* you're referring to the item, hence the `from` parameter.
4748
pub fn find_path(db: &dyn DefDatabase, item: ItemInNs, from: ModuleId) -> Option<ModPath> {
49+
let _p = ra_prof::profile("find_path");
4850
find_path_inner(db, item, from, MAX_PATH_LEN)
4951
}
5052

@@ -198,7 +200,7 @@ fn find_importable_locations(
198200
.chain(crate_graph[from.krate].dependencies.iter().map(|dep| dep.crate_id))
199201
{
200202
result.extend(
201-
importable_locations_in_crate(db, item, krate)
203+
db.importable_locations_of(item, krate)
202204
.iter()
203205
.filter(|(_, _, vis)| vis.is_visible_from(db, from))
204206
.map(|(m, n, _)| (*m, n.clone())),
@@ -213,11 +215,11 @@ fn find_importable_locations(
213215
///
214216
/// Note that the crate doesn't need to be the one in which the item is defined;
215217
/// it might be re-exported in other crates.
216-
fn importable_locations_in_crate(
218+
pub(crate) fn importable_locations_in_crate(
217219
db: &dyn DefDatabase,
218220
item: ItemInNs,
219221
krate: CrateId,
220-
) -> Vec<(ModuleId, Name, Visibility)> {
222+
) -> Arc<[(ModuleId, Name, Visibility)]> {
221223
let def_map = db.crate_def_map(krate);
222224
let mut result = Vec::new();
223225
for (local_id, data) in def_map.modules.iter() {
@@ -243,7 +245,8 @@ fn importable_locations_in_crate(
243245
result.push((ModuleId { krate, local_id }, name.clone(), vis));
244246
}
245247
}
246-
result
248+
249+
Arc::from(result)
247250
}
248251

249252
#[cfg(test)]

0 commit comments

Comments
 (0)