Skip to content

Commit 2db80b6

Browse files
committed
Use RUSTFLAGS instead of feature to gate benching
Currently we use the `"_bench_unstable"` feature to gate benching code. This results in features not being additive for stable toolchains i.e., one cannot currently run `cargo +stable check --all-features`. Although this method of gating bench code is widely proposed as a good solution online there is another solution that keeps all features additive. Guard the bench code with `#[cfg(bench)]` and when running benches use `RUSTFLAGS='--cfg=bench'` to turn on `bench`.
1 parent fb6e018 commit 2db80b6

File tree

12 files changed

+28
-32
lines changed

12 files changed

+28
-32
lines changed

.github/workflows/build.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,8 @@ jobs:
273273
cd ..
274274
- name: Run benchmarks on Rust ${{ matrix.toolchain }}
275275
run: |
276-
RUSTC_BOOTSTRAP=1 cargo bench --features _bench_unstable
276+
277+
RUSTC_BOOTSTRAP=1 RUSTFLAGS='--cfg=bench' cargo bench
277278
278279
check_commits:
279280
runs-on: ubuntu-latest

lightning-persister/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ all-features = true
1414
rustdoc-args = ["--cfg", "docsrs"]
1515

1616
[features]
17-
_bench_unstable = ["lightning/_bench_unstable"]
1817

1918
[dependencies]
2019
bitcoin = "0.29.0"

lightning-persister/src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88

99
#![cfg_attr(docsrs, feature(doc_auto_cfg))]
1010

11-
#![cfg_attr(all(test, feature = "_bench_unstable"), feature(test))]
12-
#[cfg(all(test, feature = "_bench_unstable"))] extern crate test;
11+
#![cfg_attr(bench, feature(test))]
12+
#[cfg(bench)] extern crate test;
1313

1414
mod util;
1515

@@ -316,7 +316,7 @@ mod tests {
316316
}
317317
}
318318

319-
#[cfg(all(test, feature = "_bench_unstable"))]
319+
#[cfg(bench)]
320320
pub mod bench {
321321
use test::Bencher;
322322

lightning-rapid-gossip-sync/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ Utility to process gossip routing data from Rapid Gossip Sync Server.
1313
default = ["std"]
1414
no-std = ["lightning/no-std"]
1515
std = ["lightning/std"]
16-
_bench_unstable = []
1716

1817
[dependencies]
1918
lightning = { version = "0.0.112", path = "../lightning", default-features = false }

lightning-rapid-gossip-sync/src/lib.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,8 @@
6262
6363
#![cfg_attr(all(not(feature = "std"), not(test)), no_std)]
6464

65-
// Allow and import test features for benching
66-
#![cfg_attr(all(test, feature = "_bench_unstable"), feature(test))]
67-
#[cfg(all(test, feature = "_bench_unstable"))]
68-
extern crate test;
65+
#![cfg_attr(bench, feature(test))]
66+
#[cfg(bench)] extern crate test;
6967

7068
#[cfg(not(feature = "std"))]
7169
extern crate alloc;
@@ -278,7 +276,7 @@ mod tests {
278276
}
279277
}
280278

