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

Use less memory when serializing versioned epoch stakes #2520

Merged

Conversation

jstarry
Copy link

@jstarry jstarry commented Aug 9, 2024

Problem

When serializing versioned epoch stakes, we do a shallow clone of the stake delegations map when serializing stake accounts in the stake format. Even this shallow clone uses more memory than necessary.

Summary of Changes

Use a streaming serializing approach when serializing stake accounts in the stake format similar to #2455

Fixes #

@jstarry jstarry force-pushed the fix/versioned-epoch-stakes-stream-serde branch from b5fa1be to fdb537c Compare August 9, 2024 06:31
@jstarry jstarry force-pushed the fix/versioned-epoch-stakes-stream-serde branch from fdb537c to a1180a4 Compare August 9, 2024 11:49
{
match self {
Self::Stake(stakes) => stakes.serialize(serializer),
Self::Account(stakes) => Stakes::<&Stake>::from(stakes).serialize(serializer),
Copy link
Author

Choose a reason for hiding this comment

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

Before we did a shallow clone here

{
match self {
Self::Stake(stakes) => stakes.serialize(serializer),
Self::Account(stakes) => serialize_stake_accounts_to_stake_format(stakes, serializer),
Copy link
Author

Choose a reason for hiding this comment

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

Now we are doing a memory efficient serialization approach which doesn't need to allocate a new stakes map before serialization

alessandrod
alessandrod previously approved these changes Aug 11, 2024
Copy link

@alessandrod alessandrod left a comment

Choose a reason for hiding this comment

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

looks great, killing all these large, frequent allocation spikes is going to make a difference 🎉

/// remains allocated during serialization.
#[cfg_attr(feature = "frozen-abi", derive(AbiExample, AbiEnumVisitor))]
#[derive(Debug, Clone)]
pub enum SerdeStakesToStakeFormat {

Choose a reason for hiding this comment

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

nit: does this need to be pub?

Copy link
Author

Choose a reason for hiding this comment

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

Yeah unfortunately BankFieldsToSerialize and ExtraFieldsToSerialize both need to be pub right now for some snapshot functions and structs so VersionedEpochStakes and SerdeStakesToStakeFormat both need to be pub as well. I think we could lower the visibility of everything in a follow up. I'll give it a shot in a follow up PR.

/// the stake data. Serialization works by building a `Stakes<&Stake>` map which
/// borrows `&Stake` from `StakeAccount` entries in `Stakes<StakeAccount>`. Note
/// that `Stakes<&Stake>` still copies `Pubkey` keys so the `Stakes<&Stake>`
/// data structure still allocates a fair amount of memory but the memory only

Choose a reason for hiding this comment

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

Where are the pubkeys copied? Isn't Stakes a bunch of nested Arcs + im::HashMap?

Copy link
Author

Choose a reason for hiding this comment

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

Yeah you're right, this is an outdated comment, will clean it up

/// the stake data. Serialization works by building a `Stakes<&Stake>` map which
/// borrows `&Stake` from `StakeAccount` entries in `Stakes<StakeAccount>`. Note
/// that `Stakes<&Stake>` still copies `Pubkey` keys so the `Stakes<&Stake>`
/// data structure still allocates a fair amount of memory but the memory only

Choose a reason for hiding this comment

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

Where are the pubkeys copied? Isn't Stakes a bunch of nested Arcs + im::HashMap?

@jstarry jstarry merged commit 5b54ff2 into anza-xyz:master Aug 12, 2024
41 checks passed
@jstarry jstarry deleted the fix/versioned-epoch-stakes-stream-serde branch August 18, 2024 08:52
ray-kast pushed a commit to abklabs/agave that referenced this pull request Nov 27, 2024
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