-
Notifications
You must be signed in to change notification settings - Fork 12.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
rustdoc: Reduce allocations in a theme
function
#95909
Conversation
(rust-highfive has picked a reviewer for you, use r? to override) |
I'm really not sure it'll change anything considering how rare this code is run. However, could you benchmark this locally and show us the results please? Running a perf check on this wouldn't show anything unfortunately... |
For what it's worth, Godbolt reports fewer assembly language instructions for processing the string when compiled with |
You have a book on how to run benchmarks in rust here. So pick whichever you prefer. ;) As for what do compare: take both codes (before and after your change) and run them on a string similar to the rustdoc CSS files (or on the rustdoc CSS files directly, doesn't matter). Once done, please provide the results alongside the source code for the benchmark as well. |
I did the benchmarks very crudely by running
use criterion::{black_box, criterion_group, criterion_main, Criterion};
pub fn criterion_benchmark(c: &mut Criterion) {
// don't mind the naming of the benchmarks
let mut group = c.benchmark_group("dark theme");
// the string literal being the text of the file
group.bench_function("new", |b| b.iter(|| new(black_box([string literal].to_string()))));
group.finish();
}
fn old(s: String) -> String {
s
.trim()
.replace('\n', " ")
.replace('\t', " ")
.replace('/', "")
.replace('{', "")
.replace('}', "")
.split(' ')
.filter(|s| !s.is_empty())
.collect::<Vec<&str>>()
.join(" ")
}
fn new(s: String) -> String {
s
.trim()
.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(" ")
}
criterion_group!(benches, criterion_benchmark);
criterion_main!(benches); *I tried:
|
In any case, thanks for the results, it's really great! I'll put it in the rollup since it's in a code location where the perf change won't have any impact on the results. @bors: r+ rollup |
📌 Commit 7feb738 has been approved by |
…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.
…askrgr Rollup of 7 pull requests Successful merges: - rust-lang#95320 (Document the current MIR semantics that are clear from existing code) - rust-lang#95722 (pre-push.sh: Use python3 if python is not found) - rust-lang#95881 (Use `to_string` instead of `format!`) - rust-lang#95909 (rustdoc: Reduce allocations in a `theme` function) - rust-lang#95910 (Fix crate_type attribute to not warn on duplicates) - rust-lang#95920 (use `Span::find_ancestor_inside` to get right span in CastCheck) - rust-lang#95936 (Fix a bad error message for `relative paths are not supported in visibilities` error) Failed merges: r? `@ghost` `@rustbot` modify labels: rollup
str::replace
allocates a newString
...This could probably be made more efficient, but I think it'd have to be clunky imperative code to achieve that.