281-
#[cfg(all(test, feature = "_bench_unstable"))]
279+
#[cfg(bench)]
282280
pub mod bench {
283281
use test::Bencher;
284282

lightning/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ max_level_trace = []
2828
# Allow signing of local transactions that may have been revoked or will be revoked, for functional testing (e.g. justice tx handling).
2929
# This is unsafe to use in production because it may result in the counterparty publishing taking our funds.
3030
unsafe_revoked_tx_signing = []
31-
_bench_unstable = []
3231

3332
no-std = ["hashbrown", "bitcoin/no-std", "core2/alloc"]
3433
std = ["bitcoin/std"]

lightning/src/lib.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@
5454

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

57-
#![cfg_attr(all(any(test, feature = "_test_utils"), feature = "_bench_unstable"), feature(test))]
58-
#[cfg(all(any(test, feature = "_test_utils"), feature = "_bench_unstable"))] extern crate test;
57+
#![cfg_attr(bench, feature(test))]
58+
#[cfg(bench)] extern crate test;
5959

6060
#[cfg(not(any(feature = "std", feature = "no-std")))]
6161
compile_error!("at least one of the `std` or `no-std` features must be enabled");
@@ -175,18 +175,18 @@ mod prelude {
175175
pub use alloc::string::ToString;
176176
}
177177

178-
#[cfg(all(not(feature = "_bench_unstable"), feature = "std", test))]
178+
#[cfg(all(not(bench), feature = "std", test))]
179179
mod debug_sync;
180-
#[cfg(all(not(feature = "_bench_unstable"), feature = "backtrace", feature = "std", test))]
180+
#[cfg(all(not(bench), feature = "backtrace", feature = "std", test))]
181181
extern crate backtrace;
182182

183183
#[cfg(feature = "std")]
184184
mod sync {
185-
#[cfg(all(not(feature = "_bench_unstable"), test))]
185+
#[cfg(all(not(bench), test))]
186186
pub use crate::debug_sync::*;
187-
#[cfg(any(feature = "_bench_unstable", not(test)))]
187+
#[cfg(any(bench, not(test)))]
188188
pub use ::std::sync::{Arc, Mutex, Condvar, MutexGuard, RwLock, RwLockReadGuard, RwLockWriteGuard};
189-
#[cfg(any(feature = "_bench_unstable", not(test)))]
189+
#[cfg(any(bench, not(test)))]
190190
pub use crate::util::fairrwlock::FairRwLock;
191191
}
192192

lightning/src/ln/channelmanager.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8380,7 +8380,7 @@ mod tests {
83808380
}
83818381
}
83828382

8383-
#[cfg(all(any(test, feature = "_test_utils"), feature = "_bench_unstable"))]
8383+
#[cfg(bench)]
83848384
pub mod bench {
83858385
use crate::chain::Listen;
83868386
use crate::chain::chainmonitor::{ChainMonitor, Persist};

lightning/src/ln/functional_test_utils.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1470,7 +1470,7 @@ macro_rules! expect_pending_htlcs_forwardable_from_events {
14701470
}}
14711471
}
14721472
#[macro_export]
1473-
#[cfg(any(test, feature = "_bench_unstable", feature = "_test_utils"))]
1473+
#[cfg(any(test, bench, feature = "_test_utils"))]
14741474
macro_rules! expect_payment_received {
14751475
($node: expr, $expected_payment_hash: expr, $expected_payment_secret: expr, $expected_recv_value: expr) => {
14761476
expect_payment_received!($node, $expected_payment_hash, $expected_payment_secret, $expected_recv_value, None, $node.node.get_our_node_id())
@@ -1497,7 +1497,7 @@ macro_rules! expect_payment_received {
14971497
}
14981498

14991499
#[macro_export]
1500-
#[cfg(any(test, feature = "_bench_unstable", feature = "_test_utils"))]
1500+
#[cfg(any(test, bench, feature = "_test_utils"))]
15011501
macro_rules! expect_payment_claimed {
15021502
($node: expr, $expected_payment_hash: expr, $expected_recv_value: expr) => {
15031503
let events = $node.node.get_and_clear_pending_events();
@@ -1603,7 +1603,7 @@ macro_rules! expect_payment_forwarded {
16031603
}
16041604
}
16051605

1606-
#[cfg(any(test, feature = "_bench_unstable", feature = "_test_utils"))]
1606+
#[cfg(any(test, bench, feature = "_test_utils"))]
16071607
pub fn expect_channel_ready_event<'a, 'b, 'c, 'd>(node: &'a Node<'b, 'c, 'd>, expected_counterparty_node_id: &PublicKey) {
16081608
let events = node.node.get_and_clear_pending_events();
16091609
assert_eq!(events.len(), 1);

lightning/src/routing/gossip.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3280,7 +3280,7 @@ mod tests {
32803280
}
32813281
}
32823282

3283-
#[cfg(all(test, feature = "_bench_unstable"))]
3283+
#[cfg(bench)]
32843284
mod benches {
32853285
use super::*;
32863286

0 commit comments

Comments
 (0)