implements copy-on-write for vote-accounts#19362
implements copy-on-write for vote-accounts#19362behzadnouri merged 1 commit intosolana-labs:masterfrom
Conversation
66af32d to
24cbc2c
Compare
Codecov Report
@@ Coverage Diff @@
## master #19362 +/- ##
========================================
Coverage 82.7% 82.7%
========================================
Files 459 461 +2
Lines 130921 131077 +156
========================================
+ Hits 108321 108457 +136
- Misses 22600 22620 +20 |
Bank::vote_accounts redundantly clones vote-accounts HashMap even though an immutable reference will suffice: https://github.com/solana-labs/solana/blob/95c998a19/runtime/src/bank.rs#L5174-L5186 This commit implements copy-on-write semantics for vote-accounts by wrapping the underlying HashMap in Arc<...>.
24cbc2c to
818fd84
Compare
| pub(crate) fn insert(&mut self, pubkey: Pubkey, (stake, vote_account): (u64, VoteAccount)) { | ||
| self.add_node_stake(stake, &vote_account); | ||
| if let Some((stake, vote_account)) = | ||
| self.vote_accounts.insert(pubkey, (stake, vote_account)) | ||
| { | ||
| let vote_accounts = Arc::make_mut(&mut self.vote_accounts); | ||
| if let Some((stake, vote_account)) = vote_accounts.insert(pubkey, (stake, vote_account)) { | ||
| self.sub_node_stake(stake, &vote_account); | ||
| } | ||
| } | ||
|
|
||
| pub fn remove(&mut self, pubkey: &Pubkey) -> Option<(u64, VoteAccount)> { | ||
| let value = self.vote_accounts.remove(pubkey); | ||
| if let Some((stake, ref vote_account)) = value { | ||
| pub(crate) fn remove(&mut self, pubkey: &Pubkey) -> Option<(u64, VoteAccount)> { | ||
| let vote_accounts = Arc::make_mut(&mut self.vote_accounts); | ||
| let entry = vote_accounts.remove(pubkey); | ||
| if let Some((stake, ref vote_account)) = entry { | ||
| self.sub_node_stake(stake, vote_account); | ||
| } | ||
| value | ||
| entry | ||
| } | ||
|
|
||
| pub fn add_stake(&mut self, pubkey: &Pubkey, delta: u64) { | ||
| if let Some((stake, vote_account)) = self.vote_accounts.get_mut(pubkey) { | ||
| pub(crate) fn add_stake(&mut self, pubkey: &Pubkey, delta: u64) { | ||
| let vote_accounts = Arc::make_mut(&mut self.vote_accounts); | ||
| if let Some((stake, vote_account)) = vote_accounts.get_mut(pubkey) { | ||
| *stake += delta; | ||
| let vote_account = vote_account.clone(); | ||
| self.add_node_stake(delta, &vote_account); | ||
| } | ||
| } | ||
|
|
||
| pub fn sub_stake(&mut self, pubkey: &Pubkey, delta: u64) { | ||
| if let Some((stake, vote_account)) = self.vote_accounts.get_mut(pubkey) { | ||
| pub(crate) fn sub_stake(&mut self, pubkey: &Pubkey, delta: u64) { | ||
| let vote_accounts = Arc::make_mut(&mut self.vote_accounts); | ||
| if let Some((stake, vote_account)) = vote_accounts.get_mut(pubkey) { |
There was a problem hiding this comment.
Nice! I think this works
Quick sanity check, this should be safe even if we have interleaving updates between Replay and BankingStage.
For example, imagine we have:
/--------5 (Banking Stage)
0
\-----------4 (ReplayStage)
So the sequence of events:
-
We start a leader block for our own slot
5, inheriting the sharedstakesobject from the slot0:.Line 1344 in 84db04c
-
At some later point in time Replay concurrently creates and starts replaying block
4, also inheriting the sharedstakesobject from the slot0.
The situation we want to avoid is either of the slot-specific updates to stakes via update_cached_accounts():
Line 5152 in 84db04c
Now we know this is safe because if we do an update in slot 5 before or after 4 is created, we clone the vote accounts for 5 via make_mut, so these updates are hidden from 4.
There was a problem hiding this comment.
Yes, it will be safe because as soon as any mutations are about to be made (and there is still another Arc reference to the same hash-map), a clone is made and so the mutations are not leaked to other Arc references.
The added test_staked_nodes_cow test is testing one such scenario.
Bank::vote_accounts redundantly clones vote-accounts HashMap even though an immutable reference will suffice: https://github.com/solana-labs/solana/blob/95c998a19/runtime/src/bank.rs#L5174-L5186 This commit implements copy-on-write semantics for vote-accounts by wrapping the underlying HashMap in Arc<...>.
This reverts commit cb0c470.
Bank::vote_accounts redundantly clones vote-accounts HashMap even though an immutable reference will suffice: https://github.com/solana-labs/solana/blob/95c998a19/runtime/src/bank.rs#L5174-L5186 This commit implements copy-on-write semantics for vote-accounts by wrapping the underlying HashMap in Arc<...>. (cherry picked from commit 8ad52fa) # Conflicts: # core/src/cluster_info_vote_listener.rs # core/src/consensus.rs # ledger/src/blockstore_processor.rs # runtime/src/bank.rs # runtime/src/stakes.rs # runtime/src/vote_account.rs
Bank::vote_accounts redundantly clones vote-accounts HashMap even though an immutable reference will suffice: https://github.com/solana-labs/solana/blob/95c998a19/runtime/src/bank.rs#L5174-L5186 This commit implements copy-on-write semantics for vote-accounts by wrapping the underlying HashMap in Arc<...>.
Bank::vote_accounts redundantly clones vote-accounts HashMap even though an immutable reference will suffice: https://github.com/solana-labs/solana/blob/95c998a19/runtime/src/bank.rs#L5174-L5186 This commit implements copy-on-write semantics for vote-accounts by wrapping the underlying HashMap in Arc<...>.
Bank::vote_accounts redundantly clones vote-accounts HashMap even though an immutable reference will suffice: https://github.com/solana-labs/solana/blob/95c998a19/runtime/src/bank.rs#L5174-L5186 This commit implements copy-on-write semantics for vote-accounts by wrapping the underlying HashMap in Arc<...>.
…22139) Bank::vote_accounts redundantly clones vote-accounts HashMap even though an immutable reference will suffice: https://github.com/solana-labs/solana/blob/95c998a19/runtime/src/bank.rs#L5174-L5186 This commit implements copy-on-write semantics for vote-accounts by wrapping the underlying HashMap in Arc<...>. Co-authored-by: behzad nouri <behzadnouri@gmail.com>
Problem
Bank::vote_accountsredundantly clones vote-accounts HashMap even thoughan immutable reference will suffice:
https://github.com/solana-labs/solana/blob/95c998a19/runtime/src/bank.rs#L5174-L5186
Summary of Changes
This commit implements copy-on-write semantics for vote-accounts by
wrapping the underlying HashMap in
Arc<...>.