From 7feb7383d23868c1ace7288b33e6029a9562c5e7 Mon Sep 17 00:00:00 2001 From: Roc Yu Date: Sun, 10 Apr 2022 19:17:38 -0400 Subject: [PATCH] rustdoc: Reduce allocations in a `theme` function --- src/librustdoc/theme.rs | 16 +++++++++------- 1 file 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::() .trim() - .replace('\n', " ") - .replace('/', "") - .replace('\t', " ") - .replace('{', "") - .replace('}', "") + .chars() + .filter_map(|c| match c { + '\n' | '\t' => Some(' '), + '/' | '{' | '}' => None, + c => Some(c), + }) + .collect::() .split(' ') .filter(|s| !s.is_empty()) - .collect::>() - .join(" "), + .intersperse(" ") + .collect::(), ) .unwrap_or_else(|_| String::new()) }