From 2c1777a2e264e58fccd5ace94b238c8a497ddbda Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Tue, 12 Jan 2021 15:51:02 +0100 Subject: Ensure uniqueness of file ids in reference search via hashmap --- crates/ssr/src/search.rs | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) (limited to 'crates/ssr/src/search.rs') diff --git a/crates/ssr/src/search.rs b/crates/ssr/src/search.rs index a1d653aff..a3eb2e800 100644 --- a/crates/ssr/src/search.rs +++ b/crates/ssr/src/search.rs @@ -20,7 +20,7 @@ use test_utils::mark; /// them more than once. #[derive(Default)] pub(crate) struct UsageCache { - usages: Vec<(Definition, Vec)>, + usages: Vec<(Definition, FileReferences)>, } impl<'db> MatchFinder<'db> { @@ -58,11 +58,7 @@ impl<'db> MatchFinder<'db> { ) { if let Some(resolved_path) = pick_path_for_usages(pattern) { let definition: Definition = resolved_path.resolution.clone().into(); - for file_range in self - .find_usages(usage_cache, definition) - .iter() - .flat_map(FileReferences::file_ranges) - { + for file_range in self.find_usages(usage_cache, definition).file_ranges() { if let Some(node_to_match) = self.find_node_to_match(resolved_path, file_range) { if !is_search_permitted_ancestors(&node_to_match) { mark::hit!(use_declaration_with_braces); @@ -112,7 +108,7 @@ impl<'db> MatchFinder<'db> { &self, usage_cache: &'a mut UsageCache, definition: Definition, - ) -> &'a [FileReferences] { + ) -> &'a FileReferences { // Logically if a lookup succeeds we should just return it. Unfortunately returning it would // extend the lifetime of the borrow, then we wouldn't be able to do the insertion on a // cache miss. This is a limitation of NLL and is fixed with Polonius. For now we do two @@ -254,7 +250,7 @@ fn is_search_permitted(node: &SyntaxNode) -> bool { } impl UsageCache { - fn find(&mut self, definition: &Definition) -> Option<&[FileReferences]> { + fn find(&mut self, definition: &Definition) -> Option<&FileReferences> { // We expect a very small number of cache entries (generally 1), so a linear scan should be // fast enough and avoids the need to implement Hash for Definition. for (d, refs) in &self.usages { -- cgit v1.2.3