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

lightning/src/routing/router.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1015,7 +1015,7 @@ struct PathBuildingHop<'a> {
10151015
/// decrease as well. Thus, we have to explicitly track which nodes have been processed and
10161016
/// avoid processing them again.
10171017
was_processed: bool,
1018-
#[cfg(all(not(feature = "_bench_unstable"), any(test, fuzzing)))]
1018+
#[cfg(all(not(bench), any(test, fuzzing)))]
10191019
// In tests, we apply further sanity checks on cases where we skip nodes we already processed
10201020
// to ensure it is specifically in cases where the fee has gone down because of a decrease in
10211021
// value_contribution_msat, which requires tracking it here. See comments below where it is
@@ -1036,7 +1036,7 @@ impl<'a> core::fmt::Debug for PathBuildingHop<'a> {
10361036
.field("path_penalty_msat", &self.path_penalty_msat)
10371037
.field("path_htlc_minimum_msat", &self.path_htlc_minimum_msat)
10381038
.field("cltv_expiry_delta", &self.candidate.cltv_expiry_delta());
1039-
#[cfg(all(not(feature = "_bench_unstable"), any(test, fuzzing)))]
1039+
#[cfg(all(not(bench), any(test, fuzzing)))]
10401040
let debug_struct = debug_struct
10411041
.field("value_contribution_msat", &self.value_contribution_msat);
10421042
debug_struct.finish()
@@ -1570,14 +1570,14 @@ where L::Target: Logger {
15701570
path_htlc_minimum_msat,
15711571
path_penalty_msat: u64::max_value(),
15721572
was_processed: false,
1573-
#[cfg(all(not(feature = "_bench_unstable"), any(test, fuzzing)))]
1573+
#[cfg(all(not(bench), any(test, fuzzing)))]
15741574
value_contribution_msat,
15751575
}
15761576
});
15771577

15781578
#[allow(unused_mut)] // We only use the mut in cfg(test)
15791579
let mut should_process = !old_entry.was_processed;
1580-
#[cfg(all(not(feature = "_bench_unstable"), any(test, fuzzing)))]
1580+
#[cfg(all(not(bench), any(test, fuzzing)))]
15811581
{
15821582
// In test/fuzzing builds, we do extra checks to make sure the skipping
15831583
// of already-seen nodes only happens in cases we expect (see below).
@@ -1648,13 +1648,13 @@ where L::Target: Logger {
16481648
old_entry.fee_msat = 0; // This value will be later filled with hop_use_fee_msat of the following channel
16491649
old_entry.path_htlc_minimum_msat = path_htlc_minimum_msat;
16501650
old_entry.path_penalty_msat = path_penalty_msat;
1651-
#[cfg(all(not(feature = "_bench_unstable"), any(test, fuzzing)))]
1651+
#[cfg(all(not(bench), any(test, fuzzing)))]
16521652
{
16531653
old_entry.value_contribution_msat = value_contribution_msat;
16541654
}
16551655
did_add_update_path_to_src_node = true;
16561656
} else if old_entry.was_processed && new_cost < old_cost {
1657-
#[cfg(all(not(feature = "_bench_unstable"), any(test, fuzzing)))]
1657+
#[cfg(all(not(bench), any(test, fuzzing)))]
16581658
{
16591659
// If we're skipping processing a node which was previously
16601660
// processed even though we found another path to it with a
@@ -6079,7 +6079,7 @@ pub(crate) mod bench_utils {
60796079
}
60806080
}
60816081

6082-
#[cfg(all(test, feature = "_bench_unstable", not(feature = "no-std")))]
6082+
#[cfg(all(bench, not(features = "no-std")))]
60836083
mod benches {
60846084
use super::*;
60856085
use bitcoin::hashes::Hash;

lightning/src/sign/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1628,7 +1628,7 @@ pub fn dyn_sign() {
16281628
let _signer: Box<dyn EcdsaChannelSigner>;
16291629
}
16301630

1631-
#[cfg(all(test, feature = "_bench_unstable", not(feature = "no-std")))]
1631+
#[cfg(all(test, bench, not(feature = "no-std")))]
16321632
mod benches {
16331633
use std::sync::{Arc, mpsc};
16341634
use std::sync::mpsc::TryRecvError;

lightning/src/sync/mod.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
pub(crate) enum LockHeldState {
44
HeldByThread,
55
NotHeldByThread,
6-
#[cfg(any(feature = "_bench_unstable", not(test)))]
6+
#[cfg(any(bench, not(test)))]
77
Unsupported,
88
}
99

@@ -20,20 +20,20 @@ pub(crate) trait LockTestExt<'a> {
2020
fn unsafe_well_ordered_double_lock_self(&'a self) -> Self::ExclLock;
2121
}
2222

23-
#[cfg(all(feature = "std", not(feature = "_bench_unstable"), test))]
23+
#[cfg(all(feature = "std", not(bench), test))]
2424
mod debug_sync;
25-
#[cfg(all(feature = "std", not(feature = "_bench_unstable"), test))]
25+
#[cfg(all(feature = "std", not(bench), test))]
2626
pub use debug_sync::*;
27-
#[cfg(all(feature = "std", not(feature = "_bench_unstable"), test))]
27+
#[cfg(all(feature = "std", not(bench), test))]
2828
// Note that to make debug_sync's regex work this must not contain `debug_string` in the module name
2929
mod test_lockorder_checks;
3030

31-
#[cfg(all(feature = "std", any(feature = "_bench_unstable", not(test))))]
31+
#[cfg(all(feature = "std", any(bench, not(test))))]
3232
pub(crate) mod fairrwlock;
33-
#[cfg(all(feature = "std", any(feature = "_bench_unstable", not(test))))]
33+
#[cfg(all(feature = "std", any(bench, not(test))))]
3434
pub use {std::sync::{Arc, Mutex, Condvar, MutexGuard, RwLock, RwLockReadGuard, RwLockWriteGuard}, fairrwlock::FairRwLock};
3535

36-
#[cfg(all(feature = "std", any(feature = "_bench_unstable", not(test))))]
36+
#[cfg(all(feature = "std", any(bench, not(test))))]
3737
mod ext_impl {
3838
use super::*;
3939
impl<'a, T: 'a> LockTestExt<'a> for Mutex<T> {

0 commit comments

Comments
 (0)