-
Notifications
You must be signed in to change notification settings - Fork 4.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Make StakeDelegations clone-on-write #21542
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
//! Map pubkeys to stake delegations | ||
//! | ||
//! This module implements clone-on-write semantics for `StakeDelegations` to reduce unnecessary | ||
//! cloning of the underlying map. | ||
use { | ||
solana_sdk::{pubkey::Pubkey, stake::state::Delegation}, | ||
std::{ | ||
collections::HashMap, | ||
ops::{Deref, DerefMut}, | ||
sync::Arc, | ||
}, | ||
}; | ||
|
||
/// A map of pubkey-to-stake-delegation with clone-on-write semantics | ||
#[derive(Default, Clone, PartialEq, Debug, Deserialize, Serialize, AbiExample)] | ||
pub struct StakeDelegations(Arc<StakeDelegationsInner>); | ||
|
||
impl Deref for StakeDelegations { | ||
type Target = StakeDelegationsInner; | ||
fn deref(&self) -> &Self::Target { | ||
&self.0 | ||
} | ||
} | ||
|
||
impl DerefMut for StakeDelegations { | ||
fn deref_mut(&mut self) -> &mut Self::Target { | ||
Arc::make_mut(&mut self.0) | ||
} | ||
} | ||
|
||
/// The inner type, which maps pubkeys to stake delegations | ||
type StakeDelegationsInner = HashMap<Pubkey, Delegation>; | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn test_stake_delegations_is_cow() { | ||
let voter_pubkey = Pubkey::new_unique(); | ||
let stake = rand::random(); | ||
let activation_epoch = rand::random(); | ||
let warmup_cooldown_rate = rand::random(); | ||
let delegation = | ||
Delegation::new(&voter_pubkey, stake, activation_epoch, warmup_cooldown_rate); | ||
|
||
let pubkey = Pubkey::new_unique(); | ||
|
||
let mut stake_delegations = StakeDelegations::default(); | ||
stake_delegations.insert(pubkey, delegation); | ||
|
||
// Test: Clone the stake delegations and **do not modify**. Assert the underlying maps are | ||
// the same instance. | ||
{ | ||
let stake_delegations2 = stake_delegations.clone(); | ||
assert_eq!(stake_delegations, stake_delegations2); | ||
assert!( | ||
Arc::ptr_eq(&stake_delegations.0, &stake_delegations2.0), | ||
"Inner Arc must point to the same HashMap" | ||
); | ||
assert!( | ||
std::ptr::eq(stake_delegations.deref(), stake_delegations2.deref()), | ||
"Deref must point to the same HashMap" | ||
); | ||
} | ||
|
||
// Test: Clone the stake delegations and then modify (remove the K-V, then re-add the same | ||
// one, so the stake delegations are still logically equal). Assert the underlying maps | ||
// are unique instances. | ||
{ | ||
let mut stake_delegations2 = stake_delegations.clone(); | ||
stake_delegations2.clear(); | ||
assert_ne!(stake_delegations, stake_delegations2); | ||
stake_delegations2.insert(pubkey, delegation); | ||
assert_eq!(stake_delegations, stake_delegations2); | ||
assert!( | ||
!Arc::ptr_eq(&stake_delegations.0, &stake_delegations2.0), | ||
"Inner Arc must point to different HashMaps" | ||
); | ||
assert!( | ||
!std::ptr::eq(stake_delegations.deref(), stake_delegations2.deref()), | ||
"Deref must point to different HashMaps" | ||
); | ||
} | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,10 @@ | ||
//! Stakes serve as a cache of stake and vote accounts to derive | ||
//! node stakes | ||
use { | ||
crate::vote_account::{VoteAccount, VoteAccounts, VoteAccountsHashMap}, | ||
crate::{ | ||
stake_delegations::StakeDelegations, | ||
vote_account::{VoteAccount, VoteAccounts, VoteAccountsHashMap}, | ||
}, | ||
rayon::{ | ||
iter::{IntoParallelRefIterator, ParallelIterator}, | ||
ThreadPool, | ||
|
@@ -27,7 +30,7 @@ pub struct Stakes { | |
vote_accounts: VoteAccounts, | ||
|
||
/// stake_delegations | ||
stake_delegations: HashMap<Pubkey, Delegation>, | ||
stake_delegations: StakeDelegations, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Pretty nice! Basically no other changes needed. |
||
|
||
/// unused | ||
unused: u64, | ||
|
@@ -273,7 +276,7 @@ impl Stakes { | |
&self.vote_accounts | ||
} | ||
|
||
pub fn stake_delegations(&self) -> &HashMap<Pubkey, Delegation> { | ||
pub fn stake_delegations(&self) -> &StakeDelegations { | ||
&self.stake_delegations | ||
} | ||
|
||
|
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This function was only called by the bank tests, so I've moved it into the test module. Originally I only needed to change the return type (to
StakeDelegations
), but saw I could also move the fn.