Skip to content

Commit aeab7a7

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 d7f6e34 commit aeab7a7

File tree

13 files changed

+32
-36
lines changed

13 files changed

+32
-36
lines changed

.github/workflows/build.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,8 @@ jobs:
141141
cd ..
142142
- name: Run benchmarks on Rust ${{ matrix.toolchain }}
143143
run: |
144-
RUSTC_BOOTSTRAP=1 cargo bench --features _bench_unstable
144+
145+
RUSTC_BOOTSTRAP=1 RUSTFLAGS='--cfg=bench' cargo bench
145146
146147
check_commits:
147148
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

@@ -338,7 +338,7 @@ mod tests {
338338
}
339339
}
340340

341-
#[cfg(all(test, feature = "_bench_unstable"))]
341+
#[cfg(bench)]
342342
pub mod bench {
343343
use test::Bencher;
344344

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.115", 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
@@ -64,10 +64,8 @@
6464
6565
#![cfg_attr(all(not(feature = "std"), not(test)), no_std)]
6666

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

7270
#[cfg(not(feature = "std"))]
7371
extern crate alloc;
@@ -287,7 +285,7 @@ mod tests {
287285
}
288286
}
289287

290-
#[cfg(all(test, feature = "_bench_unstable"))]
288+
#[cfg(bench)]
291289
pub mod bench {
292290
use test::Bencher;
293291

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
# Override signing to not include randomness when generating signatures for test vectors.
3332
_test_vectors = []
3433

lightning/src/lib.rs

Lines changed: 3 additions & 3 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");
@@ -177,7 +177,7 @@ mod prelude {
177177
pub use alloc::string::ToString;
178178
}
179179

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
mod sync;

lightning/src/ln/channelmanager.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9266,7 +9266,7 @@ mod tests {
92669266
}
92679267
}
92689268

9269-
#[cfg(all(any(test, feature = "_test_utils"), feature = "_bench_unstable"))]
9269+
#[cfg(bench)]
92709270
pub mod bench {
92719271
use crate::chain::Listen;
92729272
use crate::chain::chainmonitor::{ChainMonitor, Persist};

lightning/src/ln/functional_test_utils.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1769,7 +1769,7 @@ macro_rules! get_route_and_payment_hash {
17691769
}
17701770

17711771
#[macro_export]
1772-
#[cfg(any(test, feature = "_bench_unstable", feature = "_test_utils"))]
1772+
#[cfg(any(test, bench, feature = "_test_utils"))]
17731773
macro_rules! expect_payment_claimable {
17741774
($node: expr, $expected_payment_hash: expr, $expected_payment_secret: expr, $expected_recv_value: expr) => {
17751775
expect_payment_claimable!($node, $expected_payment_hash, $expected_payment_secret, $expected_recv_value, None, $node.node.get_our_node_id())
@@ -1796,7 +1796,7 @@ macro_rules! expect_payment_claimable {
17961796
}
17971797

17981798
#[macro_export]
1799-
#[cfg(any(test, feature = "_bench_unstable", feature = "_test_utils"))]
1799+
#[cfg(any(test, bench, feature = "_test_utils"))]
18001800
macro_rules! expect_payment_claimed {
18011801
($node: expr, $expected_payment_hash: expr, $expected_recv_value: expr) => {
18021802
let events = $node.node.get_and_clear_pending_events();
@@ -1913,7 +1913,7 @@ macro_rules! expect_payment_forwarded {
19131913
}
19141914
}
19151915

1916-
#[cfg(any(test, feature = "_bench_unstable", feature = "_test_utils"))]
1916+
#[cfg(any(test, bench, feature = "_test_utils"))]
19171917
pub fn expect_channel_pending_event<'a, 'b, 'c, 'd>(node: &'a Node<'b, 'c, 'd>, expected_counterparty_node_id: &PublicKey) {
19181918
let events = node.node.get_and_clear_pending_events();
19191919
assert_eq!(events.len(), 1);
@@ -1925,7 +1925,7 @@ pub fn expect_channel_pending_event<'a, 'b, 'c, 'd>(node: &'a Node<'b, 'c, 'd>,
19251925
}
19261926
}
19271927

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

lightning/src/routing/gossip.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3388,7 +3388,7 @@ pub(crate) mod tests {
33883388
}
33893389
}
33903390

3391-
#[cfg(all(test, feature = "_bench_unstable"))]
3391+
#[cfg(bench)]
33923392
mod benches {
33933393
use super::*;
33943394

0 commit comments

Comments
 (0)