1
Fork 0

Rollup merge of #136816 - yotamofek:pr/notable-traits-button-cleanup, r=aDotInTheVoid

refactor `notable_traits_button` to use iterator combinators  instead of for loop

~Small cleanup.
Use `Iterator::any` instead of `for` loop with `predicate = true;`.
I think this makes the code more readable... and also has the additional benefit of short-circuiting the iterator when a notable trait is found (a `break` statement was missing in the `for` loop version, I think). Probably won't be significant enough to show on perf results, though.~

Three commits, each attempting to optimize `notable_trait_buttons` by a little bit.
This commit is contained in:
Jacob Pratt 2025-03-16 21:47:43 -04:00 committed by GitHub
commit 04032ab233
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -1465,8 +1465,6 @@ pub(crate) fn notable_traits_button(
ty: &clean::Type, ty: &clean::Type,
cx: &Context<'_>, cx: &Context<'_>,
) -> Option<impl fmt::Display> { ) -> Option<impl fmt::Display> {
let mut has_notable_trait = false;
if ty.is_unit() { if ty.is_unit() {
// Very common fast path. // Very common fast path.
return None; return None;
@ -1484,27 +1482,19 @@ pub(crate) fn notable_traits_button(
return None; return None;
} }
if let Some(impls) = cx.cache().impls.get(&did) { let impls = cx.cache().impls.get(&did)?;
for i in impls { let has_notable_trait = impls
let impl_ = i.inner_impl(); .iter()
if impl_.polarity != ty::ImplPolarity::Positive { .map(Impl::inner_impl)
continue; .filter(|impl_| {
} impl_.polarity == ty::ImplPolarity::Positive
if !ty.is_doc_subtype_of(&impl_.for_, cx.cache()) {
// Two different types might have the same did, // Two different types might have the same did,
// without actually being the same. // without actually being the same.
continue; && ty.is_doc_subtype_of(&impl_.for_, cx.cache())
} })
if let Some(trait_) = &impl_.trait_ { .filter_map(|impl_| impl_.trait_.as_ref())
let trait_did = trait_.def_id(); .filter_map(|trait_| cx.cache().traits.get(&trait_.def_id()))
.any(|t| t.is_notable_trait(cx.tcx()));
if cx.cache().traits.get(&trait_did).is_some_and(|t| t.is_notable_trait(cx.tcx())) {
has_notable_trait = true;
}
}
}
}
has_notable_trait.then(|| { has_notable_trait.then(|| {
cx.types_with_notable_traits.borrow_mut().insert(ty.clone()); cx.types_with_notable_traits.borrow_mut().insert(ty.clone());