Skip to content

Commit 62dceff

Browse files
committed
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 { ... }
1 parent bb69419 commit 62dceff

File tree

7 files changed

+17
-13
lines changed

7 files changed

+17
-13
lines changed

Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ edition = "2018"
1515
# Please don't forget to add relevant features to docs.rs below
1616
[features]
1717
default = [ "std", "secp-recovery" ]
18-
unstable = []
1918
rand = ["secp256k1/rand-std"]
2019
serde = ["actual-serde", "bitcoin_hashes/serde", "secp256k1/serde"]
2120
secp-lowmemory = ["secp256k1/lowmemory"]

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,11 @@ shell alias to check your documentation changes build correctly.
110110
alias build-docs='RUSTDOCFLAGS="--cfg docsrs" cargo +nightly rustdoc --features="$FEATURES" -- -D rustdoc::broken-intra-doc-links'
111111
```
112112

113+
### Running benchmarks
114+
115+
We use a custom Rust compiler configuration conditional to guard the bench mark code. To run the
116+
bench marks use: `RUSTFLAGS='--cfg=bench' cargo +nightly bench`.
117+
113118
## Pull Requests
114119

115120
Every PR needs at least two reviews to get merged. During the review phase

contrib/test.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ then
7777
)
7878
fi
7979

80-
# Bench if told to
80+
# Bench if told to, only works with non-stable toolchain (nightly, beta).
8181
if [ "$DO_BENCH" = true ]
8282
then
8383
if [ "NIGHTLY" = false ]
@@ -90,7 +90,7 @@ then
9090
fi
9191
exit 1
9292
fi
93-
cargo bench --features unstable
93+
RUSTFLAGS='--cfg=bench' cargo bench
9494
fi
9595

9696
# Use as dependency if told to

src/blockdata/block.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -528,7 +528,7 @@ mod tests {
528528
}
529529
}
530530

531-
#[cfg(all(test, feature = "unstable"))]
531+
#[cfg(bench)]
532532
mod benches {
533533
use super::Block;
534534
use crate::EmptyWrite;

src/blockdata/transaction.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1724,7 +1724,7 @@ mod tests {
17241724
}
17251725
}
17261726

1727-
#[cfg(all(test, feature = "unstable"))]
1727+
#[cfg(bench)]
17281728
mod benches {
17291729
use super::Transaction;
17301730
use crate::EmptyWrite;

src/blockdata/witness.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -441,7 +441,7 @@ mod test {
441441
}
442442

443443

444-
#[cfg(all(test, feature = "unstable"))]
444+
#[cfg(bench)]
445445
mod benches {
446446
use test::{Bencher, black_box};
447447
use super::Witness;

src/lib.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
//! * `std` - the usual dependency on `std` (default).
2020
//! * `secp-recovery` - enables calculating public key from a signature and message.
2121
//! * `base64` - (dependency), enables encoding of PSBTs and message signatures.
22-
//! * `unstable` - enables unstable features for testing.
2322
//! * `rand` - (dependency), makes it more convenient to generate random values.
2423
//! * `serde` - (dependency), implements `serde`-based serialization and
2524
//! deserialization.
@@ -31,9 +30,8 @@
3130
3231
#![cfg_attr(all(not(feature = "std"), not(test)), no_std)]
3332

34-
// Experimental features we need
35-
#![cfg_attr(all(test, feature = "unstable"), feature(test))]
36-
33+
// Experimental features we need.
34+
#![cfg_attr(bench, feature(test))]
3735
#![cfg_attr(docsrs, feature(doc_cfg))]
3836

3937
// Coding conventions
@@ -56,6 +54,8 @@ compile_error!("rust-bitcoin currently only supports architectures with pointers
5654
than 16 bits, let us know if you want 16-bit support. Note that we do
5755
NOT guarantee that we will implement it!");
5856

57+
#[cfg(bench)] extern crate test;
58+
5959
#[cfg(feature = "no-std")]
6060
#[macro_use]
6161
extern crate alloc;
@@ -184,10 +184,10 @@ mod prelude {
184184
pub use std::collections::HashSet;
185185
}
186186

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

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

0 commit comments

Comments
 (0)