perf(comm): cache RLP-encoded block bytes to avoid per-peer re-serialization#40
Open
simonzg wants to merge 2 commits into
Open
perf(comm): cache RLP-encoded block bytes to avoid per-peer re-serialization#40simonzg wants to merge 2 commits into
simonzg wants to merge 2 commits into
Conversation
…tion per peer During block propagation, the same newly-produced block is requested by many peers in a short window. MsgGetBlockByID and MsgGetBlocksFromNumber handlers called rlp.EncodeToBytes on every individual peer request with no caching — O(peers) encoding work for what is logically an O(1) operation. RLP-encoding a full block (header + transactions + QC) allocates large byte slices and traverses the entire block structure. With 100 peers requesting the same block within seconds, this encoding ran 100 times. Add a 512-entry LRU cache (encodedBlocksCache) on Communicator keyed by block ID storing the encoded bytes. Both RPC handlers check the cache before encoding and populate it on miss. Blocks are immutable once committed so cached bytes are always valid. 512 entries covers well over an epoch. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Why
During block propagation, every peer that doesn't yet have a block will request it — typically within seconds of it being produced. The
MsgGetBlockByIDandMsgGetBlocksFromNumberRPC handlers callrlp.EncodeToBytesfrom scratch for every individual peer request:RLP-encoding a full block (header + all transactions + QC) allocates large byte slices and traverses the entire block structure. With 100 connected peers, this work is repeated 100 times for the same block — O(peers) encoding cost for what is logically a one-time O(1) operation. The repeated allocations also add GC pressure during the high-traffic window right after each new block.
What Changed
Added a 512-entry LRU cache
encodedBlocksCache(keyed bymeter.Bytes32block ID, values are[]byte) toCommunicator.Both
MsgGetBlockByIDandMsgGetBlocksFromNumbernow:Blocks are immutable once committed to the chain, so cached encoded bytes are permanently valid and require no invalidation. The 512-entry cap covers well over an epoch's worth of blocks with negligible memory overhead.
Expected Impact