-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Wrong benchmarks in library/core/benches/char/methods.rs and possibly other places #107590
Comments
@rustbot claim |
Author of https://gendignoux.com/blog/2022/01/31/rust-benchmarks.html here. Apologies for not having reported a bug here as I didn't have the bandwidth to follow-up - but I'm glad that someone else picked it up! Thanks @safinaskar and @chenyukang 🙏 Beyond this specific example, I think one broader problem (as I mentioned in my conclusion) is that the "box" name misleadingly implies that it "contains" a computation put into it, i.e. that in the following example (close to the Bencher API) the function loop {
black_box(f());
} In reality, the "box" is rather a barrier/fence, and only "contains" a value (the result of evaluating loop {
let x = f();
black_box(x);
} or even: let x = f();
loop {
black_box(x);
} In fact, if the compiler is perfect, we want it to optimize the computation of Back to the impl Bencher {
/// Callback for benchmark functions to run in their body.
pub fn iter<I, O, F>(&mut self, input: I, mut inner: F)
where
I: Copy,
F: FnMut(I) -> O,
{
// Omitting the measurements, statistics, etc.
loop {
black_box(f(black_box(input)))
}
} On the other hand, the current I don't really have the bandwidth to propose an RFC at the moment, but leaving my thoughts here in case someone wants to pick up this idea. |
…r=thomcc Fix benchmarks in library/core with black_box Fixes rust-lang#107590
@chenyukang , @gendx . The fix is completely wrong. Please, reopen the bug. First, the patch doesn't fix all bugs. For example, the article also mentions file library/core/benches/num/dec2flt/mod.rs , it is not fixed. So, please review all benches in whole repo. Second, patched files are patched wrong. You need to pass inputs through
; prevents result of computation from passing to Bencher::iter . So ; should be removed:
b.iter(|| {
- format_shortest(black_box(&decoded), &mut buf);
+ format_shortest(black_box(&decoded), &mut buf)
}); Also, I think some lint should be added to clippy, which will prevent |
Thanks for correcting! |
Not all bench mark test should return a value in the block, the change: b.iter(|| {
- format_shortest(black_box(&decoded), &mut buf);
+ format_shortest(black_box(&decoded), &mut buf)
}); will be rejected by compiler: --> library/core/benches/num/flt2dec/strategy/grisu.rs:18:9
|
16 | let mut buf = [MaybeUninit::new(0); MAX_SIG_DIGITS];
| ------- variable defined here
17 | b.iter(|| {
| - inferred to be a `FnMut` closure
18 | format_shortest(black_box(&decoded), &mut buf)
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^---^
| | |
| | variable captured here
| returns a reference to a captured variable which escapes the closure body
|
= note: `FnMut` closures only have access to their captured variables while they are executing...
= note: ...therefore, they cannot allow references to captured variables to escape And this test will write the result to Another common scenario is we loop in the block of b.iter(|| {
for i in 1..Nums {
...blah
}
}); Anyway, benchmark testing do need some careful tweaks considering |
@chenyukang , I propose wrap result in black_box in both cases |
…r=workingjubilee Fix more benchmark test with black_box Follow up fix for rust-lang#107590
…jubilee Fix more benchmark test with black_box Follow up fix for rust-lang/rust#107590
…jubilee Fix more benchmark test with black_box Follow up fix for rust-lang/rust#107590
As I learned from https://gendignoux.com/blog/2022/01/31/rust-benchmarks.html#application-to-real-benchmarks file
library/core/benches/char/methods.rs
and possibly other files in this repo contain wrong benchmarks.As author of the article points, call
c.to_digit(2)
optimizesto Noneand thus doesn't get benchmarked.So, please, find all such places and fix them. The faulty example still present in latest master
The text was updated successfully, but these errors were encountered: