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

fix: borrow stakes delegation during snapshot serialization #2455

Merged
merged 4 commits into from
Aug 8, 2024

Conversation

jstarry
Copy link

@jstarry jstarry commented Aug 6, 2024

Problem

When serializing snapshots, we do a full deep clone of the stake delegations when serializing Stakes<Delegation>. As of v2.1 this serialization flow is limited to serializing the stakes cache since epoch stakes are serialized with versioned epoch stakes as of #2282.

Summary of Changes

Build a shallow copy of stake delegations by borrowing the delegation field from the stake accounts to avoid allocating so much when serializing Stakes<Delegation> during snapshot creation.

Fixes #

@jstarry jstarry added the v2.0 Backport to v2.0 branch label Aug 6, 2024
Copy link

mergify bot commented Aug 6, 2024

Backports to the beta branch are to be avoided unless absolutely necessary for fixing bugs, security issues, and perf regressions. Changes intended for backport should be structured such that a minimum effective diff can be committed separately from any refactoring, plumbing, cleanup, etc that are not strictly necessary to achieve the goal. Any of the latter should go only into master and ride the normal stabilization schedule. Exceptions include CI/metrics changes, CLI improvements and documentation updates on a case by case basis.

@jstarry
Copy link
Author

jstarry commented Aug 6, 2024

Adding v2.0 backport to reduce memory allocation when serializing epoch stakes entries as well.

@@ -544,6 +547,23 @@ impl From<Stakes<StakeAccount>> for Stakes<Delegation> {
}
}

impl<'a> From<&'a Stakes<StakeAccount>> for Stakes<&'a Delegation> {
fn from(stakes: &'a Stakes<StakeAccount>) -> Self {

Choose a reason for hiding this comment

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

it looks like this is only used by serialize(), is that right? if so, I think we could
avoid this big allocation entirely and stream-serialize the input
&Stakes<StakeAccount> without creating this temporary Stakes<&Delegation>
directly in the caller?

Copy link
Author

Choose a reason for hiding this comment

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

Yeah good call, let me know what you think of 36b5a16

Copy link
Author

Choose a reason for hiding this comment

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

New commit here with generics and serialize-with removed: e004718

@jstarry
Copy link
Author

jstarry commented Aug 7, 2024

@ryoqun I might need some help with this abi example error which I believe is coming from using generics:

thread 'bank::serde_snapshot::tests::test_bank_serialize::BankAbiTestWrapper_frozen_abi::test_abi_digest' panicked at /solana/frozen-abi/src/abi_example.rs:267:9:
derive or implement AbiExample/AbiEnumVisitor for solana_runtime::stakes::serde_stakes::_::<impl serde::ser::Serialize for solana_runtime::stakes::serde_stakes::SerdeStakeDelegationsWrapper<_>>::serialize::__SerializeWith<solana_runtime::stake_account::StakeAccount<solana_program::stake::state::Delegation>>

I tried removing the generics too (can share the code if you want) but getting this error still:

thread 'bank::serde_snapshot::tests::test_bank_serialize::BankAbiTestWrapper_frozen_abi::test_abi_digest' panicked at frozen-abi/src/abi_digester.rs:405:9:
assertion `left == right` failed: Exactly 1 map entry is needed to generate the ABI digest precisely
  left: 0
 right: 1

@jstarry
Copy link
Author

jstarry commented Aug 8, 2024

Nvm @ryoqun I was able to make it work by not using __SerializeWith or generics

@@ -227,6 +227,13 @@ impl StakeStateV2 {
}
}

pub fn delegation_ref(&self) -> Option<&Delegation> {

Choose a reason for hiding this comment

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

nit for the future: I don't think it's worth having both delegation() and
delegation_ref()

It looks like we could have delegation return a ref, and then callers .clone()
if they need to?

Since we're backporting this it's probably better to add delegation_ref() for
now to minimize the diff, but something to keep in mind for the future.

Copy link
Author

Choose a reason for hiding this comment

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

Totally agree but didnt want to break the sdk api

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!

@jstarry jstarry merged commit c99095d into anza-xyz:master Aug 8, 2024
50 checks passed
mergify bot pushed a commit that referenced this pull request Aug 8, 2024
(cherry picked from commit c99095d)

# Conflicts:
#	runtime/src/bank/serde_snapshot.rs
#	runtime/src/stakes.rs
@jstarry
Copy link
Author

jstarry commented Aug 8, 2024

Btw I plan to make similar improvements to the versioned epoch stakes serialization code

@jstarry jstarry deleted the fix/shallow-stakes-clone branch August 8, 2024 15:33
jstarry added a commit that referenced this pull request Aug 9, 2024
(cherry picked from commit c99095d)

# Conflicts:
#	runtime/src/bank/serde_snapshot.rs
#	runtime/src/stakes.rs
jstarry added a commit that referenced this pull request Aug 9, 2024
(cherry picked from commit c99095d)

# Conflicts:
#	runtime/src/bank/serde_snapshot.rs
#	runtime/src/stakes.rs
Copy link

mergify bot commented Aug 10, 2024

The Firedancer team maintains a line-for-line reimplementation of the
native programs, and until native programs are moved to BPF, those
implementations must exactly match their Agave counterparts.
If this PR represents a change to a native program implementation (not
tests), please include a reviewer from the Firedancer team. And please
keep refactors to a minimum.

jstarry pushed a commit that referenced this pull request Aug 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
v2.0 Backport to v2.0 branch
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants