Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid temporary allocations in render_assoc_item #82855

Merged
merged 1 commit into from
Mar 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/librustdoc/html/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,10 @@ impl Buffer {
crate fn is_for_html(&self) -> bool {
self.for_html
}

crate fn reserve(&mut self, additional: usize) {
self.buffer.reserve(additional)
}
}

/// Wrapper struct for properly emitting a function or method declaration.
Expand Down
44 changes: 26 additions & 18 deletions src/librustdoc/html/render/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2999,36 +2999,44 @@ fn render_assoc_item(
href(did, cx.cache()).map(|p| format!("{}#{}.{}", p.0, ty, name)).unwrap_or(anchor)
}
};
let mut header_len = format!(
"{}{}{}{}{}{:#}fn {}{:#}",
meth.visibility.print_with_space(cx.tcx(), meth.def_id, cx.cache()),
header.constness.print_with_space(),
header.asyncness.print_with_space(),
header.unsafety.print_with_space(),
print_default_space(meth.is_default()),
print_abi_with_space(header.abi),
name,
g.print(cx.cache())
)
.len();
let tcx = cx.tcx();
let vis = meth.visibility.print_with_space(tcx, meth.def_id, cx.cache()).to_string();
let constness = header.constness.print_with_space();
let asyncness = header.asyncness.print_with_space();
let unsafety = header.unsafety.print_with_space();
let defaultness = print_default_space(meth.is_default());
let abi = print_abi_with_space(header.abi).to_string();
// NOTE: `{:#}` does not print HTML formatting, `{}` does. So `g.print` can't be reused between the length calculation and `write!`.
let generics_len = format!("{:#}", g.print(cx.cache())).len();
let mut header_len = "fn ".len()
+ vis.len()
+ constness.len()
+ asyncness.len()
+ unsafety.len()
+ defaultness.len()
+ abi.len()
+ name.as_str().len()
+ generics_len;

let (indent, end_newline) = if parent == ItemType::Trait {
header_len += 4;
(4, false)
} else {
(0, true)
};
render_attributes(w, meth, false);
w.reserve(header_len + "<a href=\"\" class=\"fnname\">{".len() + "</a>".len());
write!(
w,
"{}{}{}{}{}{}{}fn <a href=\"{href}\" class=\"fnname\">{name}</a>\
{generics}{decl}{spotlight}{where_clause}",
if parent == ItemType::Trait { " " } else { "" },
meth.visibility.print_with_space(cx.tcx(), meth.def_id, cx.cache()),
header.constness.print_with_space(),
header.asyncness.print_with_space(),
header.unsafety.print_with_space(),
print_default_space(meth.is_default()),
print_abi_with_space(header.abi),
vis,
constness,
asyncness,
unsafety,
defaultness,
abi,
href = href,
name = name,
generics = g.print(cx.cache()),
Expand Down