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

reduces intermediate vector allocations when recovering shreds #4289

Merged
merged 1 commit into from
Jan 6, 2025

Conversation

behzadnouri
Copy link

Problem

When recovering shreds, below two .collect::<Vec<_>>() are unnecessarily allocating a vector and can be skipped by using iterators:
https://github.com/anza-xyz/agave/blob/40be9b6d1/ledger/src/shred/merkle.rs#L987
https://github.com/anza-xyz/agave/blob/40be9b6d1/ledger/src/blockstore.rs#L1089

Also we can reduce allocations by collecting a Vec<Vec<Shred>> instead of below append:
https://github.com/anza-xyz/agave/blob/40be9b6d1/ledger/src/blockstore.rs#L844

Summary of Changes

The commit reduces intermediate vector allocations when recovering shreds.

@behzadnouri behzadnouri force-pushed the shred-recover-in-place branch from 089037a to 80daa82 Compare January 5, 2025 16:52
Comment on lines -1041 to -1044
metrics.num_recovered += recovered_shreds
.iter()
.filter(|shred| shred.is_data())
.count();
Copy link
Author

Choose a reason for hiding this comment

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

this is moved inside the filter_map below.

// Recovery rules:
// 1. Only try recovery around indexes for which new data or coding shreds are received
// 2. For new data shreds, check if an erasure set exists. If not, don't try recovery
// 3. Before trying recovery, check if enough number of shreds have been received
// 3a. Enough number of shreds = (#data + #coding shreds) > erasure.num_data
for (erasure_set, working_erasure_meta) in erasure_metas.iter() {
Copy link
Author

Choose a reason for hiding this comment

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

Might be easier to review by hiding the whitespace changes:

hide-whitespaces

Copy link

@AshwinSekar AshwinSekar left a comment

Choose a reason for hiding this comment

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

Looks correct.

Is it possible to keep it an iterator at every step?
Not sure if it's desirable here but it seems like it should be doable without a Box?
merkle::recover -> shred::recover -> recover_shreds -> try_shred_recovery -> flatten -> filter_map -> collect

@behzadnouri
Copy link
Author

Is it possible to keep it an iterator at every step?

The references interfere, because we need a mutable reference here:
https://github.com/anza-xyz/agave/blob/a2b219c15/ledger/src/blockstore.rs#L1056

but that is already passed to try_shred_recovery:
https://github.com/anza-xyz/agave/blob/a2b219c15/ledger/src/blockstore.rs#L1036

@AshwinSekar
Copy link

ah got it. :shipit:

@behzadnouri behzadnouri merged commit 7740b82 into anza-xyz:master Jan 6, 2025
40 checks passed
@behzadnouri behzadnouri deleted the shred-recover-in-place branch January 6, 2025 16:34
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