Skip to content

Commit

Permalink
Rollup merge of rust-lang#95909 - vacuus:theme-build-rule, r=Guillaum…
Browse files Browse the repository at this point in the history
…eGomez

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.
  • Loading branch information
Dylan-DPC authored Apr 12, 2022
2 parents e537f32 + 7feb738 commit 2f84747
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions src/librustdoc/theme.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
Expand Down

0 comments on commit 2f84747

Please sign in to comment.