diff --git a/prdoc/pr_11156.prdoc b/prdoc/pr_11156.prdoc new file mode 100644 index 0000000000000..cf7036bbd24ac --- /dev/null +++ b/prdoc/pr_11156.prdoc @@ -0,0 +1,10 @@ +title: 'election-provider-multi-block: reduce overclaimed proof_size during Signed + phase' +doc: +- audience: Runtime Dev + description: |- + on_initialize claimed on_initialize_into_signed weight (> ~3M proof_size) every block during the Signed phase, but the heavy work (loading voter snapshots) only happens once when transitioning from Snapshot into Signed. + Use discriminant comparison to distinguish phase entry from same-phase ticks, falling back to on_initialize_nothing for the latter. +crates: +- name: pallet-election-provider-multi-block + bump: patch diff --git a/substrate/frame/election-provider-multi-block/src/lib.rs b/substrate/frame/election-provider-multi-block/src/lib.rs index bd956cba23595..c6cfd97cff0b8 100644 --- a/substrate/frame/election-provider-multi-block/src/lib.rs +++ b/substrate/frame/election-provider-multi-block/src/lib.rs @@ -728,12 +728,25 @@ pub mod pallet { if !matches!(current_phase, Phase::Export(_)) { let next_phase = current_phase.next(); - let weight2 = match next_phase { - Phase::Signed(_) => T::WeightInfo::on_initialize_into_signed(), - Phase::SignedValidation(_) => - T::WeightInfo::on_initialize_into_signed_validation(), - Phase::Unsigned(_) => T::WeightInfo::on_initialize_into_unsigned(), - _ => T::WeightInfo::on_initialize_nothing(), + let weight2 = { + use sp_std::mem::discriminant; + if discriminant(¤t_phase) != discriminant(&next_phase) { + // Claim the heavy phase-entry weight only when the + // discriminant actually changes (e.g. Snapshot -> Signed). + // Ticks within the same phase (e.g. Signed(N) -> Signed(N-1)) + // only write CurrentPhase. + // For Snapshot blocks the real cost is already accounted for by weight1 + // above. + match next_phase { + Phase::Signed(_) => T::WeightInfo::on_initialize_into_signed(), + Phase::SignedValidation(_) => + T::WeightInfo::on_initialize_into_signed_validation(), + Phase::Unsigned(_) => T::WeightInfo::on_initialize_into_unsigned(), + _ => T::WeightInfo::on_initialize_nothing(), + } + } else { + T::WeightInfo::on_initialize_nothing() + } }; Self::phase_transition(next_phase);