Skip to content

Commit

Permalink
Create configuration conditional bench
Browse files Browse the repository at this point in the history
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 {
        ...
    }
  • Loading branch information
tcharding committed Jul 14, 2022
1 parent f60c92c commit f3b2120
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 13 deletions.
1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ edition = "2018"
# Please don't forget to add relevant features to docs.rs below
[features]
default = [ "std", "secp-recovery" ]
unstable = []
rand = ["secp256k1/rand-std"]
serde = ["actual-serde", "bitcoin_hashes/serde", "secp256k1/serde"]
secp-lowmemory = ["secp256k1/lowmemory"]
Expand Down
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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'
```

### 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

Every PR needs at least two reviews to get merged. During the review phase
Expand Down
4 changes: 2 additions & 2 deletions contrib/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ then
)
fi

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

# Use as dependency if told to
Expand Down
2 changes: 1 addition & 1 deletion src/blockdata/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -528,7 +528,7 @@ mod tests {
}
}

#[cfg(all(test, feature = "unstable"))]
#[cfg(bench)]
mod benches {
use super::Block;
use crate::EmptyWrite;
Expand Down
2 changes: 1 addition & 1 deletion src/blockdata/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1724,7 +1724,7 @@ mod tests {
}
}

#[cfg(all(test, feature = "unstable"))]
#[cfg(bench)]
mod benches {
use super::Transaction;
use crate::EmptyWrite;
Expand Down
2 changes: 1 addition & 1 deletion src/blockdata/witness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,7 @@ mod test {
}


#[cfg(all(test, feature = "unstable"))]
#[cfg(bench)]
mod benches {
use test::{Bencher, black_box};
use super::Witness;
Expand Down
14 changes: 7 additions & 7 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
//! * `std` - the usual dependency on `std` (default).
//! * `secp-recovery` - enables calculating public key from a signature and message.
//! * `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.
//! * `serde` - (dependency), implements `serde`-based serialization and
//! deserialization.
Expand All @@ -31,9 +30,8 @@

#![cfg_attr(all(not(feature = "std"), not(test)), no_std)]

// Experimental features we need
#![cfg_attr(all(test, feature = "unstable"), feature(test))]

// Experimental features we need.
#![cfg_attr(bench, feature(test))]
#![cfg_attr(docsrs, feature(doc_cfg))]

// Coding conventions
Expand All @@ -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
NOT guarantee that we will implement it!");

#[cfg(bench)] extern crate test;

#[cfg(feature = "no-std")]
#[macro_use]
extern crate alloc;
Expand Down Expand Up @@ -184,10 +184,10 @@ mod prelude {
pub use std::collections::HashSet;
}

#[cfg(all(test, feature = "unstable"))] use tests::EmptyWrite;
#[cfg(bench)] use bench::EmptyWrite;

#[cfg(all(test, feature = "unstable"))]
mod tests {
#[cfg(bench)]
mod bench {
use core::fmt::Arguments;
use crate::io::{IoSlice, Result, Write};

Expand Down

0 comments on commit f3b2120

Please sign in to comment.