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

[Merged by Bors] - Avoid cloning snapshots during sync #3271

Closed

Conversation

michaelsproul
Copy link
Member

Issue Addressed

Closes #2944

Proposed Changes

Remove snapshots from the cache during sync rather than cloning them. This reduces unnecessary cloning and memory fragmentation during sync.

Additional Info

This PR relies on the fact that the block_delay cache is not populated for blocks from sync. Relying on block delay may have the side effect that a change in block_delay calculation could lead to: a) more clones, if block delays are added for syncing blocks or b) less clones, if blocks near the head are erroneously provided without a block_delay. Case (a) would be a regression to the current status quo, and (b) is low-risk given we know that the snapshot cache is current susceptible to misses (hence tree-states).

@michaelsproul michaelsproul added ready-for-review The code is ready for review low-hanging-fruit Easy to resolve, get it before someone else does! optimization Something to make Lighthouse run more efficiently. labels Jun 15, 2022
@michaelsproul michaelsproul changed the base branch from stable to unstable June 15, 2022 08:47
Copy link
Member

@macladson macladson left a comment

Choose a reason for hiding this comment

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

Looks good. These assumptions seem safe to me. We will lose some snapshot cache hits due to some block delay cache misses, but it should be minor.

@@ -253,7 +253,8 @@ impl<T: EthSpec> SnapshotCache<T> {
.position(|snapshot| snapshot.beacon_block_root == block_root)
.map(|i| {
if let Some(cache) = self.snapshots.get(i) {
if block_slot > cache.beacon_block.slot() + 1 {
// Avoid cloning the block during sync (when the `block_delay` is `None`).
if block_slot > cache.beacon_block.slot() + 1 && block_delay.is_some() {
Copy link
Member

Choose a reason for hiding this comment

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

Very very minor point in the case where block_delay is None:
If we nest this if block below into the if let Some block would it avoid us checking block_delay twice (assuming block_slot > cache.beacon_block.slot() + 1 == true)?

Copy link
Member Author

Choose a reason for hiding this comment

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

yeah good call, that's simpler

@michaelsproul michaelsproul added waiting-on-author The reviewer has suggested changes and awaits thier implementation. and removed ready-for-review The code is ready for review labels Jun 20, 2022
@michaelsproul michaelsproul added ready-for-merge This PR is ready to merge. and removed waiting-on-author The reviewer has suggested changes and awaits thier implementation. labels Jun 20, 2022
@michaelsproul
Copy link
Member Author

bors r+

bors bot pushed a commit that referenced this pull request Jun 20, 2022
## Issue Addressed

Closes #2944

## Proposed Changes

Remove snapshots from the cache during sync rather than cloning them. This reduces unnecessary cloning and memory fragmentation during sync.

## Additional Info

This PR relies on the fact that the `block_delay` cache is not populated for blocks from sync. Relying on block delay may have the side effect that a change in `block_delay` calculation could lead to: a) more clones, if block delays are added for syncing blocks or b) less clones, if blocks near the head are erroneously provided without a `block_delay`. Case (a) would be a regression to the current status quo, and (b) is low-risk given we know that the snapshot cache is current susceptible to misses (hence `tree-states`).
@bors bors bot changed the title Avoid cloning snapshots during sync [Merged by Bors] - Avoid cloning snapshots during sync Jun 21, 2022
@bors bors bot closed this Jun 21, 2022
@michaelsproul michaelsproul deleted the reduce-sync-clones branch June 21, 2022 02:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
low-hanging-fruit Easy to resolve, get it before someone else does! optimization Something to make Lighthouse run more efficiently. ready-for-merge This PR is ready to merge.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Snapshots cloned during sync
2 participants