forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move
test::Bencher
to a new (unstable) std::bench
module
This is a first step toward stabilizing it and the `#[bench]` attribute: rust-lang#66287 To avoid also moving much of the `test` crate `Bencher` is now only responsible for runnning user code and measuring the time it takes, not anymore for doing statistical analysis. This separation is based on `&mut dyn FnMut` callbacks. This introduces dynamic dispatch, which in general could affect performance characteristics of a program. However I expect benchmarking results not to be affected here since the {`Instant::new`; user code; `Instant::elapsed`} sequence is kept monomorphized and not crossing a dynamic dispatch boundary. This also adds a lifetime parameter to the `Bencher` struct, which is is technically a breaking change to an unstable type. I expect the impact to be low on existing users: only those with `#[deny(warnings)]` or `#[deny(elided_lifetimes_in_paths)]` would need to change their code. (See next commit.) This lint is `allow` by default.
- Loading branch information
1 parent
3fc30d8
commit 2018b69
Showing
7 changed files
with
81 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
//! Benchmarking | ||
#![unstable(feature = "test", issue = "50297")] | ||
|
||
use crate::hint::black_box; | ||
use crate::time::{Instant, Duration}; | ||
|
||
|
||
/// Manager of the benchmarking runs. | ||
/// | ||
/// This is fed into functions marked with `#[bench]` to allow for | ||
/// set-up & tear-down before running a piece of code repeatedly via a | ||
/// call to `iter`. | ||
#[allow(missing_debug_implementations)] | ||
pub struct Bencher<'a> { | ||
callback: Callback<'a>, | ||
/// FIXME | ||
pub bytes: u64, | ||
} | ||
|
||
impl<'a> Bencher<'a> { | ||
/// Callback for benchmark functions to run in their body. | ||
pub fn iter<T>(&mut self, mut f: impl FnMut() -> T) { | ||
(self.callback)(self.bytes, &mut |n_iterations| { | ||
let start = Instant::now(); | ||
for _ in 0..n_iterations { | ||
black_box(f()); | ||
} | ||
ns_from_dur(start.elapsed()) | ||
}) | ||
} | ||
} | ||
|
||
fn ns_from_dur(dur: Duration) -> u64 { | ||
dur.as_secs() * 1_000_000_000 + (dur.subsec_nanos() as u64) | ||
} | ||
|
||
// Permanently-unstable implementation details, only public for use by the `test` crate: | ||
|
||
/// n_iterations -> nanoseconds | ||
#[doc(hidden)] | ||
#[unstable(feature = "test_internals", issue = "0")] | ||
pub type TimeIterations<'i> = &'i mut dyn FnMut(u64) -> u64; | ||
|
||
#[doc(hidden)] | ||
#[unstable(feature = "test_internals", issue = "0")] | ||
pub type Callback<'a> = &'a mut dyn FnMut(/* bytes: */ u64, TimeIterations<'_>); | ||
|
||
#[doc(hidden)] | ||
#[unstable(feature = "test_internals", issue = "0")] | ||
impl<'a> Bencher<'a> { | ||
pub fn new(callback: Callback<'a>) -> Self { | ||
Self { callback, bytes: 0 } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters