Skip to content

Commit

Permalink
short-term fix for para inherent weight overestimation (#5082)
Browse files Browse the repository at this point in the history
closes #849

## Context

For the background on this and the long-term fix, see
#849 (comment).

## Changes

* The weigh files are renamed from `runtime_(parachains|common).*` to
`polkadot_runtime_(parachains|common).*`. The reason for it is the
renaming introduced in #4633. The new weight command and files are
generated now include `polkadot_` prefix.
* The WeightInfo for `paras_inherent` now includes `enter_empty` which
calculates the cost of processing an empty parachains inherent. This
cost is subtracted dynamically when calculating other weights (so the
other weights remain the same)

## Benefits

See
#849 (comment),
but TL;DR is that we are not blocked on weights for scaling the number
of validators and cores further.

Resolved questions:
- [x] why new benchmarks for westend are doing fewer db IOPS?
Is it due polkadot-sdk update (db IOPS diff)?
or the bench setup is no longer valid?

https://github.com/polkadot-fellows/runtimes/blob/7723274a2c5cbb10213379271094d5180716ca7d/relay/polkadot/src/weights/runtime_parachains_paras_inherent.rs#L131-L196
Answer: see background section of #5270 

TODOs:
- [x] Rerun benchmarks for Rococo and Westend
- [x] PRDoc

---------

Co-authored-by: command-bot <>
  • Loading branch information
ordian authored Aug 29, 2024
1 parent 5620196 commit cc7ebe0
Show file tree
Hide file tree
Showing 42 changed files with 270 additions and 207 deletions.
15 changes: 8 additions & 7 deletions polkadot/runtime/parachains/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,9 +221,10 @@ impl<T: paras_inherent::Config> BenchBuilder<T> {
.expect("self.block_number is u32")
}

/// Maximum number of validators that may be part of a validator group.
/// Fallback for the maximum number of validators participating in parachains consensus (a.k.a.
/// active validators).
pub(crate) fn fallback_max_validators() -> u32 {
configuration::ActiveConfig::<T>::get().max_validators.unwrap_or(200)
configuration::ActiveConfig::<T>::get().max_validators.unwrap_or(1024)
}

/// Maximum number of validators participating in parachains consensus (a.k.a. active
Expand Down Expand Up @@ -285,8 +286,8 @@ impl<T: paras_inherent::Config> BenchBuilder<T> {

/// Get the minimum number of validity votes in order for a backed candidate to be included.
#[cfg(feature = "runtime-benchmarks")]
pub(crate) fn fallback_min_validity_votes() -> u32 {
(Self::fallback_max_validators() / 2) + 1
pub(crate) fn fallback_min_backing_votes() -> u32 {
2
}

fn mock_head_data() -> HeadData {
Expand Down Expand Up @@ -356,11 +357,11 @@ impl<T: paras_inherent::Config> BenchBuilder<T> {
availability_votes,
commitments,
);
inclusion::PendingAvailability::<T>::mutate(para_id, |maybe_andidates| {
if let Some(candidates) = maybe_andidates {
inclusion::PendingAvailability::<T>::mutate(para_id, |maybe_candidates| {
if let Some(candidates) = maybe_candidates {
candidates.push_back(candidate_availability);
} else {
*maybe_andidates =
*maybe_candidates =
Some([candidate_availability].into_iter().collect::<VecDeque<_>>());
}
});
Expand Down
37 changes: 21 additions & 16 deletions polkadot/runtime/parachains/src/paras_inherent/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,20 @@ use polkadot_primitives::v7::GroupIndex;
use crate::builder::BenchBuilder;

benchmarks! {
enter_empty {
let scenario = BenchBuilder::<T>::new()
.build();

let mut benchmark = scenario.data.clone();

benchmark.bitfields.clear();
benchmark.backed_candidates.clear();
benchmark.disputes.clear();
}: enter(RawOrigin::None, benchmark)
verify {
// Assert that the block was not discarded
assert!(Included::<T>::get().is_some());
}
// Variant over `v`, the number of dispute statements in a dispute statement set. This gives the
// weight of a single dispute statement set.
enter_variable_disputes {
Expand Down Expand Up @@ -92,18 +106,8 @@ benchmarks! {
// Variant over `v`, the amount of validity votes for a backed candidate. This gives the weight
// of a single backed candidate.
enter_backed_candidates_variable {
// NOTE: the starting value must be over half of the max validators per group so the backed
// candidate is not rejected. Also, we cannot have more validity votes than validators in
// the group.

// Do not use this range for Rococo because it only has 1 validator per backing group,
// which causes issues when trying to create slopes with the benchmarking analysis. Instead
// use v = 1 for running Rococo benchmarks
let v in (BenchBuilder::<T>::fallback_min_validity_votes())
..(BenchBuilder::<T>::fallback_max_validators());

// Comment in for running rococo benchmarks
// let v = 1;
let v in (BenchBuilder::<T>::fallback_min_backing_votes())
..(BenchBuilder::<T>::fallback_max_validators_per_core());

let cores_with_backed: BTreeMap<_, _>
= vec![(0, v)] // The backed candidate will have `v` validity votes.
Expand All @@ -119,7 +123,6 @@ benchmarks! {
// There is 1 backed,
assert_eq!(benchmark.backed_candidates.len(), 1);
// with `v` validity votes.
// let votes = v as usize;
let votes = min(scheduler::Pallet::<T>::group_validators(GroupIndex::from(0)).unwrap().len(), v as usize);
assert_eq!(benchmark.backed_candidates.get(0).unwrap().validity_votes().len(), votes);

Expand Down Expand Up @@ -157,7 +160,7 @@ benchmarks! {
let v = crate::configuration::ActiveConfig::<T>::get().max_code_size;

let cores_with_backed: BTreeMap<_, _>
= vec![(0, BenchBuilder::<T>::fallback_min_validity_votes())]
= vec![(0, BenchBuilder::<T>::fallback_min_backing_votes())]
.into_iter()
.collect();

Expand All @@ -168,8 +171,10 @@ benchmarks! {

let mut benchmark = scenario.data.clone();

// let votes = BenchBuilder::<T>::fallback_min_validity_votes() as usize;
let votes = min(scheduler::Pallet::<T>::group_validators(GroupIndex::from(0)).unwrap().len(), BenchBuilder::<T>::fallback_min_validity_votes() as usize);
let votes = min(
scheduler::Pallet::<T>::group_validators(GroupIndex::from(0)).unwrap().len(),
BenchBuilder::<T>::fallback_min_backing_votes() as usize
);

// There is 1 backed
assert_eq!(benchmark.backed_candidates.len(), 1);
Expand Down
18 changes: 15 additions & 3 deletions polkadot/runtime/parachains/src/paras_inherent/weights.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ use polkadot_primitives::{
use super::{BackedCandidate, Config, DisputeStatementSet, Weight};

pub trait WeightInfo {
/// The weight of processing an empty parachain inherent.
fn enter_empty() -> Weight;
/// Variant over `v`, the count of dispute statements in a dispute statement set. This gives the
/// weight of a single dispute statement set.
fn enter_variable_disputes(v: u32) -> Weight;
Expand All @@ -45,6 +47,9 @@ pub struct TestWeightInfo;
// mock.
#[cfg(not(feature = "runtime-benchmarks"))]
impl WeightInfo for TestWeightInfo {
fn enter_empty() -> Weight {
Weight::zero()
}
fn enter_variable_disputes(v: u32) -> Weight {
// MAX Block Weight should fit 4 disputes
Weight::from_parts(80_000 * v as u64 + 80_000, 0)
Expand All @@ -66,6 +71,9 @@ impl WeightInfo for TestWeightInfo {
// running as a test.
#[cfg(feature = "runtime-benchmarks")]
impl WeightInfo for TestWeightInfo {
fn enter_empty() -> Weight {
Weight::zero()
}
fn enter_variable_disputes(_v: u32) -> Weight {
Weight::zero()
}
Expand Down Expand Up @@ -123,7 +131,8 @@ where
set_proof_size_to_tx_size(
<<T as Config>::WeightInfo as WeightInfo>::enter_variable_disputes(
statement_set.as_ref().statements.len() as u32,
),
)
.saturating_sub(<<T as Config>::WeightInfo as WeightInfo>::enter_empty()),
statement_set,
)
}
Expand All @@ -133,14 +142,16 @@ pub fn signed_bitfields_weight<T: Config>(
) -> Weight {
set_proof_size_to_tx_size(
<<T as Config>::WeightInfo as WeightInfo>::enter_bitfields()
.saturating_sub(<<T as Config>::WeightInfo as WeightInfo>::enter_empty())
.saturating_mul(bitfields.len() as u64),
bitfields,
)
}

pub fn signed_bitfield_weight<T: Config>(bitfield: &UncheckedSignedAvailabilityBitfield) -> Weight {
set_proof_size_to_tx_size(
<<T as Config>::WeightInfo as WeightInfo>::enter_bitfields(),
<<T as Config>::WeightInfo as WeightInfo>::enter_bitfields()
.saturating_sub(<<T as Config>::WeightInfo as WeightInfo>::enter_empty()),
bitfield,
)
}
Expand All @@ -155,7 +166,8 @@ pub fn backed_candidate_weight<T: frame_system::Config + Config>(
<<T as Config>::WeightInfo as WeightInfo>::enter_backed_candidates_variable(
candidate.validity_votes().len() as u32,
)
},
}
.saturating_sub(<<T as Config>::WeightInfo as WeightInfo>::enter_empty()),
candidate,
)
}
Expand Down
2 changes: 1 addition & 1 deletion polkadot/runtime/rococo/src/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ where
fn on_reap_identity(who: &AccountId, fields: u32, subs: u32) -> DispatchResult {
use crate::{
impls::IdentityMigratorCalls::PokeDeposit,
weights::runtime_common_identity_migrator::WeightInfo as MigratorWeights,
weights::polkadot_runtime_common_identity_migrator::WeightInfo as MigratorWeights,
};

let total_to_send = Self::calculate_remote_deposit(fields, subs);
Expand Down
32 changes: 16 additions & 16 deletions polkadot/runtime/rococo/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -674,7 +674,7 @@ impl claims::Config for Runtime {
type VestingSchedule = Vesting;
type Prefix = Prefix;
type MoveClaimOrigin = EnsureRoot<AccountId>;
type WeightInfo = weights::runtime_common_claims::WeightInfo<Runtime>;
type WeightInfo = weights::polkadot_runtime_common_claims::WeightInfo<Runtime>;
}

parameter_types! {
Expand Down Expand Up @@ -940,7 +940,7 @@ impl pallet_proxy::Config for Runtime {
impl parachains_origin::Config for Runtime {}

impl parachains_configuration::Config for Runtime {
type WeightInfo = weights::runtime_parachains_configuration::WeightInfo<Runtime>;
type WeightInfo = weights::polkadot_runtime_parachains_configuration::WeightInfo<Runtime>;
}

impl parachains_shared::Config for Runtime {
Expand All @@ -963,7 +963,7 @@ impl parachains_inclusion::Config for Runtime {
type DisputesHandler = ParasDisputes;
type RewardValidators = RewardValidators;
type MessageQueue = MessageQueue;
type WeightInfo = weights::runtime_parachains_inclusion::WeightInfo<Runtime>;
type WeightInfo = weights::polkadot_runtime_parachains_inclusion::WeightInfo<Runtime>;
}

parameter_types! {
Expand All @@ -972,7 +972,7 @@ parameter_types! {

impl parachains_paras::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type WeightInfo = weights::runtime_parachains_paras::WeightInfo<Runtime>;
type WeightInfo = weights::polkadot_runtime_parachains_paras::WeightInfo<Runtime>;
type UnsignedPriority = ParasUnsignedPriority;
type QueueFootprinter = ParaInclusion;
type NextSessionRotation = Babe;
Expand Down Expand Up @@ -1046,11 +1046,11 @@ impl parachains_hrmp::Config for Runtime {
HrmpChannelSizeAndCapacityWithSystemRatio,
>;
type VersionWrapper = crate::XcmPallet;
type WeightInfo = weights::runtime_parachains_hrmp::WeightInfo<Runtime>;
type WeightInfo = weights::polkadot_runtime_parachains_hrmp::WeightInfo<Runtime>;
}

impl parachains_paras_inherent::Config for Runtime {
type WeightInfo = weights::runtime_parachains_paras_inherent::WeightInfo<Runtime>;
type WeightInfo = weights::polkadot_runtime_parachains_paras_inherent::WeightInfo<Runtime>;
}

impl parachains_scheduler::Config for Runtime {
Expand Down Expand Up @@ -1079,7 +1079,7 @@ impl coretime::Config for Runtime {
type Currency = Balances;
type BrokerId = BrokerId;
type BrokerPotLocation = BrokerPot;
type WeightInfo = weights::runtime_parachains_coretime::WeightInfo<Runtime>;
type WeightInfo = weights::polkadot_runtime_parachains_coretime::WeightInfo<Runtime>;
type SendXcm = crate::xcm_config::XcmRouter;
type AssetTransactor = crate::xcm_config::LocalAssetTransactor;
type AccountToLocation = xcm_builder::AliasesIntoAccountId32<
Expand All @@ -1100,7 +1100,7 @@ impl parachains_on_demand::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type Currency = Balances;
type TrafficDefaultValue = OnDemandTrafficDefaultValue;
type WeightInfo = weights::runtime_parachains_on_demand::WeightInfo<Runtime>;
type WeightInfo = weights::polkadot_runtime_parachains_on_demand::WeightInfo<Runtime>;
type MaxHistoricalRevenue = MaxHistoricalRevenue;
type PalletId = OnDemandPalletId;
}
Expand All @@ -1110,15 +1110,15 @@ impl parachains_assigner_coretime::Config for Runtime {}
impl parachains_initializer::Config for Runtime {
type Randomness = pallet_babe::RandomnessFromOneEpochAgo<Runtime>;
type ForceOrigin = EnsureRoot<AccountId>;
type WeightInfo = weights::runtime_parachains_initializer::WeightInfo<Runtime>;
type WeightInfo = weights::polkadot_runtime_parachains_initializer::WeightInfo<Runtime>;
type CoretimeOnNewSession = Coretime;
}

impl parachains_disputes::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type RewardValidators = ();
type SlashingHandler = parachains_slashing::SlashValidatorsForDisputes<ParasSlashing>;
type WeightInfo = weights::runtime_parachains_disputes::WeightInfo<Runtime>;
type WeightInfo = weights::polkadot_runtime_parachains_disputes::WeightInfo<Runtime>;
}

impl parachains_slashing::Config for Runtime {
Expand Down Expand Up @@ -1149,7 +1149,7 @@ impl paras_registrar::Config for Runtime {
type OnSwap = (Crowdloan, Slots, SwapLeases);
type ParaDeposit = ParaDeposit;
type DataDepositPerByte = DataDepositPerByte;
type WeightInfo = weights::runtime_common_paras_registrar::WeightInfo<Runtime>;
type WeightInfo = weights::polkadot_runtime_common_paras_registrar::WeightInfo<Runtime>;
}

parameter_types! {
Expand All @@ -1163,7 +1163,7 @@ impl slots::Config for Runtime {
type LeasePeriod = LeasePeriod;
type LeaseOffset = ();
type ForceOrigin = EitherOf<EnsureRoot<Self::AccountId>, LeaseAdmin>;
type WeightInfo = weights::runtime_common_slots::WeightInfo<Runtime>;
type WeightInfo = weights::polkadot_runtime_common_slots::WeightInfo<Runtime>;
}

parameter_types! {
Expand All @@ -1184,7 +1184,7 @@ impl crowdloan::Config for Runtime {
type Registrar = Registrar;
type Auctioneer = Auctions;
type MaxMemoLength = MaxMemoLength;
type WeightInfo = weights::runtime_common_crowdloan::WeightInfo<Runtime>;
type WeightInfo = weights::polkadot_runtime_common_crowdloan::WeightInfo<Runtime>;
}

parameter_types! {
Expand All @@ -1203,14 +1203,14 @@ impl auctions::Config for Runtime {
type SampleLength = SampleLength;
type Randomness = pallet_babe::RandomnessFromOneEpochAgo<Runtime>;
type InitiateOrigin = EitherOf<EnsureRoot<Self::AccountId>, AuctionAdmin>;
type WeightInfo = weights::runtime_common_auctions::WeightInfo<Runtime>;
type WeightInfo = weights::polkadot_runtime_common_auctions::WeightInfo<Runtime>;
}

impl identity_migrator::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type Reaper = EnsureSigned<AccountId>;
type ReapIdentityHandler = ToParachainIdentityReaper<Runtime, Self::AccountId>;
type WeightInfo = weights::runtime_common_identity_migrator::WeightInfo<Runtime>;
type WeightInfo = weights::polkadot_runtime_common_identity_migrator::WeightInfo<Runtime>;
}

type NisCounterpartInstance = pallet_balances::Instance2;
Expand Down Expand Up @@ -1353,7 +1353,7 @@ impl assigned_slots::Config for Runtime {
type PermanentSlotLeasePeriodLength = PermanentSlotLeasePeriodLength;
type TemporarySlotLeasePeriodLength = TemporarySlotLeasePeriodLength;
type MaxTemporarySlotPerLeasePeriod = MaxTemporarySlotPerLeasePeriod;
type WeightInfo = weights::runtime_common_assigned_slots::WeightInfo<Runtime>;
type WeightInfo = weights::polkadot_runtime_common_assigned_slots::WeightInfo<Runtime>;
}

impl validator_manager::Config for Runtime {
Expand Down
32 changes: 16 additions & 16 deletions polkadot/runtime/rococo/src/weights/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,20 +44,20 @@ pub mod pallet_utility;
pub mod pallet_vesting;
pub mod pallet_whitelist;
pub mod pallet_xcm;
pub mod runtime_common_assigned_slots;
pub mod runtime_common_auctions;
pub mod runtime_common_claims;
pub mod runtime_common_crowdloan;
pub mod runtime_common_identity_migrator;
pub mod runtime_common_paras_registrar;
pub mod runtime_common_slots;
pub mod runtime_parachains_configuration;
pub mod runtime_parachains_coretime;
pub mod runtime_parachains_disputes;
pub mod runtime_parachains_hrmp;
pub mod runtime_parachains_inclusion;
pub mod runtime_parachains_initializer;
pub mod runtime_parachains_on_demand;
pub mod runtime_parachains_paras;
pub mod runtime_parachains_paras_inherent;
pub mod polkadot_runtime_common_assigned_slots;
pub mod polkadot_runtime_common_auctions;
pub mod polkadot_runtime_common_claims;
pub mod polkadot_runtime_common_crowdloan;
pub mod polkadot_runtime_common_identity_migrator;
pub mod polkadot_runtime_common_paras_registrar;
pub mod polkadot_runtime_common_slots;
pub mod polkadot_runtime_parachains_configuration;
pub mod polkadot_runtime_parachains_coretime;
pub mod polkadot_runtime_parachains_disputes;
pub mod polkadot_runtime_parachains_hrmp;
pub mod polkadot_runtime_parachains_inclusion;
pub mod polkadot_runtime_parachains_initializer;
pub mod polkadot_runtime_parachains_on_demand;
pub mod polkadot_runtime_parachains_paras;
pub mod polkadot_runtime_parachains_paras_inherent;
pub mod xcm;
Loading

0 comments on commit cc7ebe0

Please sign in to comment.