Skip to content

Commit 3c8898b

Browse files
committed
Do not search through all three namespaces in ItemScope::name_of
1 parent ab657af commit 3c8898b

File tree

1 file changed

+10
-19
lines changed

1 file changed

+10
-19
lines changed

crates/hir_def/src/item_scope.rs

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -136,12 +136,17 @@ impl ItemScope {
136136

137137
/// XXX: this is O(N) rather than O(1), try to not introduce new usages.
138138
pub(crate) fn name_of(&self, item: ItemInNs) -> Option<(&Name, Visibility)> {
139-
for (name, per_ns) in self.entries() {
140-
if let Some(vis) = item.match_with(per_ns) {
141-
return Some((name, vis));
139+
let (def, mut iter) = match item {
140+
ItemInNs::Macros(def) => {
141+
return self
142+
.macros
143+
.iter()
144+
.find_map(|(name, &(other_def, vis))| (other_def == def).then(|| (name, vis)));
142145
}
143-
}
144-
None
146+
ItemInNs::Types(def) => (def, self.types.iter()),
147+
ItemInNs::Values(def) => (def, self.values.iter()),
148+
};
149+
iter.find_map(|(name, &(other_def, vis))| (other_def == def).then(|| (name, vis)))
145150
}
146151

147152
pub(crate) fn traits<'a>(&'a self) -> impl Iterator<Item = TraitId> + 'a {
@@ -386,20 +391,6 @@ pub enum ItemInNs {
386391
}
387392

388393
impl ItemInNs {
389-
fn match_with(self, per_ns: PerNs) -> Option<Visibility> {
390-
match self {
391-
ItemInNs::Types(def) => {
392-
per_ns.types.filter(|(other_def, _)| *other_def == def).map(|(_, vis)| vis)
393-
}
394-
ItemInNs::Values(def) => {
395-
per_ns.values.filter(|(other_def, _)| *other_def == def).map(|(_, vis)| vis)
396-
}
397-
ItemInNs::Macros(def) => {
398-
per_ns.macros.filter(|(other_def, _)| *other_def == def).map(|(_, vis)| vis)
399-
}
400-
}
401-
}
402-
403394
pub fn as_module_def_id(self) -> Option<ModuleDefId> {
404395
match self {
405396
ItemInNs::Types(id) | ItemInNs::Values(id) => Some(id),

0 commit comments

Comments
 (0)