Skip to content

Commit 1e6d849

Browse files
committed
Avoid temporary allocations in render_assoc_item
`render_assoc_item` came up as very hot in a profile of rustdoc on `bevy`. This avoids some temporary allocations just to calculate the length of the header. This should be a strict improvement, since all string formatting was done twice before.
1 parent edeee91 commit 1e6d849

File tree

2 files changed

+30
-18
lines changed

2 files changed

+30
-18
lines changed

src/librustdoc/html/format.rs

+4
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,10 @@ impl Buffer {
105105
crate fn is_for_html(&self) -> bool {
106106
self.for_html
107107
}
108+
109+
crate fn reserve(&mut self, additional: usize) {
110+
self.buffer.reserve(additional)
111+
}
108112
}
109113

110114
/// Wrapper struct for properly emitting a function or method declaration.

src/librustdoc/html/render/mod.rs

+26-18
Original file line numberDiff line numberDiff line change
@@ -2999,36 +2999,44 @@ fn render_assoc_item(
29992999
href(did, cx.cache()).map(|p| format!("{}#{}.{}", p.0, ty, name)).unwrap_or(anchor)
30003000
}
30013001
};
3002-
let mut header_len = format!(
3003-
"{}{}{}{}{}{:#}fn {}{:#}",
3004-
meth.visibility.print_with_space(cx.tcx(), meth.def_id, cx.cache()),
3005-
header.constness.print_with_space(),
3006-
header.asyncness.print_with_space(),
3007-
header.unsafety.print_with_space(),
3008-
print_default_space(meth.is_default()),
3009-
print_abi_with_space(header.abi),
3010-
name,
3011-
g.print(cx.cache())
3012-
)
3013-
.len();
3002+
let tcx = cx.tcx();
3003+
let vis = meth.visibility.print_with_space(tcx, meth.def_id, cx.cache()).to_string();
3004+
let constness = header.constness.print_with_space();
3005+
let asyncness = header.asyncness.print_with_space();
3006+
let unsafety = header.unsafety.print_with_space();
3007+
let defaultness = print_default_space(meth.is_default());
3008+
let abi = print_abi_with_space(header.abi).to_string();
3009+
// NOTE: `{:#}` does not print HTML formatting, `{}` does. So `g.print` can't be reused between the length calculation and `write!`.
3010+
let generics_len = format!("{:#}", g.print(cx.cache())).len();
3011+
let mut header_len = "fn ".len()
3012+
+ vis.len()
3013+
+ constness.len()
3014+
+ asyncness.len()
3015+
+ unsafety.len()
3016+
+ defaultness.len()
3017+
+ abi.len()
3018+
+ name.as_str().len()
3019+
+ generics_len;
3020+
30143021
let (indent, end_newline) = if parent == ItemType::Trait {
30153022
header_len += 4;
30163023
(4, false)
30173024
} else {
30183025
(0, true)
30193026
};
30203027
render_attributes(w, meth, false);
3028+
w.reserve(header_len + "<a href=\"\" class=\"fnname\">{".len() + "</a>".len());
30213029
write!(
30223030
w,
30233031
"{}{}{}{}{}{}{}fn <a href=\"{href}\" class=\"fnname\">{name}</a>\
30243032
{generics}{decl}{spotlight}{where_clause}",
30253033
if parent == ItemType::Trait { " " } else { "" },
3026-
meth.visibility.print_with_space(cx.tcx(), meth.def_id, cx.cache()),
3027-
header.constness.print_with_space(),
3028-
header.asyncness.print_with_space(),
3029-
header.unsafety.print_with_space(),
3030-
print_default_space(meth.is_default()),
3031-
print_abi_with_space(header.abi),
3034+
vis,
3035+
constness,
3036+
asyncness,
3037+
unsafety,
3038+
defaultness,
3039+
abi,
30323040
href = href,
30333041
name = name,
30343042
generics = g.print(cx.cache()),

0 commit comments

Comments
 (0)