Skip to content

Commit 6952a3b

Browse files
Merge #7912
7912: Dedupe import map results r=matklad a=SomeoneToIgnore While debugging #7902, I've found that there are some duplicates are produced during the external dependencies lookup. I've spotted at least some of the `indexed_value.value` duplicated when typed `Arc` and requested the completions for that, so I've also deduped the `IndexedValue`'s to avoid unnecessary computations. This helps to show `Arc` in the completion suggestions in a zero dependency project and in `hir` module, but we loose it again in the `ide` module. Co-authored-by: Kirill Bulatov <mail4score@gmail.com>
2 parents 13982e4 + 128a6a4 commit 6952a3b

File tree

1 file changed

+49
-46
lines changed

1 file changed

+49
-46
lines changed

crates/hir_def/src/import_map.rs

Lines changed: 49 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,7 @@ pub fn search_dependencies<'a>(
388388
db: &'a dyn DefDatabase,
389389
krate: CrateId,
390390
query: Query,
391-
) -> Vec<ItemInNs> {
391+
) -> FxHashSet<ItemInNs> {
392392
let _p = profile::span("search_dependencies").detail(|| format!("{:?}", query));
393393

394394
let graph = db.crate_graph();
@@ -403,41 +403,42 @@ pub fn search_dependencies<'a>(
403403
}
404404

