Skip to content

Commit cdb45c8

Browse files
committed
Auto merge of #145851 - lolbinarycat:rustdoc-optimize, r=GuillaumeGomez
rustdoc: a few micro-optimizations targeted at build_impl Unsure if these will be anything substantial, but the first one at least should git rid of quite a few branches, second one unsure if it's worth it. r? `@GuillaumeGomez`
2 parents d829133 + f16d1fc commit cdb45c8

File tree

2 files changed

+24
-19
lines changed

2 files changed

+24
-19
lines changed

compiler/rustc_resolve/src/rustdoc.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,8 +207,10 @@ pub fn attrs_to_doc_fragments<'a, A: AttributeExt + Clone + 'a>(
207207
attrs: impl Iterator<Item = (&'a A, Option<DefId>)>,
208208
doc_only: bool,
209209
) -> (Vec<DocFragment>, ThinVec<A>) {
210-
let mut doc_fragments = Vec::new();
211-
let mut other_attrs = ThinVec::<A>::new();
210+
let (min_size, max_size) = attrs.size_hint();
211+
let size_hint = max_size.unwrap_or(min_size);
212+
let mut doc_fragments = Vec::with_capacity(size_hint);
213+
let mut other_attrs = ThinVec::<A>::with_capacity(if doc_only { 0 } else { size_hint });
212214
for (attr, item_id) in attrs {
213215
if let Some((doc_str, comment_kind)) = attr.doc_str_and_comment_kind() {
214216
let doc = beautify_doc_string(doc_str, comment_kind);
@@ -230,6 +232,9 @@ pub fn attrs_to_doc_fragments<'a, A: AttributeExt + Clone + 'a>(
230232
}
231233
}
232234

235+
doc_fragments.shrink_to_fit();
236+
other_attrs.shrink_to_fit();
237+
233238
unindent_doc_fragments(&mut doc_fragments);
234239

235240
(doc_fragments, other_attrs)

src/librustdoc/clean/inline.rs

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -572,30 +572,30 @@ pub(crate) fn build_impl(
572572
super::build_deref_target_impls(cx, &trait_items, ret);
573573
}
574574

575-
// Return if the trait itself or any types of the generic parameters are doc(hidden).
576-
let mut stack: Vec<&Type> = vec![&for_];
575+
if !document_hidden {
576+
// Return if the trait itself or any types of the generic parameters are doc(hidden).
577+
let mut stack: Vec<&Type> = vec![&for_];
577578

578-
if let Some(did) = trait_.as_ref().map(|t| t.def_id())
579-
&& !document_hidden
580-
&& tcx.is_doc_hidden(did)
581-
{
582-
return;
583-
}
584-
585-
if let Some(generics) = trait_.as_ref().and_then(|t| t.generics()) {
586-
stack.extend(generics);
587-
}
588-
589-
while let Some(ty) = stack.pop() {
590-
if let Some(did) = ty.def_id(&cx.cache)
591-
&& !document_hidden
579+
if let Some(did) = trait_.as_ref().map(|t| t.def_id())
592580
&& tcx.is_doc_hidden(did)
593581
{
594582
return;
595583
}
596-
if let Some(generics) = ty.generics() {
584+
585+
if let Some(generics) = trait_.as_ref().and_then(|t| t.generics()) {
597586
stack.extend(generics);
598587
}
588+
589+
while let Some(ty) = stack.pop() {
590+
if let Some(did) = ty.def_id(&cx.cache)
591+
&& tcx.is_doc_hidden(did)
592+
{
593+
return;
594+
}
595+
if let Some(generics) = ty.generics() {
596+
stack.extend(generics);
597+
}
598+
}
599599
}
600600

601601
if let Some(did) = trait_.as_ref().map(|t| t.def_id()) {

0 commit comments

Comments
 (0)