Skip to content

Reduce clones and cleanup in block cache path#6431

Open
incrypto32 wants to merge 4 commits intomasterfrom
krishna/reduce-clones-json-roundtrips
Open

Reduce clones and cleanup in block cache path#6431
incrypto32 wants to merge 4 commits intomasterfrom
krishna/reduce-clones-json-roundtrips

Conversation

@incrypto32
Copy link
Member

Addresses review feedback from #6400.

  • Migrate check_blocks to use typed blocks() instead of raw JSON
  • Remove blocks_as_json from ChainStore trait
  • Wrap LightEthereumBlock in Arc inside CachedBlock::Light to avoid expensive deep clones
  • Inline EthereumJsonBlock into CachedBlock::from_json and delete the wrapper

Use the typed `blocks()` method instead of `blocks_as_json()` so that
block comparisons go through the same alloy serialization on both the
cached and provider sides. This gives a fairer diff by normalizing
both blocks through the same types, and only compares the block data
(without receipts) since `eth_getBlockByHash` does not return receipts.
All callers now use the typed `blocks()` method. The resolver serializes
the CachedBlock to JSON at the GraphQL boundary. This eliminates the
separate raw-JSON code path from the trait.
Arc-wrap the Light variant to avoid expensive deep clones of AnyBlock.
into_light_block now borrows self and returns Arc via cheap refcount bumps.
Remove the single-consumer EthereumJsonBlock wrapper and inline its
deserialization and patching logic directly into CachedBlock::from_json.
Copy link
Collaborator

@lutter lutter left a comment

Choose a reason for hiding this comment

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

Nice! Just a few very minor comments, feel free to ignore

let json = match block {
CachedBlock::Full(ref b) => serde_json::to_value(b),
CachedBlock::Light(ref b) => serde_json::to_value(b),
};
Copy link
Collaborator

Choose a reason for hiding this comment

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

This could be a to_json method on CachedBlock

.unwrap_or_default()
.into_iter()
.map(CachedBlock::into_light_block)
.map(|b| b.into_light_block())
Copy link
Collaborator

Choose a reason for hiding this comment

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

This seems like a noop, but I assume it's because b isn't a reference here. Maybe changing into_iter() to iter() would be better to make it clear that some copying is going on

}

pub fn into_light_block(self) -> Arc<LightEthereumBlock> {
pub fn into_light_block(&self) -> Arc<LightEthereumBlock> {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Very minor, but in Rust, into_ methods usually take ownership; to_ methods usually operate on references.

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