Create configuration conditional bench

Currently we are unable to build with all features enabled with a
non-nightly toolchain, this is because of the use of

    `#![cfg_attr(all(test, feature = "unstable"), feature(test))]`

which causes the following error when building:

 error[E0554]: `#![feature]` may not be used on the stable release
 channel

The "unstable" feature is used to guard bench mark modules, this is
widely suggested online but there is a better way.

When running the bench marks use the following incantation:

    `RUSTFLAGS='--cfg=bench' cargo bench`

This creates a configuration conditional "bench" that can be used to
guard the bench mark modules.

    #[cfg(bench)]
    mod benches {
        ...
    }
This commit is contained in:
Tobin C. Harding 2022-07-12 10:07:38 +10:00
parent f60c92ca58
commit f3b2120ec9
7 changed files with 17 additions and 13 deletions

View File

@ -15,7 +15,6 @@ edition = "2018"
# Please don't forget to add relevant features to docs.rs below # Please don't forget to add relevant features to docs.rs below
[features] [features]
default = [ "std", "secp-recovery" ] default = [ "std", "secp-recovery" ]
unstable = []
rand = ["secp256k1/rand-std"] rand = ["secp256k1/rand-std"]
serde = ["actual-serde", "bitcoin_hashes/serde", "secp256k1/serde"] serde = ["actual-serde", "bitcoin_hashes/serde", "secp256k1/serde"]
secp-lowmemory = ["secp256k1/lowmemory"] secp-lowmemory = ["secp256k1/lowmemory"]

View File

@ -110,6 +110,11 @@ shell alias to check your documentation changes build correctly.
alias build-docs='RUSTDOCFLAGS="--cfg docsrs" cargo +nightly rustdoc --features="$FEATURES" -- -D rustdoc::broken-intra-doc-links' alias build-docs='RUSTDOCFLAGS="--cfg docsrs" cargo +nightly rustdoc --features="$FEATURES" -- -D rustdoc::broken-intra-doc-links'
``` ```
### Running benchmarks
We use a custom Rust compiler configuration conditional to guard the bench mark code. To run the
bench marks use: `RUSTFLAGS='--cfg=bench' cargo +nightly bench`.
## Pull Requests ## Pull Requests
Every PR needs at least two reviews to get merged. During the review phase Every PR needs at least two reviews to get merged. During the review phase

View File

@ -77,7 +77,7 @@ then
) )
fi fi
# Bench if told to # Bench if told to, only works with non-stable toolchain (nightly, beta).
if [ "$DO_BENCH" = true ] if [ "$DO_BENCH" = true ]
then then
if [ "NIGHTLY" = false ] if [ "NIGHTLY" = false ]
@ -90,7 +90,7 @@ then
fi fi
exit 1 exit 1
fi fi
cargo bench --features unstable RUSTFLAGS='--cfg=bench' cargo bench
fi fi
# Use as dependency if told to # Use as dependency if told to

View File

@ -528,7 +528,7 @@ mod tests {
} }
} }
#[cfg(all(test, feature = "unstable"))] #[cfg(bench)]
mod benches { mod benches {
use super::Block; use super::Block;
use crate::EmptyWrite; use crate::EmptyWrite;

View File

@ -1724,7 +1724,7 @@ mod tests {
} }
} }
#[cfg(all(test, feature = "unstable"))] #[cfg(bench)]
mod benches { mod benches {
use super::Transaction; use super::Transaction;
use crate::EmptyWrite; use crate::EmptyWrite;

View File

@ -441,7 +441,7 @@ mod test {
} }
#[cfg(all(test, feature = "unstable"))] #[cfg(bench)]
mod benches { mod benches {
use test::{Bencher, black_box}; use test::{Bencher, black_box};
use super::Witness; use super::Witness;

View File

@ -19,7 +19,6 @@
//! * `std` - the usual dependency on `std` (default). //! * `std` - the usual dependency on `std` (default).
//! * `secp-recovery` - enables calculating public key from a signature and message. //! * `secp-recovery` - enables calculating public key from a signature and message.
//! * `base64` - (dependency), enables encoding of PSBTs and message signatures. //! * `base64` - (dependency), enables encoding of PSBTs and message signatures.
//! * `unstable` - enables unstable features for testing.
//! * `rand` - (dependency), makes it more convenient to generate random values. //! * `rand` - (dependency), makes it more convenient to generate random values.
//! * `serde` - (dependency), implements `serde`-based serialization and //! * `serde` - (dependency), implements `serde`-based serialization and
//! deserialization. //! deserialization.
@ -31,9 +30,8 @@
#![cfg_attr(all(not(feature = "std"), not(test)), no_std)] #![cfg_attr(all(not(feature = "std"), not(test)), no_std)]
// Experimental features we need // Experimental features we need.
#![cfg_attr(all(test, feature = "unstable"), feature(test))] #![cfg_attr(bench, feature(test))]
#![cfg_attr(docsrs, feature(doc_cfg))] #![cfg_attr(docsrs, feature(doc_cfg))]
// Coding conventions // Coding conventions
@ -56,6 +54,8 @@ compile_error!("rust-bitcoin currently only supports architectures with pointers
than 16 bits, let us know if you want 16-bit support. Note that we do than 16 bits, let us know if you want 16-bit support. Note that we do
NOT guarantee that we will implement it!"); NOT guarantee that we will implement it!");
#[cfg(bench)] extern crate test;
#[cfg(feature = "no-std")] #[cfg(feature = "no-std")]
#[macro_use] #[macro_use]
extern crate alloc; extern crate alloc;
@ -184,10 +184,10 @@ mod prelude {
pub use std::collections::HashSet; pub use std::collections::HashSet;
} }
#[cfg(all(test, feature = "unstable"))] use tests::EmptyWrite; #[cfg(bench)] use bench::EmptyWrite;
#[cfg(all(test, feature = "unstable"))] #[cfg(bench)]
mod tests { mod bench {
use core::fmt::Arguments; use core::fmt::Arguments;
use crate::io::{IoSlice, Result, Write}; use crate::io::{IoSlice, Result, Write};