Skip to content

Commit 1b8741c

Browse files
committed
Avoid allocating when rendering some sidebar sections
1 parent 7885ce5 commit 1b8741c

File tree

1 file changed

+76
-88
lines changed
  • src/librustdoc/html/render

1 file changed

+76
-88
lines changed

src/librustdoc/html/render/mod.rs

Lines changed: 76 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -4515,82 +4515,74 @@ fn extract_for_impl_name(item: &clean::Item, cache: &Cache) -> Option<(String, S
45154515
}
45164516

45174517
fn sidebar_trait(cx: &Context<'_>, buf: &mut Buffer, it: &clean::Item, t: &clean::Trait) {
4518-
let mut sidebar = String::new();
4518+
write!(buf, "<div class=\"block items\">");
4519+
4520+
fn print_sidebar_section(
4521+
out: &mut Buffer,
4522+
items: &[clean::Item],
4523+
before: &str,
4524+
filter: impl Fn(&clean::Item) -> bool,
4525+
write: impl Fn(&mut Buffer, &Symbol),
4526+
after: &str,
4527+
) {
4528+
let mut items = items
4529+
.iter()
4530+
.filter_map(|m| match m.name {
4531+
Some(ref name) if filter(m) => Some(name),
4532+
_ => None,
4533+
})
4534+
.collect::<Vec<_>>();
45194535

4520-
let mut types = t
4521-
.items
4522-
.iter()
4523-
.filter_map(|m| match m.name {
4524-
Some(ref name) if m.is_associated_type() => {
4525-
Some(format!("<a href=\"#associatedtype.{name}\">{name}</a>", name = name))
4526-
}
4527-
_ => None,
4528-
})
4529-
.collect::<Vec<_>>();
4530-
let mut consts = t
4531-
.items
4532-
.iter()
4533-
.filter_map(|m| match m.name {
4534-
Some(ref name) if m.is_associated_const() => {
4535-
Some(format!("<a href=\"#associatedconstant.{name}\">{name}</a>", name = name))
4536-
}
4537-
_ => None,
4538-
})
4539-
.collect::<Vec<_>>();
4540-
let mut required = t
4541-
.items
4542-
.iter()
4543-
.filter_map(|m| match m.name {
4544-
Some(ref name) if m.is_ty_method() => {
4545-
Some(format!("<a href=\"#tymethod.{name}\">{name}</a>", name = name))
4546-
}
4547-
_ => None,
4548-
})
4549-
.collect::<Vec<String>>();
4550-
let mut provided = t
4551-
.items
4552-
.iter()
4553-
.filter_map(|m| match m.name {
4554-
Some(ref name) if m.is_method() => {
4555-
Some(format!("<a href=\"#method.{0}\">{0}</a>", name))
4536+
if !items.is_empty() {
4537+
items.sort();
4538+
out.push_str(before);
4539+
for item in items.into_iter() {
4540+
write(out, item);
45564541
}
4557-
_ => None,
4558-
})
4559-
.collect::<Vec<String>>();
4560-
4561-
if !types.is_empty() {
4562-
types.sort();
4563-
sidebar.push_str(&format!(
4564-
"<a class=\"sidebar-title\" href=\"#associated-types\">\
4565-
Associated Types</a><div class=\"sidebar-links\">{}</div>",
4566-
types.join("")
4567-
));
4568-
}
4569-
if !consts.is_empty() {
4570-
consts.sort();
4571-
sidebar.push_str(&format!(
4572-
"<a class=\"sidebar-title\" href=\"#associated-const\">\
4573-
Associated Constants</a><div class=\"sidebar-links\">{}</div>",
4574-
consts.join("")
4575-
));
4576-
}
4577-
if !required.is_empty() {
4578-
required.sort();
4579-
sidebar.push_str(&format!(
4580-
"<a class=\"sidebar-title\" href=\"#required-methods\">\
4581-
Required Methods</a><div class=\"sidebar-links\">{}</div>",
4582-
required.join("")
4583-
));
4584-
}
4585-
if !provided.is_empty() {
4586-
provided.sort();
4587-
sidebar.push_str(&format!(
4588-
"<a class=\"sidebar-title\" href=\"#provided-methods\">\
4589-
Provided Methods</a><div class=\"sidebar-links\">{}</div>",
4590-
provided.join("")
4591-
));
4542+
out.push_str(after);
4543+
}
45924544
}
45934545

4546+
print_sidebar_section(
4547+
buf,
4548+
&t.items,
4549+
"<a class=\"sidebar-title\" href=\"#associated-types\">\
4550+
Associated Types</a><div class=\"sidebar-links\">",
4551+
|m| m.is_associated_type(),
4552+
|out, sym| write!(out, "<a href=\"#associatedtype.{0}\">{0}</a>", sym),
4553+
"</div>",
4554+
);
4555+
4556+
print_sidebar_section(
4557+
buf,
4558+
&t.items,
4559+
"<a class=\"sidebar-title\" href=\"#associated-const\">\
4560+
Associated Constants</a><div class=\"sidebar-links\">",
4561+
|m| m.is_associated_const(),
4562+
|out, sym| write!(out, "<a href=\"#associatedconstant.{0}\">{0}</a>", sym),
4563+
"</div>",
4564+
);
4565+
4566+
print_sidebar_section(
4567+
buf,
4568+
&t.items,
4569+
"<a class=\"sidebar-title\" href=\"#required-methods\">\
4570+
Required Methods</a><div class=\"sidebar-links\">",
4571+
|m| m.is_ty_method(),
4572+
|out, sym| write!(out, "<a href=\"#tymethod.{0}\">{0}</a>", sym),
4573+
"</div>",
4574+
);
4575+
4576+
print_sidebar_section(
4577+
buf,
4578+
&t.items,
4579+
"<a class=\"sidebar-title\" href=\"#provided-methods\">\
4580+
Provided Methods</a><div class=\"sidebar-links\">",
4581+
|m| m.is_method(),
4582+
|out, sym| write!(out, "<a href=\"#method.{0}\">{0}</a>", sym),
4583+
"</div>",
4584+
);
4585+
45944586
if let Some(implementors) = cx.cache.implementors.get(&it.def_id) {
45954587
let mut res = implementors
45964588
.iter()
@@ -4605,29 +4597,29 @@ fn sidebar_trait(cx: &Context<'_>, buf: &mut Buffer, it: &clean::Item, t: &clean
46054597

46064598
if !res.is_empty() {
46074599
res.sort();
4608-
sidebar.push_str(&format!(
4600+
buf.push_str(
46094601
"<a class=\"sidebar-title\" href=\"#foreign-impls\">\
46104602
Implementations on Foreign Types</a>\
4611-
<div class=\"sidebar-links\">{}</div>",
4612-
res.into_iter()
4613-
.map(|(name, id)| format!("<a href=\"#{}\">{}</a>", id, Escape(&name)))
4614-
.collect::<Vec<_>>()
4615-
.join("")
4616-
));
4603+
<div class=\"sidebar-links\">",
4604+
);
4605+
for (name, id) in res.into_iter() {
4606+
buf.push_str(&format!("<a href=\"#{}\">{}</a>", id, Escape(&name)));
4607+
}
4608+
buf.push_str("</div>");
46174609
}
46184610
}
46194611

4620-
sidebar.push_str(&sidebar_assoc_items(cx, it));
4612+
buf.push_str(&sidebar_assoc_items(cx, it));
46214613

4622-
sidebar.push_str("<a class=\"sidebar-title\" href=\"#implementors\">Implementors</a>");
4614+
buf.push_str("<a class=\"sidebar-title\" href=\"#implementors\">Implementors</a>");
46234615
if t.is_auto {
4624-
sidebar.push_str(
4616+
buf.push_str(
46254617
"<a class=\"sidebar-title\" \
46264618
href=\"#synthetic-implementors\">Auto Implementors</a>",
46274619
);
46284620
}
46294621

4630-
write!(buf, "<div class=\"block items\">{}</div>", sidebar)
4622+
write!(buf, "</div>")
46314623
}
46324624

46334625
fn sidebar_primitive(cx: &Context<'_>, buf: &mut Buffer, it: &clean::Item) {
@@ -4743,11 +4735,7 @@ fn sidebar_module(buf: &mut Buffer, items: &[clean::Item]) {
47434735
if items.iter().any(|it| {
47444736
it.type_() == ItemType::ExternCrate || (it.type_() == ItemType::Import && !it.is_stripped())
47454737
}) {
4746-
sidebar.push_str(&format!(
4747-
"<li><a href=\"#{id}\">{name}</a></li>",
4748-
id = "reexports",
4749-
name = "Re-exports"
4750-
));
4738+
sidebar.push_str("<li><a href=\"#reexports\">Re-exports</a></li>");
47514739
}
47524740

47534741
// ordering taken from item_module, reorder, where it prioritized elements in a certain order

0 commit comments

Comments
 (0)