Skip to content

Commit 2836143

Browse files
authored
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.
2 parents 3ff5cb2 + 7feb738 commit 2836143

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

src/librustdoc/theme.rs

+9-7
Original file line numberDiff line numberDiff line change
@@ -173,15 +173,17 @@ fn build_rule(v: &[u8], positions: &[usize]) -> String {
173173
.map(|x| ::std::str::from_utf8(&v[x[0]..x[1]]).unwrap_or(""))
174174
.collect::<String>()
175175
.trim()
176-
.replace('\n', " ")
177-
.replace('/', "")
178-
.replace('\t', " ")
179-
.replace('{', "")
180-
.replace('}', "")
176+
.chars()
177+
.filter_map(|c| match c {
178+
'\n' | '\t' => Some(' '),
179+
'/' | '{' | '}' => None,
180+
c => Some(c),
181+
})
182+
.collect::<String>()
181183
.split(' ')
182184
.filter(|s| !s.is_empty())
183-
.collect::<Vec<&str>>()
184-
.join(" "),
185+
.intersperse(" ")
186+
.collect::<String>(),
185187
)
186188
.unwrap_or_else(|_| String::new())
187189
}

0 commit comments

Comments
 (0)