Skip to content

Commit cd12d14

Browse files
svyatonikbkchr
authored andcommitted
Fix HeadersToKeep and MaxBridgedAuthorities in Millau benchmarks (#1851)
* fix `HeadersToKeep` and `MaxBridgedAuthorities` in Millau benchmarks * typo * impl review suggestion
1 parent 411150b commit cd12d14

File tree

6 files changed

+65
-63
lines changed

6 files changed

+65
-63
lines changed

bridges/bin/millau/runtime/src/lib.rs

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -387,30 +387,12 @@ impl pallet_bridge_relayers::Config for Runtime {
387387
type WeightInfo = ();
388388
}
389389

390-
#[cfg(feature = "runtime-benchmarks")]
391-
parameter_types! {
392-
/// Number of headers to keep in benchmarks.
393-
///
394-
/// In benchmarks we always populate with full number of `HeadersToKeep` to make sure that
395-
/// pruning is taken into account.
396-
///
397-
/// Note: This is lower than regular value, to speed up benchmarking setup.
398-
pub const HeadersToKeep: u32 = 1024;
399-
/// Maximal number of authorities at Rialto.
400-
///
401-
/// In benchmarks we're using sets of up to `1024` authorities to prepare for possible
402-
/// upgrades in the future and see if performance degrades when number of authorities
403-
/// grow.
404-
pub const MaxAuthoritiesAtRialto: u32 = pallet_bridge_grandpa::benchmarking::MAX_VALIDATOR_SET_SIZE;
405-
}
406-
407-
#[cfg(not(feature = "runtime-benchmarks"))]
408390
parameter_types! {
409391
/// Number of headers to keep.
410392
///
411393
/// Assuming the worst case of every header being finalized, we will keep headers at least for a
412-
/// week.
413-
pub const HeadersToKeep: u32 = 7 * bp_rialto::DAYS;
394+
/// day.
395+
pub const HeadersToKeep: u32 = bp_rialto::DAYS;
414396
/// Maximal number of authorities at Rialto.
415397
pub const MaxAuthoritiesAtRialto: u32 = bp_rialto::MAX_AUTHORITIES_COUNT;
416398
}

bridges/bin/rialto-parachain/runtime/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -533,8 +533,8 @@ parameter_types! {
533533
/// Number of headers to keep.
534534
///
535535
/// Assuming the worst case of every header being finalized, we will keep headers at least for a
536-
/// week.
537-
pub const HeadersToKeep: u32 = 7 * bp_millau::DAYS as u32;
536+
/// day.
537+
pub const HeadersToKeep: u32 = bp_millau::DAYS as u32;
538538

539539
/// Maximal number of authorities at Millau.
540540
pub const MaxAuthoritiesAtMillau: u32 = bp_millau::MAX_AUTHORITIES_COUNT;

bridges/bin/rialto/runtime/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -401,8 +401,8 @@ parameter_types! {
401401
/// Number of headers to keep.
402402
///
403403
/// Assuming the worst case of every header being finalized, we will keep headers at least for a
404-
/// week.
405-
pub const HeadersToKeep: u32 = 7 * bp_rialto::DAYS;
404+
/// day.
405+
pub const HeadersToKeep: u32 = bp_rialto::DAYS;
406406

407407
/// Maximal number of authorities at Millau.
408408
pub const MaxAuthoritiesAtMillau: u32 = bp_millau::MAX_AUTHORITIES_COUNT;

bridges/modules/grandpa/src/benchmarking.rs

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -53,26 +53,29 @@ use sp_finality_grandpa::AuthorityId;
5353
use sp_runtime::traits::Zero;
5454
use sp_std::vec::Vec;
5555

56-
// The maximum number of vote ancestries to include in a justification.
57-
//
58-
// In practice this would be limited by the session length (number of blocks a single authority set
59-
// can produce) of a given chain.
56+
/// The maximum number of vote ancestries to include in a justification.
57+
///
58+
/// In practice this would be limited by the session length (number of blocks a single authority set
59+
/// can produce) of a given chain.
6060
const MAX_VOTE_ANCESTRIES: u32 = 1000;
6161

62-
// The maximum number of pre-commits to include in a justification. In practice this scales with the
63-
// number of validators.
64-
pub const MAX_VALIDATOR_SET_SIZE: u32 = 1024;
65-
66-
// `1..MAX_VALIDATOR_SET_SIZE` and `1..MAX_VOTE_ANCESTRIES` are too large && benchmarks are
67-
// running for almost 40m (steps=50, repeat=20) on a decent laptop, which is too much. Since
68-
// we're building linear function here, let's just select some limited subrange for benchmarking.
69-
const VALIDATOR_SET_SIZE_RANGE_BEGIN: u32 = MAX_VALIDATOR_SET_SIZE / 20;
70-
const VALIDATOR_SET_SIZE_RANGE_END: u32 =
71-
VALIDATOR_SET_SIZE_RANGE_BEGIN + VALIDATOR_SET_SIZE_RANGE_BEGIN;
62+
// `1..MAX_VOTE_ANCESTRIES` is too large && benchmarks are running for almost 40m (steps=50,
63+
// repeat=20) on a decent laptop, which is too much. Since we're building linear function here,
64+
// let's just select some limited subrange for benchmarking.
7265
const MAX_VOTE_ANCESTRIES_RANGE_BEGIN: u32 = MAX_VOTE_ANCESTRIES / 20;
7366
const MAX_VOTE_ANCESTRIES_RANGE_END: u32 =
7467
MAX_VOTE_ANCESTRIES_RANGE_BEGIN + MAX_VOTE_ANCESTRIES_RANGE_BEGIN;
7568

69+
// the same with validators - if there are too much validators, let's run benchmarks on subrange
70+
fn validator_set_range_end<T: Config<I>, I: 'static>() -> u32 {
71+
let max_bridged_authorities = T::MaxBridgedAuthorities::get();
72+
if max_bridged_authorities > 128 {
73+
sp_std::cmp::max(128, max_bridged_authorities / 5)
74+
} else {
75+
max_bridged_authorities
76+
}
77+
}
78+
7679
/// Returns number of first header to be imported.
7780
///
7881
/// Since we bootstrap the pallet with `HeadersToKeep` already imported headers,
@@ -117,7 +120,7 @@ benchmarks_instance_pallet! {
117120
// This is the "gold standard" benchmark for this extrinsic, and it's what should be used to
118121
// annotate the weight in the pallet.
119122
submit_finality_proof {
120-
let p in VALIDATOR_SET_SIZE_RANGE_BEGIN..VALIDATOR_SET_SIZE_RANGE_END;
123+
let p in 1 .. validator_set_range_end::<T, I>();
121124
let v in MAX_VOTE_ANCESTRIES_RANGE_BEGIN..MAX_VOTE_ANCESTRIES_RANGE_END;
122125
let caller: T::AccountId = whitelisted_caller();
123126
let (header, justification) = prepare_benchmark_data::<T, I>(p, v);

bridges/modules/grandpa/src/lib.rs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,9 @@ pub mod pallet {
104104
#[pallet::constant]
105105
type MaxRequests: Get<u32>;
106106

107+
// Avoid using `HeadersToKeep` directly in the pallet code. Use `headers_to_keep` function
108+
// instead.
109+
107110
/// Maximal number of finalized headers to keep in the storage.
108111
///
109112
/// The setting is there to prevent growing the on-chain state indefinitely. Note
@@ -464,6 +467,20 @@ pub mod pallet {
464467
})?)
465468
}
466469

470+
/// Return number of headers to keep in the runtime storage.
471+
#[cfg(not(feature = "runtime-benchmarks"))]
472+
pub(crate) fn headers_to_keep<T: Config<I>, I: 'static>() -> u32 {
473+
T::HeadersToKeep::get()
474+
}
475+
476+
/// Return number of headers to keep in the runtime storage.
477+
#[cfg(feature = "runtime-benchmarks")]
478+
pub(crate) fn headers_to_keep<T: Config<I>, I: 'static>() -> u32 {
479+
// every db operation (significantly) slows down benchmarks, so let's keep as min as
480+
// possible
481+
2
482+
}
483+
467484
/// Import a previously verified header to the storage.
468485
///
469486
/// Note this function solely takes care of updating the storage and pruning old entries,
@@ -479,7 +496,7 @@ pub mod pallet {
479496
<ImportedHashes<T, I>>::insert(index, hash);
480497

481498
// Update ring buffer pointer and remove old header.
482-
<ImportedHashesPointer<T, I>>::put((index + 1) % T::HeadersToKeep::get());
499+
<ImportedHashesPointer<T, I>>::put((index + 1) % headers_to_keep::<T, I>());
483500
if let Ok(hash) = pruning {
484501
log::debug!(target: LOG_TARGET, "Pruning old header: {:?}.", hash);
485502
<ImportedHeaders<T, I>>::remove(hash);
@@ -523,7 +540,7 @@ pub mod pallet {
523540
init_params: super::InitializationData<BridgedHeader<T, I>>,
524541
) {
525542
let start_number = *init_params.header.number();
526-
let end_number = start_number + T::HeadersToKeep::get().into();
543+
let end_number = start_number + headers_to_keep::<T, I>().into();
527544
initialize_bridge::<T, I>(init_params).expect("benchmarks are correct");
528545

529546
let mut number = start_number;

bridges/modules/grandpa/src/weights.rs

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,8 @@ impl<T: frame_system::Config> WeightInfo for BridgeWeight<T> {
7575
///
7676
/// Storage: BridgeRialtoGrandpa CurrentAuthoritySet (r:1 w:0)
7777
///
78-
/// Proof: BridgeRialtoGrandpa CurrentAuthoritySet (max_values: Some(1), max_size: Some(40970),
79-
/// added: 41465, mode: MaxEncodedLen)
78+
/// Proof: BridgeRialtoGrandpa CurrentAuthoritySet (max_values: Some(1), max_size: Some(209),
79+
/// added: 704, mode: MaxEncodedLen)
8080
///
8181
/// Storage: BridgeRialtoGrandpa ImportedHashesPointer (r:1 w:1)
8282
///
@@ -93,19 +93,19 @@ impl<T: frame_system::Config> WeightInfo for BridgeWeight<T> {
9393
/// Proof: BridgeRialtoGrandpa ImportedHeaders (max_values: None, max_size: Some(68), added:
9494
/// 2543, mode: MaxEncodedLen)
9595
///
96-
/// The range of component `p` is `[51, 102]`.
96+
/// The range of component `p` is `[1, 5]`.
9797
///
9898
/// The range of component `v` is `[50, 100]`.
9999
fn submit_finality_proof(p: u32, v: u32) -> Weight {
100100
// Proof Size summary in bytes:
101-
// Measured: `2524 + p * (40 ±0)`
102-
// Estimated: `46001`
103-
// Minimum execution time: 2_282_140 nanoseconds.
104-
Weight::from_parts(142_496_714, 46001)
105-
// Standard Error: 32_796
106-
.saturating_add(Weight::from_ref_time(40_232_935).saturating_mul(p.into()))
107-
// Standard Error: 33_574
108-
.saturating_add(Weight::from_ref_time(1_185_407).saturating_mul(v.into()))
101+
// Measured: `459 + p * (40 ±0)`
102+
// Estimated: `5240`
103+
// Minimum execution time: 368_734 nanoseconds.
104+
Weight::from_parts(64_214_587, 5240)
105+
// Standard Error: 226_504
106+
.saturating_add(Weight::from_ref_time(41_231_918).saturating_mul(p.into()))
107+
// Standard Error: 20_667
108+
.saturating_add(Weight::from_ref_time(2_770_962).saturating_mul(v.into()))
109109
.saturating_add(T::DbWeight::get().reads(6_u64))
110110
.saturating_add(T::DbWeight::get().writes(6_u64))
111111
}
@@ -130,8 +130,8 @@ impl WeightInfo for () {
130130
///
131131
/// Storage: BridgeRialtoGrandpa CurrentAuthoritySet (r:1 w:0)
132132
///
133-
/// Proof: BridgeRialtoGrandpa CurrentAuthoritySet (max_values: Some(1), max_size: Some(40970),
134-
/// added: 41465, mode: MaxEncodedLen)
133+
/// Proof: BridgeRialtoGrandpa CurrentAuthoritySet (max_values: Some(1), max_size: Some(209),
134+
/// added: 704, mode: MaxEncodedLen)
135135
///
136136
/// Storage: BridgeRialtoGrandpa ImportedHashesPointer (r:1 w:1)
137137
///
@@ -148,19 +148,19 @@ impl WeightInfo for () {
148148
/// Proof: BridgeRialtoGrandpa ImportedHeaders (max_values: None, max_size: Some(68), added:
149149
/// 2543, mode: MaxEncodedLen)
150150
///
151-
/// The range of component `p` is `[51, 102]`.
151+
/// The range of component `p` is `[1, 5]`.
152152
///
153153
/// The range of component `v` is `[50, 100]`.
154154
fn submit_finality_proof(p: u32, v: u32) -> Weight {
155155
// Proof Size summary in bytes:
156-
// Measured: `2524 + p * (40 ±0)`
157-
// Estimated: `46001`
158-
// Minimum execution time: 2_282_140 nanoseconds.
159-
Weight::from_parts(142_496_714, 46001)
160-
// Standard Error: 32_796
161-
.saturating_add(Weight::from_ref_time(40_232_935).saturating_mul(p.into()))
162-
// Standard Error: 33_574
163-
.saturating_add(Weight::from_ref_time(1_185_407).saturating_mul(v.into()))
156+
// Measured: `459 + p * (40 ±0)`
157+
// Estimated: `5240`
158+
// Minimum execution time: 368_734 nanoseconds.
159+
Weight::from_parts(64_214_587, 5240)
160+
// Standard Error: 226_504
161+
.saturating_add(Weight::from_ref_time(41_231_918).saturating_mul(p.into()))
162+
// Standard Error: 20_667
163+
.saturating_add(Weight::from_ref_time(2_770_962).saturating_mul(v.into()))
164164
.saturating_add(RocksDbWeight::get().reads(6_u64))
165165
.saturating_add(RocksDbWeight::get().writes(6_u64))
166166
}

0 commit comments

Comments
 (0)