Skip to content

Commit

Permalink
Fix cargo bench.
Browse files Browse the repository at this point in the history
Benchmarking is only supported by Rust nightly and therefore before
this commit it was disabled by default and hidden behind `bench`
feature.  OTOH, declaring such feature in `Cargo.toml` would have
broken `cargo +stable test --all-features`.  Therefore the commit
switches from using a `bench` *feature* to using a `bench` config.

This commit also makes `use std:convert::TryInto` conditional via
`#[cfg(debug_assertions)]`.  This avoids the following build error:

    ```
    $ RUSTFLAGS="--cfg=bench" cargo +nightly bench --all-features
    ...
    error: unused import: `std::convert::TryInto`
     --> src/cast.rs:1:5
      |
    1 | use std::convert::TryInto;
      |     ^^^^^^^^^^^^^^^^^^^^^
    ```

Additionally, this commit also opts into the `test` feature (only if the
"bench" configurtaion is requested, because benchmarking is only available in
the nightly version of the compiler) and declares the `extern crate
test` dependency on the compiler-provided `test` crate. This avoids the
following build errors:

    ```
    $ RUSTFLAGS="--cfg=bench" cargo +nightly bench --all-features
    ...
    error[E0433]: failed to resolve: use of undeclared crate or module `test`
       --> src/optimize.rs:710:33
        |
    710 | fn bench_optimize(bencher: &mut test::Bencher) {
        |                                 ^^^^
                   use of undeclared crate or module `test`
    ```

    ```
    $ RUSTFLAGS="--cfg=bench" cargo +nightly bench --all-features
    ...
    error[E0658]: use of unstable library feature "test"
       --> src/optimize.rs:710:33
        |
    710 | fn bench_optimize(bencher: &mut test::Bencher) {
        |                                 ^^^^^^^^^^^^^
        |
        = note: see issue #50297
                <rust-lang/rust#50297>
                for more information
        = help: add `#![feature(test)]` to the crate attributes to enable
    ```

After these changes the following command line succeeds:

    ```
    $ RUSTFLAGS="--cfg=bench" cargo +nightly bench --all-features
    ...
    test bits::bench_find_min_version    ... bench:           1 ns/iter (+/- 0)
    test bits::bench_push_splitted_bytes ... bench:       3,862 ns/iter (+/- 58)
    test optimize::bench_optimize        ... bench:          19 ns/iter (+/- 0)
    ```
  • Loading branch information
anforowicz committed Apr 28, 2023
1 parent 3bb1dd7 commit c037ab9
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/bits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ fn test_push_number() {
);
}

#[cfg(feature = "bench")]
#[cfg(bench)]
#[bench]
fn bench_push_splitted_bytes(bencher: &mut test::Bencher) {
bencher.iter(|| {
Expand Down Expand Up @@ -1084,7 +1084,7 @@ mod encode_auto_tests {
}
}

#[cfg(feature = "bench")]
#[cfg(bench)]
#[bench]
fn bench_find_min_version(bencher: &mut test::Bencher) {
use test::black_box;
Expand Down
1 change: 1 addition & 0 deletions src/cast.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#[cfg(debug_assertions)]
use std::convert::TryInto;

// TODO remove this, use try_into wher as_* is used
Expand Down
9 changes: 9 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,19 @@
)]
#![cfg_attr(feature = "bench", doc(include = "../README.md"))]
// ^ make sure we can test our README.md.
// TODO: Avoid using `feature = "bench"` above.
#![cfg_attr(docsrs, feature(doc_cfg))]
// No `unsafe` please. If `unsafe` is really needed, then please
// consider encapsulating it in a separate crates.io crate.
#![forbid(unsafe_code)]
// Using `#[bench]`, `test::Bencher`, and `cargo bench` requires opting into the unstable `test`
// feature. See https://github.com/rust-lang/rust/issues/50297 for more details. Benchmarks
// features are only available in the nightly versions of the Rust compiler - to keep stable
// builds working we only enable benching behind the "bench" config. To run the benchmarks
// use: `RUSTFLAGS='--cfg=bench' cargo +nightly bench --all-features`
#![cfg_attr(bench, feature(test))]
#[cfg(bench)]
extern crate test;

// Re-exported dependencies.
#[cfg(feature = "bmp")]
Expand Down
2 changes: 1 addition & 1 deletion src/optimize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -705,7 +705,7 @@ mod optimize_tests {
}
}

#[cfg(feature = "bench")]
#[cfg(bench)]
#[bench]
fn bench_optimize(bencher: &mut test::Bencher) {
use crate::types::Version;
Expand Down

0 comments on commit c037ab9

Please sign in to comment.