about summary refs log tree commit diff
diff options
context:
space:
mode:
authorMatthias Krüger <matthias.krueger@famsik.de>2022-04-12 08:46:58 +0200
committerGitHub <noreply@github.com>2022-04-12 08:46:58 +0200
commit2836143380af7d41efb59d95d44d97b417dad2b4 (patch)
tree32143bcc4ee6a7f5a1c88c7404f323c179c3d68b
parent3ff5cb20b6e8b5c4b4a8fe5dbd07d5d66ffbcd43 (diff)
parent7feb7383d23868c1ace7288b33e6029a9562c5e7 (diff)
downloadrust-2836143380af7d41efb59d95d44d97b417dad2b4.tar.gz
rust-2836143380af7d41efb59d95d44d97b417dad2b4.zip
Rollup merge of #95909 - vacuus:theme-build-rule, r=GuillaumeGomez
rustdoc: Reduce allocations in a `theme` function

`str::replace` allocates a new `String`...

This could probably be made more efficient, but I think it'd have to be clunky imperative code to achieve that.
-rw-r--r--src/librustdoc/theme.rs16
1 files changed, 9 insertions, 7 deletions
diff --git a/src/librustdoc/theme.rs b/src/librustdoc/theme.rs
index 1e9a65e1d2f..7c19865b6d7 100644
--- a/src/librustdoc/theme.rs
+++ b/src/librustdoc/theme.rs
@@ -173,15 +173,17 @@ fn build_rule(v: &[u8], positions: &[usize]) -> String {
             .map(|x| ::std::str::from_utf8(&v[x[0]..x[1]]).unwrap_or(""))
             .collect::<String>()
             .trim()
-            .replace('\n', " ")
-            .replace('/', "")
-            .replace('\t', " ")
-            .replace('{', "")
-            .replace('}', "")
+            .chars()
+            .filter_map(|c| match c {
+                '\n' | '\t' => Some(' '),
+                '/' | '{' | '}' => None,
+                c => Some(c),
+            })
+            .collect::<String>()
             .split(' ')
             .filter(|s| !s.is_empty())
-            .collect::<Vec<&str>>()
-            .join(" "),
+            .intersperse(" ")
+            .collect::<String>(),
     )
     .unwrap_or_else(|_| String::new())
 }