perf: Use O(1) ring buffer cache for block hashes instead of BTreeMap#3299
Merged
rakita merged 4 commits intobluealloy:mainfrom Jan 15, 2026
Merged
perf: Use O(1) ring buffer cache for block hashes instead of BTreeMap#3299rakita merged 4 commits intobluealloy:mainfrom
rakita merged 4 commits intobluealloy:mainfrom
Conversation
Merging this PR will degrade performance by 3.07%
Performance Changes
Comparing |
Contributor
Author
Member
I dont thing benchmarks cover past block hashes so this is okay |
rakita
reviewed
Jan 14, 2026
rakita
reviewed
Jan 14, 2026
rakita
approved these changes
Jan 14, 2026
Member
rakita
left a comment
There was a problem hiding this comment.
left few nits, but in general lgtm
Member
|
do |
Contributor
Author
Fascinating |
rakita
reviewed
Jan 15, 2026
rakita
reviewed
Jan 15, 2026
| if let Some(entry) = self.block_hashes.get(&number) { | ||
| return Ok(*entry); | ||
| if let Some(entry) = self.block_hashes.get(number) { | ||
| return Ok(FixedBytes(*entry)); |
Member
There was a problem hiding this comment.
Interesting artefact, can fix it later
Open
bshastry
added a commit
to bshastry/revm
that referenced
this pull request
Jan 16, 2026
The ring buffer cache introduced in bluealloy#3299 was initialized with `(0, B256::ZERO)` for all entries. This caused `get(0)` to incorrectly match the default entry and return `B256::ZERO` instead of `None`, preventing the database fallback from being invoked. This is a consensus bug for any chain at low block numbers (< 256) where BLOCKHASH(0) should return the genesis block hash. Fix: Use `Option<B256>` instead of `B256` to explicitly distinguish between "not cached" (`None`) and "cached with value" (`Some(hash)`). 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
bshastry
added a commit
to bshastry/revm
that referenced
this pull request
Jan 16, 2026
The ring buffer cache introduced in bluealloy#3299 was initialized with `(0, B256::ZERO)` for all entries. This caused `get(0)` to incorrectly match the default entry and return `B256::ZERO` instead of `None`, preventing the database fallback from being invoked. This is a consensus bug for any chain at low block numbers (< 256) where BLOCKHASH(0) should return the genesis block hash. Fix: Use `Option<B256>` instead of `B256` to explicitly distinguish between "not cached" (`None`) and "cached with value" (`Some(hash)`). 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
rakita
added a commit
that referenced
this pull request
Jan 16, 2026
…3319) * fix(database): BlockHashCache incorrectly returns zero for block 0 The ring buffer cache introduced in #3299 was initialized with `(0, B256::ZERO)` for all entries. This caused `get(0)` to incorrectly match the default entry and return `B256::ZERO` instead of `None`, preventing the database fallback from being invoked. This is a consensus bug for any chain at low block numbers (< 256) where BLOCKHASH(0) should return the genesis block hash. Fix: Use `Option<B256>` instead of `B256` to explicitly distinguish between "not cached" (`None`) and "cached with value" (`Some(hash)`). 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com> * refactor: use Option<u64> for block number in BlockHashCache Address review feedback: - Use Option<u64> for block number instead of Option<B256> for hash - Keep const on insert and get functions * style: simplify get() comparison Use direct Option comparison as suggested in review. --------- Co-authored-by: Claude <noreply@anthropic.com> Co-authored-by: rakita <dragan0rakita@gmail.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.
Replaces the
BTreeMap<u64, B256>used for block hash caching inStatewith a newBlockHashCachestruct that implements a fixed-size ring buffer(Technically anyway)from benchmark: