Commit baa1a55
committed
runtime: Collect stake delegations only once during epoch activation
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_trie1 parent 222485e commit baa1a55
9 files changed
Lines changed: 396 additions & 183 deletions
File tree
- runtime/src
- bank
- partitioned_epoch_rewards
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
47 | 47 | | |
48 | 48 | | |
49 | 49 | | |
50 | | - | |
| 50 | + | |
51 | 51 | | |
52 | 52 | | |
53 | 53 | | |
| |||
112 | 112 | | |
113 | 113 | | |
114 | 114 | | |
115 | | - | |
116 | 115 | | |
117 | 116 | | |
118 | 117 | | |
| |||
167 | 166 | | |
168 | 167 | | |
169 | 168 | | |
170 | | - | |
| 169 | + | |
171 | 170 | | |
172 | 171 | | |
173 | 172 | | |
| |||
1050 | 1049 | | |
1051 | 1050 | | |
1052 | 1051 | | |
| 1052 | + | |
| 1053 | + | |
| 1054 | + | |
| 1055 | + | |
| 1056 | + | |
| 1057 | + | |
| 1058 | + | |
| 1059 | + | |
1053 | 1060 | | |
1054 | 1061 | | |
1055 | 1062 | | |
| |||
1591 | 1598 | | |
1592 | 1599 | | |
1593 | 1600 | | |
| 1601 | + | |
| 1602 | + | |
| 1603 | + | |
| 1604 | + | |
| 1605 | + | |
| 1606 | + | |
| 1607 | + | |
| 1608 | + | |
| 1609 | + | |
| 1610 | + | |
| 1611 | + | |
| 1612 | + | |
| 1613 | + | |
| 1614 | + | |
| 1615 | + | |
| 1616 | + | |
| 1617 | + | |
| 1618 | + | |
| 1619 | + | |
| 1620 | + | |
| 1621 | + | |
| 1622 | + | |
| 1623 | + | |
| 1624 | + | |
| 1625 | + | |
| 1626 | + | |
| 1627 | + | |
| 1628 | + | |
| 1629 | + | |
| 1630 | + | |
| 1631 | + | |
| 1632 | + | |
| 1633 | + | |
| 1634 | + | |
| 1635 | + | |
| 1636 | + | |
| 1637 | + | |
| 1638 | + | |
| 1639 | + | |
| 1640 | + | |
| 1641 | + | |
1594 | 1642 | | |
1595 | 1643 | | |
1596 | 1644 | | |
| |||
1610 | 1658 | | |
1611 | 1659 | | |
1612 | 1660 | | |
1613 | | - | |
1614 | | - | |
1615 | | - | |
1616 | | - | |
1617 | | - | |
| 1661 | + | |
| 1662 | + | |
| 1663 | + | |
| 1664 | + | |
| 1665 | + | |
| 1666 | + | |
| 1667 | + | |
| 1668 | + | |
1618 | 1669 | | |
1619 | | - | |
1620 | | - | |
| 1670 | + | |
| 1671 | + | |
| 1672 | + | |
| 1673 | + | |
| 1674 | + | |
| 1675 | + | |
| 1676 | + | |
1621 | 1677 | | |
1622 | 1678 | | |
1623 | 1679 | | |
1624 | 1680 | | |
1625 | 1681 | | |
1626 | 1682 | | |
1627 | | - | |
1628 | | - | |
1629 | | - | |
1630 | | - | |
1631 | | - | |
1632 | | - | |
1633 | | - | |
1634 | | - | |
1635 | | - | |
1636 | | - | |
1637 | | - | |
| 1683 | + | |
| 1684 | + | |
| 1685 | + | |
| 1686 | + | |
| 1687 | + | |
| 1688 | + | |
| 1689 | + | |
| 1690 | + | |
| 1691 | + | |
1638 | 1692 | | |
1639 | 1693 | | |
1640 | 1694 | | |
| |||
1643 | 1697 | | |
1644 | 1698 | | |
1645 | 1699 | | |
1646 | | - | |
| 1700 | + | |
1647 | 1701 | | |
1648 | 1702 | | |
1649 | 1703 | | |
| |||
2301 | 2355 | | |
2302 | 2356 | | |
2303 | 2357 | | |
2304 | | - | |
2305 | | - | |
2306 | | - | |
2307 | | - | |
2308 | | - | |
2309 | | - | |
2310 | | - | |
2311 | | - | |
2312 | | - | |
2313 | | - | |
2314 | | - | |
2315 | | - | |
2316 | | - | |
2317 | | - | |
2318 | | - | |
2319 | | - | |
2320 | | - | |
2321 | | - | |
2322 | | - | |
2323 | | - | |
2324 | | - | |
2325 | | - | |
2326 | | - | |
2327 | | - | |
2328 | | - | |
2329 | | - | |
2330 | | - | |
2331 | | - | |
2332 | | - | |
2333 | | - | |
2334 | | - | |
2335 | | - | |
2336 | | - | |
2337 | | - | |
2338 | | - | |
2339 | 2358 | | |
2340 | 2359 | | |
2341 | 2360 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
11 | 11 | | |
12 | 12 | | |
13 | 13 | | |
14 | | - | |
| 14 | + | |
15 | 15 | | |
16 | 16 | | |
17 | 17 | | |
| |||
63 | 63 | | |
64 | 64 | | |
65 | 65 | | |
66 | | - | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
67 | 71 | | |
68 | 72 | | |
69 | 73 | | |
| |||
0 commit comments