Skip to content

Commit

Permalink
Potential staking optimization: avoid decoding Vecs (#2414) (#2484)
Browse files Browse the repository at this point in the history
* Use decode_len to improve performance

* Fix the benchmarking test

* Fix the benchmark test

* Changes after review comments
  • Loading branch information
Cem Eliguzel authored Sep 11, 2023
1 parent acbd4e4 commit 375c4af
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
6 changes: 5 additions & 1 deletion pallets/parachain-staking/src/auto_compound.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ where
Self(sorted_delegations)
}

pub fn get_auto_compounding_delegation_count(candidate: &T::AccountId) -> usize {
<AutoCompoundingDelegationsStorage<T>>::decode_len(candidate).unwrap_or_default()
}

/// Retrieves an instance of [AutoCompoundingDelegations] storage as [AutoCompoundDelegations].
pub fn get_storage(candidate: &T::AccountId) -> Self {
Self(<AutoCompoundingDelegationsStorage<T>>::get(candidate))
Expand Down Expand Up @@ -205,7 +209,7 @@ where

if !auto_compound.is_zero() {
ensure!(
Self::get_storage(&candidate).len()
Self::get_auto_compounding_delegation_count(&candidate) as u32
<= candidate_auto_compounding_delegation_count_hint,
<Error<T>>::TooLowCandidateAutoCompoundingDelegationCountToDelegate,
);
Expand Down
9 changes: 5 additions & 4 deletions pallets/parachain-staking/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1512,11 +1512,11 @@ pub mod pallet {
candidate: T::AccountId,
) -> DispatchResultWithPostInfo {
let state = <CandidateInfo<T>>::get(&candidate).ok_or(Error::<T>::CandidateDNE)?;
let ac_state = <AutoCompoundingDelegations<T>>::get(&candidate);
let actual_auto_compound_delegation_count =
<AutoCompoundingDelegations<T>>::decode_len(&candidate).unwrap_or_default() as u32;

// TODO use these to return actual weight used via `execute_leave_candidates_ideal`
let actual_delegation_count = state.delegation_count;
let actual_auto_compound_delegation_count = ac_state.len() as u32;
let actual_weight = T::WeightInfo::execute_leave_candidates_ideal(
actual_delegation_count,
actual_auto_compound_delegation_count,
Expand Down Expand Up @@ -1788,7 +1788,8 @@ pub mod pallet {
let num_delegators = state.delegations.len();
let mut num_paid_delegations = 0u32;
let mut num_auto_compounding = 0u32;
let num_scheduled_requests = <DelegationScheduledRequests<T>>::get(&collator).len();
let num_scheduled_requests =
<DelegationScheduledRequests<T>>::decode_len(&collator).unwrap_or_default();
if state.delegations.is_empty() {
// solo collator with no delegators
extra_weight = extra_weight
Expand Down Expand Up @@ -2057,7 +2058,7 @@ pub mod pallet {
);

let actual_weight = T::WeightInfo::delegator_bond_more(
<DelegationScheduledRequests<T>>::get(&candidate).len() as u32,
<DelegationScheduledRequests<T>>::decode_len(&candidate).unwrap_or_default() as u32,
);
let in_top = state
.increase_delegation::<T>(candidate.clone(), more)
Expand Down

0 comments on commit 375c4af

Please sign in to comment.