rustdoc: reduce allocations when generating tooltips

An attempt to reduce the perf regression in
https://github.com/rust-lang/rust/pull/108052#issuecomment-1430631861
This commit is contained in:
Michael Howell 2023-02-15 14:44:31 -07:00
parent 0978711950
commit 49d995a4cf
7 changed files with 40 additions and 29 deletions

View file

@ -265,9 +265,9 @@ fn strip_generics_from_path_segment(segment: Vec<char>) -> Result<String, Malfor
}
}
pub fn strip_generics_from_path(path_str: &str) -> Result<String, MalformedGenerics> {
pub fn strip_generics_from_path(path_str: &str) -> Result<Box<str>, MalformedGenerics> {
if !path_str.contains(['<', '>']) {
return Ok(path_str.to_string());
return Ok(path_str.into());
}
let mut stripped_segments = vec![];
let mut path = path_str.chars().peekable();
@ -322,7 +322,11 @@ pub fn strip_generics_from_path(path_str: &str) -> Result<String, MalformedGener
let stripped_path = stripped_segments.join("::");
if !stripped_path.is_empty() { Ok(stripped_path) } else { Err(MalformedGenerics::MissingType) }
if !stripped_path.is_empty() {
Ok(stripped_path.into())
} else {
Err(MalformedGenerics::MissingType)
}
}
/// Returns whether the first doc-comment is an inner attribute.
@ -336,7 +340,7 @@ pub fn inner_docs(attrs: &[ast::Attribute]) -> bool {
/// Simplified version of the corresponding function in rustdoc.
/// If the rustdoc version returns a successful result, this function must return the same result.
/// Otherwise this function may return anything.
fn preprocess_link(link: &str) -> String {
fn preprocess_link(link: &str) -> Box<str> {
let link = link.replace('`', "");
let link = link.split('#').next().unwrap();
let link = link.trim();
@ -345,7 +349,7 @@ fn preprocess_link(link: &str) -> String {
let link = link.strip_suffix("{}").unwrap_or(link);
let link = link.strip_suffix("[]").unwrap_or(link);
let link = if link != "!" { link.strip_suffix('!').unwrap_or(link) } else { link };
strip_generics_from_path(link).unwrap_or_else(|_| link.to_string())
strip_generics_from_path(link).unwrap_or_else(|_| link.into())
}
/// Keep inline and reference links `[]`,
@ -365,7 +369,7 @@ pub fn may_be_doc_link(link_type: LinkType) -> bool {
/// Simplified version of `preprocessed_markdown_links` from rustdoc.
/// Must return at least the same links as it, but may add some more links on top of that.
pub(crate) fn attrs_to_preprocessed_links(attrs: &[ast::Attribute]) -> Vec<String> {
pub(crate) fn attrs_to_preprocessed_links(attrs: &[ast::Attribute]) -> Vec<Box<str>> {
let (doc_fragments, _) = attrs_to_doc_fragments(attrs.iter().map(|attr| (attr, None)), true);
let doc = prepare_to_doc_link_resolution(&doc_fragments).into_values().next().unwrap();