v3.1: runtime: Collect stake delegations only once during epoch activation (backport of #8065)#9321
Merged
vadorovsky merged 1 commit intov3.1from Dec 2, 2025
Merged
Conversation
…8065) Processing new epoch (`Bank::process_new_epoch`) involves collecting stake delegations twice: 1) In `Bank::compute_new_epoch_caches_and_rewards`, to create a stake history entry and refresh vote accounts. 2) In `Bank::get_epoch_reward_calculate_param_info`, which is then used in `Bank::calculate_stake_vote_rewards` to calculate rewards for stakers and voters. The overall time of crossing the epoch boundary is ~519ms: ``` update_epoch_us=519953i ``` Where the two heaviest operations are `collect()`` calls on stake delegations, each of them taking ~200-220ms. Reduce that to just one collect by passing the vector 1) with freshly computed stake history and vote accounts to `Bank::begin_partitioned_rewards`. This way, we can avoid calling `Bank::get_epoch_reward_calculate_param_info`. The new time of crossing the epoch boundary is ~337ms: ``` update_epoch_us=337371i ``` Making that change possible required several refactors: * Tale `&PointValue` in `Bank::create_epoch_rewards_sysvar`. That makes it easier to operate on references of `PartitionedRewardsCalculation`. Copying integers from `PointValue` is cheap and has no visible performance impact. * Split `Stakes::activate_epoch`, that was performing calculations and mutating the cache at the same time. The calculations got split to `Stakes::calculate_activated_stake` that takes `&self`. * Add `Stakes::stake_delegations_ves` method. Stake delegations are stored as hash array mapped trie (HAMT)[0], which means that inserts, deletions and lookups are average-case O(1) and worst-case O(log n). However, the performance of iterations is poor due to depth-first traversal and jumps. Currently it's also impossible to iterate over it with rayon. That issue is known and handled by converting the HAMT to a vector with `stakes.stake_delegations.iter().collect()`. Move that trick to a dedicated method that describes the performance consequences. * Add `FilteredStakeDelegation` wrapper type, that wraps a vector of stake delegations and acts as a lazy iterator that filters out ones with insufficient stake. * Split the code dealing with rewards calculation and vote rewards distribution into separate methods: * `Bank::calculate_rewards` that takes `&self` and does not acquire any locks. * `Bank::begin_partitioned_rewards` that takes `&mut self`, sets calculation status and creates a sysvar. * `Bank::distribute_vote_rewards` that stores partitioned rewards and increases capitalization. [0] https://en.wikipedia.org/wiki/Hash_array_mapped_trie Fixes: #8282 (cherry picked from commit 3a2abd6)
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## v3.1 #9321 +/- ##
========================================
Coverage 83.2% 83.2%
========================================
Files 865 865
Lines 375603 375938 +335
========================================
+ Hits 312632 313003 +371
+ Misses 62971 62935 -36 🚀 New features to boost your workflow:
|
jstarry
approved these changes
Nov 28, 2025
t-nelson
approved these changes
Dec 1, 2025
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Processing new epoch (
Bank::process_new_epoch) involves collecting stake delegations twice:Stakes::activate_epoch, to create a stake history entry and refresh vote accounts.Bank::filter_stake_delegations, which is then used inBank::calculate_stake_vote_rewardsto calculate rewards for stakers and voters.The overall time of crossing the epoch boundary is ~519ms:
Where the two heaviest operations are
collect()calls on stake delegations, each of them taking ~200-220ms:Summary of Changes
Reduce that to just one collect to a
Vec<(&Pubkey, &StakeAccount)>done on the beginning ofBank::process_new_epochand passing the stake delegations to the other methods.The new time of crossing the epoch boundary is ~337ms:
There is only one heavy
collect()done on stake delegations, which still takes the most of main thread's time. But that's the best we can do while still usingim::HashMap.Making that change possible required several refactors:
&PointValueinBank::create_epoch_rewards_sysvar. That makes it easier to operate on references ofPartitionedRewardsCalculation. Copying integers fromPointValueis cheap and has no visibleperformance impact.
Stakes::activate_epoch, that was performing calculations and mutating the cache at the same time. The calculations got split toStakes::calculate_activated_stakethat takes&self.Stakes::stake_delegations_vesmethod. Stake delegations are stored as hash array mapped trie (HAMT)[0], which means that inserts, deletions and lookups are average-case O(1) and worst-case O(log n). However, the performance of iterations is poor due to depth-first traversal and jumps. Currently it's also impossible to iterate over it with rayon. That issue is known and handled by converting the HAMT to a vector withstakes.stake_delegations.iter().collect(). Move that trick to a dedicated method that describes the performance consequences.FilteredStakeDelegationwrapper type, that wraps a vector of stake delegations and acts as a lazy iterator that filters out ones with insufficient stake.Bank::calculate_rewardsthat takes&selfand does not acquire any locks.Bank::begin_partitioned_rewardsthat takes&mut self, sets calculation status and creates a sysvar.Bank::distribute_vote_rewardsthat stores partitioned rewards and increases capitalization.[0] https://en.wikipedia.org/wiki/Hash_array_mapped_trie
Fixes: #8282
This is an automatic backport of pull request #8065 done by [Mergify](https://mergify.com).