405405
let mut stream = op.union();
406-
let mut res = Vec::new();
406+
407+
let mut all_indexed_values = FxHashSet::default();
407408
while let Some((_, indexed_values)) = stream.next() {
408-
for indexed_value in indexed_values {
409-
let import_map = &import_maps[indexed_value.index];
410-
let importables = &import_map.importables[indexed_value.value as usize..];
409+
all_indexed_values.extend(indexed_values.iter().copied());
410+
}
411411

412-
let common_importable_data = &import_map.map[&importables[0]];
413-
if !query.import_matches(common_importable_data, true) {
414-
continue;
415-
}
412+
let mut res = FxHashSet::default();
413+
for indexed_value in all_indexed_values {
414+
let import_map = &import_maps[indexed_value.index];
415+
let importables = &import_map.importables[indexed_value.value as usize..];
416416

417-
// Path shared by the importable items in this group.
418-
let common_importables_path_fst = fst_path(&common_importable_data.path);
419-
// Add the items from this `ModPath` group. Those are all subsequent items in
420-
// `importables` whose paths match `path`.
421-
let iter = importables
422-
.iter()
423-
.copied()
424-
.take_while(|item| {
425-
common_importables_path_fst == fst_path(&import_map.map[item].path)
426-
})
427-
.filter(|&item| match item_import_kind(item) {
428-
Some(import_kind) => !query.exclude_import_kinds.contains(&import_kind),
429-
None => true,
430-
})
431-
.filter(|item| {
432-
!query.case_sensitive // we've already checked the common importables path case-insensitively
417+
let common_importable_data = &import_map.map[&importables[0]];
418+
if !query.import_matches(common_importable_data, true) {
419+
continue;
420+
}
421+
422+
// Path shared by the importable items in this group.
423+
let common_importables_path_fst = fst_path(&common_importable_data.path);
424+
// Add the items from this `ModPath` group. Those are all subsequent items in
425+
// `importables` whose paths match `path`.
426+
let iter = importables
427+
.iter()
428+
.copied()
429+
.take_while(|item| common_importables_path_fst == fst_path(&import_map.map[item].path))
430+
.filter(|&item| match item_import_kind(item) {
431+
Some(import_kind) => !query.exclude_import_kinds.contains(&import_kind),
432+
None => true,
433+
})
434+
.filter(|item| {
435+
!query.case_sensitive // we've already checked the common importables path case-insensitively
433436
|| query.import_matches(&import_map.map[item], false)
434-
});
435-
res.extend(iter);
437+
});
438+
res.extend(iter);
436439

437-
if res.len() >= query.limit {
438-
res.truncate(query.limit);
439-
return res;
440-
}
440+
if res.len() >= query.limit {
441+
return res;
441442
}
442443
}
443444

@@ -821,10 +822,10 @@ mod tests {
821822
Query::new("fmt".to_string()).search_mode(SearchMode::Fuzzy),
822823
expect![[r#"
823824
dep::fmt (t)
825+
dep::fmt::Display::format_method (a)
824826
dep::fmt::Display (t)
825827
dep::fmt::Display::FMT_CONST (a)
826828
dep::fmt::Display::format_function (a)
827-
dep::fmt::Display::format_method (a)
828829
"#]],
829830
);
830831
}
@@ -850,9 +851,9 @@ mod tests {
850851
"main",
851852
Query::new("fmt".to_string()).search_mode(SearchMode::Fuzzy).assoc_items_only(),
852853
expect![[r#"
854+
dep::fmt::Display::format_method (a)
853855
dep::fmt::Display::FMT_CONST (a)
854856
dep::fmt::Display::format_function (a)
855-
dep::fmt::Display::format_method (a)
856857
"#]],
857858
);
858859

@@ -911,12 +912,12 @@ mod tests {
911912
Query::new("fmt".to_string()).search_mode(SearchMode::Fuzzy),
912913
expect![[r#"
913914
dep::fmt (t)
914-
dep::Fmt (t)
915+
dep::format (f)
915916
dep::Fmt (v)
916-
dep::Fmt (m)
917917
dep::fmt::Display (t)
918+
dep::Fmt (t)
918919
dep::fmt::Display::fmt (a)
919-
dep::format (f)
920+
dep::Fmt (m)
920921
"#]],
921922
);
922923

@@ -926,10 +927,10 @@ mod tests {
926927
Query::new("fmt".to_string()).search_mode(SearchMode::Equals),
927928
expect![[r#"
928929
dep::fmt (t)
929-
dep::Fmt (t)
930930
dep::Fmt (v)
931-
dep::Fmt (m)
931+
dep::Fmt (t)
932932
dep::fmt::Display::fmt (a)
933+
dep::Fmt (m)
933934
"#]],
934935
);
935936

@@ -939,11 +940,11 @@ mod tests {
939940
Query::new("fmt".to_string()).search_mode(SearchMode::Contains),
940941
expect![[r#"
941942
dep::fmt (t)
942-
dep::Fmt (t)
943943
dep::Fmt (v)
944-
dep::Fmt (m)
945944
dep::fmt::Display (t)
945+
dep::Fmt (t)
946946
dep::fmt::Display::fmt (a)
947+
dep::Fmt (m)
947948
"#]],
948949
);
949950
}
@@ -980,11 +981,11 @@ mod tests {
980981
Query::new("fmt".to_string()),
981982
expect![[r#"
982983
dep::fmt (t)
983-
dep::Fmt (t)
984984
dep::Fmt (v)
985-
dep::Fmt (m)
986985
dep::fmt::Display (t)
986+
dep::Fmt (t)
987987
dep::fmt::Display::fmt (a)
988+
dep::Fmt (m)
988989
"#]],
989990
);
990991

@@ -994,10 +995,10 @@ mod tests {
994995
Query::new("fmt".to_string()).name_only(),
995996
expect![[r#"
996997
dep::fmt (t)
997-
dep::Fmt (t)
998998
dep::Fmt (v)
999-
dep::Fmt (m)
999+
dep::Fmt (t)
10001000
dep::fmt::Display::fmt (a)
1001+
dep::Fmt (m)
10011002
"#]],
10021003
);
10031004
}
@@ -1018,9 +1019,9 @@ mod tests {
10181019
Query::new("FMT".to_string()),
10191020
expect![[r#"
10201021
dep::fmt (t)
1022+
dep::FMT (v)
10211023
dep::fmt (v)
10221024
dep::FMT (t)
1023-
dep::FMT (v)
10241025
"#]],
10251026
);
10261027

@@ -1060,6 +1061,8 @@ mod tests {
10601061
expect![[r#"
10611062
dep::fmt (t)
10621063
dep::Fmt (t)
1064+
dep::Fmt (m)
1065+
dep::Fmt (v)
10631066
"#]],
10641067
);
10651068
}
@@ -1080,9 +1083,9 @@ mod tests {
10801083
Query::new("FMT".to_string()),
10811084
expect![[r#"
10821085
dep::fmt (t)
1086+
dep::FMT (v)
10831087
dep::fmt (v)
10841088
dep::FMT (t)
1085-
dep::FMT (v)
10861089
"#]],
10871090
);
10881091

0 commit comments

Comments
 (0)