From 8f17f3d594f599b5f1a9c9960d363513368e0958 Mon Sep 17 00:00:00 2001 From: Kirill Bulatov Date: Mon, 8 Mar 2021 11:48:51 +0200 Subject: Deduplicate search_dependencies results --- crates/hir_def/src/import_map.rs | 37 +++++++++++++++++++------------------ 1 file changed, 19 insertions(+), 18 deletions(-) diff --git a/crates/hir_def/src/import_map.rs b/crates/hir_def/src/import_map.rs index 0a3dc7956..420619116 100644 --- a/crates/hir_def/src/import_map.rs +++ b/crates/hir_def/src/import_map.rs @@ -388,7 +388,7 @@ pub fn search_dependencies<'a>( db: &'a dyn DefDatabase, krate: CrateId, query: Query, -) -> Vec { +) -> FxHashSet { let _p = profile::span("search_dependencies").detail(|| format!("{:?}", query)); let graph = db.crate_graph(); @@ -403,7 +403,7 @@ pub fn search_dependencies<'a>( } let mut stream = op.union(); - let mut res = Vec::new(); + let mut res = FxHashSet::default(); while let Some((_, indexed_values)) = stream.next() { for indexed_value in indexed_values { let import_map = &import_maps[indexed_value.index]; @@ -435,7 +435,6 @@ pub fn search_dependencies<'a>( res.extend(iter); if res.len() >= query.limit { - res.truncate(query.limit); return res; } } @@ -821,10 +820,10 @@ mod tests { Query::new("fmt".to_string()).search_mode(SearchMode::Fuzzy), expect![[r#" dep::fmt (t) + dep::fmt::Display::format_method (a) dep::fmt::Display (t) dep::fmt::Display::FMT_CONST (a) dep::fmt::Display::format_function (a) - dep::fmt::Display::format_method (a) "#]], ); } @@ -850,9 +849,9 @@ mod tests { "main", Query::new("fmt".to_string()).search_mode(SearchMode::Fuzzy).assoc_items_only(), expect![[r#" + dep::fmt::Display::format_method (a) dep::fmt::Display::FMT_CONST (a) dep::fmt::Display::format_function (a) - dep::fmt::Display::format_method (a) "#]], ); @@ -911,12 +910,12 @@ mod tests { Query::new("fmt".to_string()).search_mode(SearchMode::Fuzzy), expect![[r#" dep::fmt (t) - dep::Fmt (t) + dep::format (f) dep::Fmt (v) - dep::Fmt (m) dep::fmt::Display (t) + dep::Fmt (t) dep::fmt::Display::fmt (a) - dep::format (f) + dep::Fmt (m) "#]], ); @@ -926,10 +925,10 @@ mod tests { Query::new("fmt".to_string()).search_mode(SearchMode::Equals), expect![[r#" dep::fmt (t) - dep::Fmt (t) dep::Fmt (v) - dep::Fmt (m) + dep::Fmt (t) dep::fmt::Display::fmt (a) + dep::Fmt (m) "#]], ); @@ -939,11 +938,11 @@ mod tests { Query::new("fmt".to_string()).search_mode(SearchMode::Contains), expect![[r#" dep::fmt (t) - dep::Fmt (t) dep::Fmt (v) - dep::Fmt (m) dep::fmt::Display (t) + dep::Fmt (t) dep::fmt::Display::fmt (a) + dep::Fmt (m) "#]], ); } @@ -980,11 +979,11 @@ mod tests { Query::new("fmt".to_string()), expect![[r#" dep::fmt (t) - dep::Fmt (t) dep::Fmt (v) - dep::Fmt (m) dep::fmt::Display (t) + dep::Fmt (t) dep::fmt::Display::fmt (a) + dep::Fmt (m) "#]], ); @@ -994,10 +993,10 @@ mod tests { Query::new("fmt".to_string()).name_only(), expect![[r#" dep::fmt (t) - dep::Fmt (t) dep::Fmt (v) - dep::Fmt (m) + dep::Fmt (t) dep::fmt::Display::fmt (a) + dep::Fmt (m) "#]], ); } @@ -1018,9 +1017,9 @@ mod tests { Query::new("FMT".to_string()), expect![[r#" dep::fmt (t) + dep::FMT (v) dep::fmt (v) dep::FMT (t) - dep::FMT (v) "#]], ); @@ -1060,6 +1059,8 @@ mod tests { expect![[r#" dep::fmt (t) dep::Fmt (t) + dep::Fmt (m) + dep::Fmt (v) "#]], ); } @@ -1080,9 +1081,9 @@ mod tests { Query::new("FMT".to_string()), expect![[r#" dep::fmt (t) + dep::FMT (v) dep::fmt (v) dep::FMT (t) - dep::FMT (v) "#]], ); -- cgit v1.2.3 From 128a6a4ec060a73fbd4e1a8dc470cb629fcdbe45 Mon Sep 17 00:00:00 2001 From: Kirill Bulatov Date: Mon, 8 Mar 2021 12:06:15 +0200 Subject: Do not process indexed values more than once --- crates/hir_def/src/import_map.rs | 60 +++++++++++++++++++++------------------- 1 file changed, 31 insertions(+), 29 deletions(-) diff --git a/crates/hir_def/src/import_map.rs b/crates/hir_def/src/import_map.rs index 420619116..e1c28bc83 100644 --- a/crates/hir_def/src/import_map.rs +++ b/crates/hir_def/src/import_map.rs @@ -403,40 +403,42 @@ pub fn search_dependencies<'a>( } let mut stream = op.union(); - let mut res = FxHashSet::default(); + + let mut all_indexed_values = FxHashSet::default(); while let Some((_, indexed_values)) = stream.next() { - for indexed_value in indexed_values { - let import_map = &import_maps[indexed_value.index]; - let importables = &import_map.importables[indexed_value.value as usize..]; + all_indexed_values.extend(indexed_values.iter().copied()); + } - let common_importable_data = &import_map.map[&importables[0]]; - if !query.import_matches(common_importable_data, true) { - continue; - } + let mut res = FxHashSet::default(); + for indexed_value in all_indexed_values { + let import_map = &import_maps[indexed_value.index]; + let importables = &import_map.importables[indexed_value.value as usize..]; - // Path shared by the importable items in this group. - let common_importables_path_fst = fst_path(&common_importable_data.path); - // Add the items from this `ModPath` group. Those are all subsequent items in - // `importables` whose paths match `path`. - let iter = importables - .iter() - .copied() - .take_while(|item| { - common_importables_path_fst == fst_path(&import_map.map[item].path) - }) - .filter(|&item| match item_import_kind(item) { - Some(import_kind) => !query.exclude_import_kinds.contains(&import_kind), - None => true, - }) - .filter(|item| { - !query.case_sensitive // we've already checked the common importables path case-insensitively + let common_importable_data = &import_map.map[&importables[0]]; + if !query.import_matches(common_importable_data, true) { + continue; + } + + // Path shared by the importable items in this group. + let common_importables_path_fst = fst_path(&common_importable_data.path); + // Add the items from this `ModPath` group. Those are all subsequent items in + // `importables` whose paths match `path`. + let iter = importables + .iter() + .copied() + .take_while(|item| common_importables_path_fst == fst_path(&import_map.map[item].path)) + .filter(|&item| match item_import_kind(item) { + Some(import_kind) => !query.exclude_import_kinds.contains(&import_kind), + None => true, + }) + .filter(|item| { + !query.case_sensitive // we've already checked the common importables path case-insensitively || query.import_matches(&import_map.map[item], false) - }); - res.extend(iter); + }); + res.extend(iter); - if res.len() >= query.limit { - return res; - } + if res.len() >= query.limit { + return res; } } -- cgit v1.2.3