about summary refs log tree commit diff
path: root/compiler/rustc_resolve/src/rustdoc.rs
diff options
context:
space:
mode:
authorMichael Howell <michael@notriddle.com>2023-02-15 14:44:31 -0700
committerMichael Howell <michael@notriddle.com>2023-02-22 21:00:10 -0700
commit49d995a4cf7c4355aa846fa039b7af656ffa287d (patch)
tree5d1e3a819485cd244be1333d18cd6c3a3b5e7810 /compiler/rustc_resolve/src/rustdoc.rs
parent0978711950b77582e4f8f334f6e9848d48ab7790 (diff)
downloadrust-49d995a4cf7c4355aa846fa039b7af656ffa287d.tar.gz
rust-49d995a4cf7c4355aa846fa039b7af656ffa287d.zip
rustdoc: reduce allocations when generating tooltips
An attempt to reduce the perf regression in
https://github.com/rust-lang/rust/pull/108052#issuecomment-1430631861
Diffstat (limited to 'compiler/rustc_resolve/src/rustdoc.rs')
-rw-r--r--compiler/rustc_resolve/src/rustdoc.rs16
1 files changed, 10 insertions, 6 deletions
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<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();