about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2021-11-10 15:12:05 +0000
committerGitHub <noreply@github.com>2021-11-10 15:12:05 +0000
commitf724c84e7d50e8f4fe84e7842008c1e4d33ef717 (patch)
tree4aeb382ec7bda89a173908596e2f01576fdb7cbc
parent899610778bbd7e3c7b6a3e52085b19f3721b483e (diff)
parent3c8898b1b170ff94450521755579a1971266e70e (diff)
downloadrust-f724c84e7d50e8f4fe84e7842008c1e4d33ef717.tar.gz
rust-f724c84e7d50e8f4fe84e7842008c1e4d33ef717.zip
Merge #10738
10738: internal: Do not search through all three namespaces in `ItemScope::name_of` r=Veykril a=Veykril

Brings down `5ms - find_path_prefixed (46 calls)` to `1ms - find_path_prefixed (46 calls)` for me on the `integrated_completion_benchmark`.
Still `O(n)` but this should considerably cut down lookups nevertheless(as shown by the timings already).
bors r+

Co-authored-by: Lukas Wirth <lukastw97@gmail.com>
-rw-r--r--crates/hir_def/src/item_scope.rs29
1 files changed, 10 insertions, 19 deletions
diff --git a/crates/hir_def/src/item_scope.rs b/crates/hir_def/src/item_scope.rs
index ec8c3a87c0f..0a2a6719edc 100644
--- a/crates/hir_def/src/item_scope.rs
+++ b/crates/hir_def/src/item_scope.rs
@@ -136,12 +136,17 @@ impl ItemScope {
 
     /// XXX: this is O(N) rather than O(1), try to not introduce new usages.
     pub(crate) fn name_of(&self, item: ItemInNs) -> Option<(&Name, Visibility)> {
-        for (name, per_ns) in self.entries() {
-            if let Some(vis) = item.match_with(per_ns) {
-                return Some((name, vis));
+        let (def, mut iter) = match item {
+            ItemInNs::Macros(def) => {
+                return self
+                    .macros
+                    .iter()
+                    .find_map(|(name, &(other_def, vis))| (other_def == def).then(|| (name, vis)));
             }
-        }
-        None
+            ItemInNs::Types(def) => (def, self.types.iter()),
+            ItemInNs::Values(def) => (def, self.values.iter()),
+        };
+        iter.find_map(|(name, &(other_def, vis))| (other_def == def).then(|| (name, vis)))
     }
 
     pub(crate) fn traits<'a>(&'a self) -> impl Iterator<Item = TraitId> + 'a {
@@ -386,20 +391,6 @@ pub enum ItemInNs {
 }
 
 impl ItemInNs {
-    fn match_with(self, per_ns: PerNs) -> Option<Visibility> {
-        match self {
-            ItemInNs::Types(def) => {
-                per_ns.types.filter(|(other_def, _)| *other_def == def).map(|(_, vis)| vis)
-            }
-            ItemInNs::Values(def) => {
-                per_ns.values.filter(|(other_def, _)| *other_def == def).map(|(_, vis)| vis)
-            }
-            ItemInNs::Macros(def) => {
-                per_ns.macros.filter(|(other_def, _)| *other_def == def).map(|(_, vis)| vis)
-            }
-        }
-    }
-
     pub fn as_module_def_id(self) -> Option<ModuleDefId> {
         match self {
             ItemInNs::Types(id) | ItemInNs::Values(id) => Some(id),