-
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 StakeHistory clone-on-write #21573
Merged
Merged
Changes from all commits
Commits
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
//! This module implements clone-on-write semantics for the SDK's `StakeHistory` to reduce | ||
//! unnecessary cloning of the underlying vector. | ||
use std::{ | ||
ops::{Deref, DerefMut}, | ||
sync::Arc, | ||
}; | ||
|
||
/// The SDK's stake history with clone-on-write semantics | ||
#[derive(Default, Clone, PartialEq, Debug, Deserialize, Serialize, AbiExample)] | ||
pub struct StakeHistory(Arc<StakeHistoryInner>); | ||
|
||
impl Deref for StakeHistory { | ||
type Target = StakeHistoryInner; | ||
fn deref(&self) -> &Self::Target { | ||
&self.0 | ||
} | ||
} | ||
|
||
impl DerefMut for StakeHistory { | ||
fn deref_mut(&mut self) -> &mut Self::Target { | ||
Arc::make_mut(&mut self.0) | ||
} | ||
} | ||
|
||
/// The inner type, which is the SDK's stake history | ||
type StakeHistoryInner = solana_sdk::stake_history::StakeHistory; | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
use solana_sdk::stake_history::StakeHistoryEntry; | ||
|
||
fn rand_stake_history_entry() -> StakeHistoryEntry { | ||
StakeHistoryEntry { | ||
effective: rand::random(), | ||
activating: rand::random(), | ||
deactivating: rand::random(), | ||
} | ||
} | ||
|
||
#[test] | ||
fn test_stake_history_is_cow() { | ||
let mut stake_history = StakeHistory::default(); | ||
(100..109).for_each(|epoch| { | ||
let entry = rand_stake_history_entry(); | ||
stake_history.add(epoch, entry); | ||
}); | ||
|
||
// Test: Clone the stake history and **do not modify**. Assert the underlying instances | ||
// are the same. | ||
{ | ||
let stake_history2 = stake_history.clone(); | ||
assert_eq!(stake_history, stake_history2); | ||
assert!( | ||
Arc::ptr_eq(&stake_history.0, &stake_history2.0), | ||
"Inner Arc must point to the same underlying instance" | ||
); | ||
assert!( | ||
std::ptr::eq(stake_history.deref(), stake_history2.deref()), | ||
"Deref must point to the same underlying instance" | ||
); | ||
} | ||
|
||
// Test: Clone the stake history and then modify. Assert the underlying instances are | ||
// unique. | ||
{ | ||
let mut stake_history2 = stake_history.clone(); | ||
assert_eq!(stake_history, stake_history2); | ||
(200..209).for_each(|epoch| { | ||
let entry = rand_stake_history_entry(); | ||
stake_history2.add(epoch, entry); | ||
}); | ||
assert_ne!(stake_history, stake_history2); | ||
assert!( | ||
!Arc::ptr_eq(&stake_history.0, &stake_history2.0), | ||
"Inner Arc must point to a different underlying instance" | ||
); | ||
assert!( | ||
!std::ptr::eq(stake_history.deref(), stake_history2.deref()), | ||
"Deref must point to a different underlying instance" | ||
); | ||
} | ||
} | ||
} |
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 |
---|---|---|
|
@@ -3,6 +3,7 @@ | |
use { | ||
crate::{ | ||
stake_delegations::StakeDelegations, | ||
stake_history::StakeHistory, | ||
vote_account::{VoteAccount, VoteAccounts, VoteAccountsHashMap}, | ||
}, | ||
rayon::{ | ||
|
@@ -17,7 +18,6 @@ use { | |
self, | ||
state::{Delegation, StakeActivationStatus, StakeState}, | ||
}, | ||
stake_history::StakeHistory, | ||
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! The only visible change to stakes.rs is where to get |
||
}, | ||
solana_stake_program::stake_state, | ||
solana_vote_program::vote_state::VoteState, | ||
|
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.
Similar to
StageDelegations
in #21542, we need anArc
here since banks are sent across threads (i.e. the accounts background services).