Skip to content

Commit

Permalink
moves new_warmup_cooldown_rate_epoch outside iterators and for loops (#…
Browse files Browse the repository at this point in the history
…33259)

Recalculating new_warmup_cooldown_rate_epoch for each item is redundant
and wasteful and instead can be done only once outside the iterators and
for loops.
Also NewWarmupCooldownRateEpoch is unnecessary and verbose and is
removed in this commit.
  • Loading branch information
behzadnouri authored Sep 15, 2023
1 parent dfaec78 commit c1090d3
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 27 deletions.
2 changes: 1 addition & 1 deletion programs/stake/src/stake_instruction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -481,7 +481,7 @@ mod tests {
account_utils::StateMut,
clock::{Epoch, UnixTimestamp},
epoch_schedule::EpochSchedule,
feature_set::{reduce_stake_warmup_cooldown::NewWarmupCooldownRateEpoch, FeatureSet},
feature_set::FeatureSet,
instruction::{AccountMeta, Instruction},
pubkey::Pubkey,
rent::Rent,
Expand Down
5 changes: 1 addition & 4 deletions programs/stake/src/stake_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,7 @@ use {
account::{AccountSharedData, ReadableAccount, WritableAccount},
account_utils::StateMut,
clock::{Clock, Epoch},
feature_set::{
self, reduce_stake_warmup_cooldown::NewWarmupCooldownRateEpoch,
stake_merge_with_unmatched_credits_observed, FeatureSet,
},
feature_set::{self, stake_merge_with_unmatched_credits_observed, FeatureSet},
instruction::{checked_add, InstructionError},
pubkey::Pubkey,
rent::Rent,
Expand Down
24 changes: 13 additions & 11 deletions runtime/src/bank.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,6 @@ use {
self, add_set_tx_loaded_accounts_data_size_instruction,
enable_early_verification_of_account_modifications,
include_loaded_accounts_data_size_in_fee_calculation,
reduce_stake_warmup_cooldown::NewWarmupCooldownRateEpoch,
remove_congestion_multiplier_from_fee_calculation, remove_deprecated_request_unit_ix,
FeatureSet,
},
Expand Down Expand Up @@ -2986,6 +2985,7 @@ impl Bank {
VoteAccount::try_from(account).ok()
};

let new_warmup_cooldown_rate_epoch = self.new_warmup_cooldown_rate_epoch();
let (points, measure_us) = measure_us!(thread_pool.install(|| {
stake_delegations
.par_iter()
Expand All @@ -3007,7 +3007,7 @@ impl Bank {
stake_account.stake_state(),
vote_state,
Some(stake_history),
self.new_warmup_cooldown_rate_epoch(),
new_warmup_cooldown_rate_epoch,
)
.unwrap_or(0)
})
Expand All @@ -3026,6 +3026,7 @@ impl Bank {
thread_pool: &ThreadPool,
metrics: &RewardsMetrics,
) -> Option<PointValue> {
let new_warmup_cooldown_rate_epoch = self.new_warmup_cooldown_rate_epoch();
let (points, measure) = measure!(thread_pool.install(|| {
vote_with_stake_delegations_map
.par_iter()
Expand All @@ -3043,7 +3044,7 @@ impl Bank {
stake_account.stake_state(),
vote_state,
Some(stake_history),
self.new_warmup_cooldown_rate_epoch(),
new_warmup_cooldown_rate_epoch,
)
.unwrap_or(0)
})
Expand Down Expand Up @@ -3089,6 +3090,7 @@ impl Bank {
VoteAccount::try_from(account).ok()
};

let new_warmup_cooldown_rate_epoch = self.new_warmup_cooldown_rate_epoch();
let vote_account_rewards: VoteRewards = DashMap::new();
let total_stake_rewards = AtomicU64::default();
let (stake_rewards, measure_stake_rewards_us) = measure_us!(thread_pool.install(|| {
Expand Down Expand Up @@ -3130,7 +3132,7 @@ impl Bank {
&point_value,
Some(stake_history),
reward_calc_tracer.as_ref(),
self.new_warmup_cooldown_rate_epoch(),
new_warmup_cooldown_rate_epoch,
);

let post_lamport = stake_account.lamports();
Expand Down Expand Up @@ -3202,6 +3204,7 @@ impl Bank {
reward_calc_tracer: Option<impl RewardCalcTracer>,
metrics: &mut RewardsMetrics,
) -> (VoteRewards, StakeRewards) {
let new_warmup_cooldown_rate_epoch = self.new_warmup_cooldown_rate_epoch();
let vote_account_rewards: VoteRewards =
DashMap::with_capacity(vote_with_stake_delegations_map.len());
let stake_delegation_iterator = vote_with_stake_delegations_map.into_par_iter().flat_map(
Expand Down Expand Up @@ -3248,7 +3251,7 @@ impl Bank {
&point_value,
Some(stake_history),
reward_calc_tracer.as_ref(),
self.new_warmup_cooldown_rate_epoch(),
new_warmup_cooldown_rate_epoch,
);
if let Ok((stakers_reward, voters_reward)) = redeemed {
// track voter rewards
Expand Down Expand Up @@ -6604,11 +6607,12 @@ impl Bank {
) {
assert!(!self.freeze_started());
let mut m = Measure::start("stakes_cache.check_and_store");
let new_warmup_cooldown_rate_epoch = self.new_warmup_cooldown_rate_epoch();
(0..accounts.len()).for_each(|i| {
self.stakes_cache.check_and_store(
accounts.pubkey(i),
accounts.account(i),
self.new_warmup_cooldown_rate_epoch(),
new_warmup_cooldown_rate_epoch,
)
});
self.rc.accounts.store_accounts_cached(accounts);
Expand Down Expand Up @@ -7730,6 +7734,7 @@ impl Bank {
) {
debug_assert_eq!(txs.len(), execution_results.len());
debug_assert_eq!(txs.len(), loaded_txs.len());
let new_warmup_cooldown_rate_epoch = self.new_warmup_cooldown_rate_epoch();
izip!(txs, execution_results, loaded_txs)
.filter(|(_, execution_result, _)| execution_result.was_executed_successfully())
.flat_map(|(tx, _, (load_result, _))| {
Expand All @@ -7741,11 +7746,8 @@ impl Bank {
.for_each(|(pubkey, account)| {
// note that this could get timed to: self.rc.accounts.accounts_db.stats.stakes_cache_check_and_store_us,
// but this code path is captured separately in ExecuteTimingType::UpdateStakesCacheUs
self.stakes_cache.check_and_store(
pubkey,
account,
self.new_warmup_cooldown_rate_epoch(),
);
self.stakes_cache
.check_and_store(pubkey, account, new_warmup_cooldown_rate_epoch);
});
}

Expand Down
17 changes: 6 additions & 11 deletions sdk/src/feature_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use {
lazy_static::lazy_static,
solana_program::{epoch_schedule::EpochSchedule, stake_history::Epoch},
solana_sdk::{
clock::Slot,
hash::{Hash, Hasher},
Expand Down Expand Up @@ -665,18 +666,7 @@ pub mod last_restart_slot_sysvar {
}

pub mod reduce_stake_warmup_cooldown {
use solana_program::{epoch_schedule::EpochSchedule, stake_history::Epoch};
solana_sdk::declare_id!("GwtDQBghCTBgmX2cpEGNPxTEBUTQRaDMGTr5qychdGMj");

pub trait NewWarmupCooldownRateEpoch {
fn new_warmup_cooldown_rate_epoch(&self, epoch_schedule: &EpochSchedule) -> Option<Epoch>;
}
impl NewWarmupCooldownRateEpoch for super::FeatureSet {
fn new_warmup_cooldown_rate_epoch(&self, epoch_schedule: &EpochSchedule) -> Option<Epoch> {
self.activated_slot(&id())
.map(|slot| epoch_schedule.get_epoch(slot))
}
}
}

pub mod revise_turbine_epoch_stakes {
Expand Down Expand Up @@ -960,6 +950,11 @@ impl FeatureSet {
self.active.remove(feature_id);
self.inactive.insert(*feature_id);
}

pub fn new_warmup_cooldown_rate_epoch(&self, epoch_schedule: &EpochSchedule) -> Option<Epoch> {
self.activated_slot(&reduce_stake_warmup_cooldown::id())
.map(|slot| epoch_schedule.get_epoch(slot))
}
}

#[cfg(test)]
Expand Down

0 comments on commit c1090d3

Please sign in to comment.