Iterate over the smaller list

If there are two lists of different sizes,
iterating over the smaller list and then
looking up in the larger list is cheaper
than vice versa, because lookups scale
sublinearly.
This commit is contained in:
est31 2020-10-24 15:54:19 +02:00
parent 4d247ad7d3
commit a21c2eb121
2 changed files with 12 additions and 0 deletions

View File

@ -265,6 +265,10 @@ pub fn in_definition_order(&self) -> impl '_ + Iterator<Item = &ty::AssocItem> {
self.items.iter().map(|(_, v)| *v)
}
pub fn len(&self) -> usize {
self.items.len()
}
/// Returns an iterator over all associated items with the given name, ignoring hygiene.
pub fn filter_by_name_unhygienic(
&self,

View File

@ -22,6 +22,14 @@ fn impls_have_common_items(&self, impl1: DefId, impl2: DefId) -> bool {
let impl_items1 = self.tcx.associated_items(impl1);
let impl_items2 = self.tcx.associated_items(impl2);
let mut impl_items1 = &impl_items1;
let mut impl_items2 = &impl_items2;
// Performance optimization: iterate over the smaller list
if impl_items1.len() > impl_items2.len() {
std::mem::swap(&mut impl_items1, &mut impl_items2);
}
for item1 in impl_items1.in_definition_order() {
let collision = impl_items2.filter_by_name_unhygienic(item1.ident.name).any(|item2| {
// Symbols and namespace match, compare hygienically.