7077: Avoid a couple of allocations r=Veykril a=lnicola

r? @Veykril 

TBH I'm not sure this is worth it, but the other `Query` was already taking a `String`.

Co-authored-by: Laurențiu Nicola <lnicola@dend.ro>
This commit is contained in:
bors[bot] 2020-12-29 13:15:07 +00:00 committed by GitHub
commit 343029fa9e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 61 additions and 60 deletions

View file

@ -91,7 +91,7 @@ pub(crate) fn extract_variable(acc: &mut Assists, ctx: &AssistContext) -> Option
// extra newlines in the indent block
let text = indent.text();
if text.starts_with('\n') {
buf.push_str("\n");
buf.push('\n');
buf.push_str(text.trim_start_matches('\n'));
} else {
buf.push_str(text);

View file

@ -62,21 +62,22 @@ pub(crate) fn replace_derive_with_manual_impl(
let current_module = ctx.sema.scope(annotated_name.syntax()).module()?;
let current_crate = current_module.krate();
let found_traits =
imports_locator::find_exact_imports(&ctx.sema, current_crate, trait_token.text())
.filter_map(
|candidate: either::Either<hir::ModuleDef, hir::MacroDef>| match candidate {
either::Either::Left(hir::ModuleDef::Trait(trait_)) => Some(trait_),
_ => None,
},
)
.flat_map(|trait_| {
current_module
.find_use_path(ctx.sema.db, hir::ModuleDef::Trait(trait_))
.as_ref()
.map(mod_path_to_ast)
.zip(Some(trait_))
});
let found_traits = imports_locator::find_exact_imports(
&ctx.sema,
current_crate,
trait_token.text().to_string(),
)
.filter_map(|candidate: either::Either<hir::ModuleDef, hir::MacroDef>| match candidate {
either::Either::Left(hir::ModuleDef::Trait(trait_)) => Some(trait_),
_ => None,
})
.flat_map(|trait_| {
current_module
.find_use_path(ctx.sema.db, hir::ModuleDef::Trait(trait_))
.as_ref()
.map(mod_path_to_ast)
.zip(Some(trait_))
});
let mut no_traits_found = true;
for (trait_path, trait_) in found_traits.inspect(|_| no_traits_found = false) {

View file

@ -179,25 +179,24 @@ impl ImportAssets {
}
};
let mut res =
imports_locator::find_exact_imports(sema, current_crate, &self.get_search_query())
.filter_map(filter)
.filter_map(|candidate| {
let item: hir::ItemInNs = candidate.either(Into::into, Into::into);
if let Some(prefix_kind) = prefixed {
self.module_with_name_to_import.find_use_path_prefixed(
db,
item,
prefix_kind,
)
} else {
self.module_with_name_to_import.find_use_path(db, item)
}
.map(|path| (path, item))
})
.filter(|(use_path, _)| use_path.len() > 1)
.take(20)
.collect::<Vec<_>>();
let mut res = imports_locator::find_exact_imports(
sema,
current_crate,
self.get_search_query().to_string(),
)
.filter_map(filter)
.filter_map(|candidate| {
let item: hir::ItemInNs = candidate.either(Into::into, Into::into);
if let Some(prefix_kind) = prefixed {
self.module_with_name_to_import.find_use_path_prefixed(db, item, prefix_kind)
} else {
self.module_with_name_to_import.find_use_path(db, item)
}
.map(|path| (path, item))
})
.filter(|(use_path, _)| use_path.len() > 1)
.take(20)
.collect::<Vec<_>>();
res.sort_by_key(|(path, _)| path.clone());
res
}

View file

@ -135,11 +135,12 @@ fn fuzzy_completion(acc: &mut Completions, ctx: &CompletionContext) -> Option<()
let anchor = ctx.name_ref_syntax.as_ref()?;
let import_scope = ImportScope::find_insert_use_container(anchor.syntax(), &ctx.sema)?;
let user_input_lowercased = potential_import_name.to_lowercase();
let mut all_mod_paths = imports_locator::find_similar_imports(
&ctx.sema,
ctx.krate?,
Some(40),
&potential_import_name,
potential_import_name,
true,
)
.filter_map(|import_candidate| {
@ -155,7 +156,6 @@ fn fuzzy_completion(acc: &mut Completions, ctx: &CompletionContext) -> Option<()
.filter(|(mod_path, _)| mod_path.len() > 1)
.collect::<Vec<_>>();
let user_input_lowercased = potential_import_name.to_lowercase();
all_mod_paths.sort_by_cached_key(|(mod_path, _)| {
compute_fuzzy_completion_order_key(mod_path, &user_input_lowercased)
});

View file

@ -137,7 +137,7 @@ pub fn resolve_completion_edits(
config: &CompletionConfig,
position: FilePosition,
full_import_path: &str,
imported_name: &str,
imported_name: String,
) -> Option<Vec<TextEdit>> {
let ctx = CompletionContext::new(db, position, config)?;
let anchor = ctx.name_ref_syntax.as_ref()?;

View file

@ -262,10 +262,11 @@ pub struct Query {
}
impl Query {
pub fn new(query: &str) -> Self {
pub fn new(query: String) -> Self {
let lowercased = query.to_lowercase();
Self {
query: query.to_string(),
lowercased: query.to_lowercase(),
query,
lowercased,
name_only: false,
search_mode: SearchMode::Contains,
case_sensitive: false,
@ -774,7 +775,7 @@ mod tests {
check_search(
ra_fixture,
"main",
Query::new("fmt").search_mode(SearchMode::Fuzzy),
Query::new("fmt".to_string()).search_mode(SearchMode::Fuzzy),
expect![[r#"
dep::fmt (t)
dep::Fmt (t)
@ -789,7 +790,7 @@ mod tests {
check_search(
ra_fixture,
"main",
Query::new("fmt").search_mode(SearchMode::Equals),
Query::new("fmt".to_string()).search_mode(SearchMode::Equals),
expect![[r#"
dep::fmt (t)
dep::Fmt (t)
@ -802,7 +803,7 @@ mod tests {
check_search(
ra_fixture,
"main",
Query::new("fmt").search_mode(SearchMode::Contains),
Query::new("fmt".to_string()).search_mode(SearchMode::Contains),
expect![[r#"
dep::fmt (t)
dep::Fmt (t)
@ -843,7 +844,7 @@ mod tests {
check_search(
ra_fixture,
"main",
Query::new("fmt"),
Query::new("fmt".to_string()),
expect![[r#"
dep::fmt (t)
dep::Fmt (t)
@ -857,7 +858,7 @@ mod tests {
check_search(
ra_fixture,
"main",
Query::new("fmt").name_only(),
Query::new("fmt".to_string()).name_only(),
expect![[r#"
dep::fmt (t)
dep::Fmt (t)
@ -881,7 +882,7 @@ mod tests {
check_search(
ra_fixture,
"main",
Query::new("FMT"),
Query::new("FMT".to_string()),
expect![[r#"
dep::fmt (t)
dep::fmt (v)
@ -893,7 +894,7 @@ mod tests {
check_search(
ra_fixture,
"main",
Query::new("FMT").case_sensitive(),
Query::new("FMT".to_string()).case_sensitive(),
expect![[r#"
dep::FMT (t)
dep::FMT (v)
@ -922,7 +923,7 @@ mod tests {
pub fn no() {}
"#,
"main",
Query::new("").limit(2),
Query::new("".to_string()).limit(2),
expect![[r#"
dep::fmt (t)
dep::Fmt (t)
@ -943,7 +944,7 @@ mod tests {
check_search(
ra_fixture,
"main",
Query::new("FMT"),
Query::new("FMT".to_string()),
expect![[r#"
dep::fmt (t)
dep::fmt (v)
@ -955,7 +956,7 @@ mod tests {
check_search(
ra_fixture,
"main",
Query::new("FMT").exclude_import_kind(ImportKind::Adt),
Query::new("FMT".to_string()).exclude_import_kind(ImportKind::Adt),
expect![[r#""#]],
);
}

View file

@ -249,7 +249,7 @@ impl CrateDefMap {
buf.push_str(" _");
}
buf.push_str("\n");
buf.push('\n');
}
for (name, child) in map.modules[module].children.iter() {

View file

@ -475,7 +475,7 @@ impl Analysis {
config: &CompletionConfig,
position: FilePosition,
full_import_path: &str,
imported_name: &str,
imported_name: String,
) -> Cancelable<Vec<TextEdit>> {
Ok(self
.with_db(|db| {

View file

@ -15,14 +15,14 @@ use rustc_hash::FxHashSet;
pub fn find_exact_imports<'a>(
sema: &Semantics<'a, RootDatabase>,
krate: Crate,
name_to_import: &str,
name_to_import: String,
) -> impl Iterator<Item = Either<ModuleDef, MacroDef>> {
let _p = profile::span("find_exact_imports");
find_imports(
sema,
krate,
{
let mut local_query = symbol_index::Query::new(name_to_import.to_string());
let mut local_query = symbol_index::Query::new(name_to_import.clone());
local_query.exact();
local_query.limit(40);
local_query
@ -39,18 +39,18 @@ pub fn find_similar_imports<'a>(
sema: &Semantics<'a, RootDatabase>,
krate: Crate,
limit: Option<usize>,
fuzzy_search_string: &str,
fuzzy_search_string: String,
name_only: bool,
) -> impl Iterator<Item = Either<ModuleDef, MacroDef>> {
let _p = profile::span("find_similar_imports");
let mut external_query =
import_map::Query::new(fuzzy_search_string).search_mode(import_map::SearchMode::Fuzzy);
let mut external_query = import_map::Query::new(fuzzy_search_string.clone())
.search_mode(import_map::SearchMode::Fuzzy);
if name_only {
external_query = external_query.name_only();
}
let mut local_query = symbol_index::Query::new(fuzzy_search_string.to_string());
let mut local_query = symbol_index::Query::new(fuzzy_search_string);
if let Some(limit) = limit {
local_query.limit(limit);

View file

@ -681,7 +681,7 @@ pub(crate) fn handle_completion_resolve(
&snap.config.completion,
FilePosition { file_id, offset },
&resolve_data.full_import_path,
&resolve_data.imported_name,
resolve_data.imported_name,
)?
.into_iter()
.flat_map(|edit| {