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>
This commit is contained in:
bors[bot] 2021-11-10 15:12:05 +00:00 committed by GitHub
commit f724c84e7d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -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),