Skip to content
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 3 commits into from
Dec 3, 2021

Conversation

brooksprumo
Copy link
Contributor

@brooksprumo brooksprumo commented Dec 1, 2021

Problem

Stake delegations are cloned into every bank, regardless if they are changed or not.

Summary of Changes

Implement clone-on-write semantics for stake delegations.

Comment on lines -5777 to -5801
/// current stake delegations for this bank
pub fn cloned_stake_delegations(&self) -> HashMap<Pubkey, Delegation> {
self.stakes.read().unwrap().stake_delegations().clone()
}

Copy link
Contributor Author

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.

Comment on lines 14 to 16
/// A map of pubkey-to-stake-delegation with clone-on-write semantics
#[derive(Default, Clone, PartialEq, Debug, Deserialize, Serialize)]
pub struct StakeDelegations(Arc<StakeDelegationsInner>);
Copy link
Contributor Author

@brooksprumo brooksprumo Dec 1, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The inner map needs to be wrapped in an Arc due to it being passed between threads. Found this out from the compiler while building an earlier version with Rc.

@jstarry Using Rc (or Arc) answers the questions we were having yesterday about Rust and lifetimes, vs just using Cow.

@@ -27,7 +30,7 @@ pub struct Stakes {
vote_accounts: VoteAccounts,

/// stake_delegations
stake_delegations: HashMap<Pubkey, Delegation>,
stake_delegations: StakeDelegations,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pretty nice! Basically no other changes needed.

@brooksprumo brooksprumo marked this pull request as ready for review December 1, 2021 23:05
@brooksprumo brooksprumo requested a review from jstarry December 1, 2021 23:05
@codecov
Copy link

codecov bot commented Dec 2, 2021

Codecov Report

Merging #21542 (1bbcb2e) into master (1fae3d2) will increase coverage by 0.0%.
The diff coverage is 100.0%.

@@           Coverage Diff           @@
##           master   #21542   +/-   ##
=======================================
  Coverage    81.5%    81.5%           
=======================================
  Files         506      507    +1     
  Lines      142216   142251   +35     
=======================================
+ Hits       116024   116071   +47     
+ Misses      26192    26180   -12     

Copy link
Member

@jstarry jstarry left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Look great!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants