From 49d995a4cf7c4355aa846fa039b7af656ffa287d Mon Sep 17 00:00:00 2001 From: Michael Howell Date: Wed, 15 Feb 2023 14:44:31 -0700 Subject: rustdoc: reduce allocations when generating tooltips An attempt to reduce the perf regression in https://github.com/rust-lang/rust/pull/108052#issuecomment-1430631861 --- compiler/rustc_resolve/src/rustdoc.rs | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) (limited to 'compiler/rustc_resolve/src') diff --git a/compiler/rustc_resolve/src/rustdoc.rs b/compiler/rustc_resolve/src/rustdoc.rs index 5e4b66018e4..b8853c1744c 100644 --- a/compiler/rustc_resolve/src/rustdoc.rs +++ b/compiler/rustc_resolve/src/rustdoc.rs @@ -265,9 +265,9 @@ fn strip_generics_from_path_segment(segment: Vec) -> Result Result { +pub fn strip_generics_from_path(path_str: &str) -> Result, 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 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 { 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 { +pub(crate) fn attrs_to_preprocessed_links(attrs: &[ast::Attribute]) -> Vec> { 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(); -- cgit 1.4.1-3-g733a5