Skip to content

Commit 2197d9b

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 64b9e83 commit 2197d9b

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
@@ -8139,7 +8139,7 @@ mod tests {
81398139
}
81408140
}
81418141

8142-
#[cfg(all(any(test, feature = "_test_utils"), feature = "_bench_unstable"))]
8142+
#[cfg(bench)]
81438143
pub mod bench {
81448144
use crate::chain::Listen;
81458145
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

lightning/src/routing/router.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -611,7 +611,7 @@ struct PathBuildingHop<'a> {
611611
/// decrease as well. Thus, we have to explicitly track which nodes have been processed and
612612
/// avoid processing them again.
613613
was_processed: bool,
614-
#[cfg(all(not(feature = "_bench_unstable"), any(test, fuzzing)))]
614+
#[cfg(all(not(bench), any(test, fuzzing)))]
615615
// In tests, we apply further sanity checks on cases where we skip nodes we already processed
616616
// to ensure it is specifically in cases where the fee has gone down because of a decrease in
617617
// value_contribution_msat, which requires tracking it here. See comments below where it is
@@ -632,7 +632,7 @@ impl<'a> core::fmt::Debug for PathBuildingHop<'a> {
632632
.field("path_penalty_msat", &self.path_penalty_msat)
633633
.field("path_htlc_minimum_msat", &self.path_htlc_minimum_msat)
634634
.field("cltv_expiry_delta", &self.candidate.cltv_expiry_delta());
635-
#[cfg(all(not(feature = "_bench_unstable"), any(test, fuzzing)))]
635+
#[cfg(all(not(bench), any(test, fuzzing)))]
636636
let debug_struct = debug_struct
637637
.field("value_contribution_msat", &self.value_contribution_msat);
638638
debug_struct.finish()
@@ -1151,14 +1151,14 @@ where L::Target: Logger {
11511151
path_htlc_minimum_msat,
11521152
path_penalty_msat: u64::max_value(),
11531153
was_processed: false,
1154-
#[cfg(all(not(feature = "_bench_unstable"), any(test, fuzzing)))]
1154+
#[cfg(all(not(bench), any(test, fuzzing)))]
11551155
value_contribution_msat,
11561156
}
11571157
});
11581158

11591159
#[allow(unused_mut)] // We only use the mut in cfg(test)
11601160
let mut should_process = !old_entry.was_processed;
1161-
#[cfg(all(not(feature = "_bench_unstable"), any(test, fuzzing)))]
1161+
#[cfg(all(not(bench), any(test, fuzzing)))]
11621162
{
11631163
// In test/fuzzing builds, we do extra checks to make sure the skipping
11641164
// of already-seen nodes only happens in cases we expect (see below).
@@ -1253,13 +1253,13 @@ where L::Target: Logger {
12531253
old_entry.fee_msat = 0; // This value will be later filled with hop_use_fee_msat of the following channel
12541254
old_entry.path_htlc_minimum_msat = path_htlc_minimum_msat;
12551255
old_entry.path_penalty_msat = path_penalty_msat;
1256-
#[cfg(all(not(feature = "_bench_unstable"), any(test, fuzzing)))]
1256+
#[cfg(all(not(bench), any(test, fuzzing)))]
12571257
{
12581258
old_entry.value_contribution_msat = value_contribution_msat;
12591259
}
12601260
did_add_update_path_to_src_node = true;
12611261
} else if old_entry.was_processed && new_cost < old_cost {
1262-
#[cfg(all(not(feature = "_bench_unstable"), any(test, fuzzing)))]
1262+
#[cfg(all(not(bench), any(test, fuzzing)))]
12631263
{
12641264
// If we're skipping processing a node which was previously
12651265
// processed even though we found another path to it with a
@@ -5504,7 +5504,7 @@ pub(crate) mod bench_utils {
55045504
}
55055505
}
55065506

5507-
#[cfg(all(test, feature = "_bench_unstable", not(feature = "no-std")))]
5507+
#[cfg(all(test, bench, not(features = "no-std")))]
55085508
mod benches {
55095509
use super::*;
55105510
use bitcoin::hashes::Hash;

lightning/src/util/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ pub mod wakers;
2727
pub(crate) mod atomic_counter;
2828
pub(crate) mod byte_utils;
2929
pub(crate) mod chacha20;
30-
#[cfg(all(any(feature = "_bench_unstable", not(test)), feature = "std"))]
30+
#[cfg(all(any(bench, not(test), features = "std")))]
3131
pub(crate) mod fairrwlock;
3232
#[cfg(fuzzing)]
3333
pub mod zbase32;

0 commit comments

Comments
 (0)