Skip to content

Conversation

@DracoLi
Copy link
Contributor

@DracoLi DracoLi commented Oct 17, 2025

Why this should be merged

I noticed that during C-Chain block execution testing, each block accepted will trigger 3 block data fetches (header, body, and receipts). This happens when collecting unflattened logs in the acceptor queue (code).

Fetching block data should be fast but decompressing the data each time adds a lot of overhead. This overhead can be completely avoided with a cache because the data should have been just written to the database.

A cache was not originally added to blockdb because the VMs all have caches for blocks already. But they can be bypassed due to oversight and having a cache at the db level ensures these kind of issues are avoided.

How this works

  • Added an lru cache with configurable size (defaults to 256).
  • Cache is keyed by block height and is updated on Put and Get.
  • Cache is used on Get and Has to avoid disk access.

How this was tested

Unit tests and reexecution benchmark

Need to be documented in RELEASES.md?

@DracoLi DracoLi force-pushed the dl/block-cache branch 2 times, most recently from af83e7d to 5e0ee3c Compare October 17, 2025 22:54
@joshua-kim joshua-kim moved this to Backlog 🧊 in avalanchego Oct 27, 2025
@DracoLi DracoLi force-pushed the dl/block-cache branch 5 times, most recently from eb97db9 to 340fd4c Compare October 29, 2025 19:50
@DracoLi DracoLi marked this pull request as ready for review October 29, 2025 21:53
@DracoLi DracoLi requested review from a team, Copilot, maru-ava and yacovm and removed request for a team October 29, 2025 21:54
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR adds an LRU cache for block entries to blockdb to improve performance by avoiding repeated decompression of recently accessed block data.

  • Implements an LRU cache with configurable size (defaults to 256 entries) for block data
  • Updates cache on both Put and Get operations with cloned data to prevent modification
  • Adds cache lookups to Get and Has operations to avoid disk access for cached entries

Reviewed Changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
x/blockdb/config.go Adds EntryCacheSize configuration field with default value and validation
x/blockdb/database.go Implements LRU cache integration in Database struct and Get/Has/Put operations
x/blockdb/entry_cache_test.go Comprehensive test suite for cache functionality including cloning behavior
x/blockdb/readblock_test.go Updates test configurations to include new EntryCacheSize parameter
x/blockdb/writeblock_test.go Fixes test assertion to handle nil values properly and adds bytes import
x/blockdb/README.md Updates documentation to reflect cache feature and removes TODO item

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@DracoLi DracoLi requested review from joshua-kim and removed request for joshua-kim November 4, 2025 14:17
@joshua-kim joshua-kim moved this from Backlog 🧊 to In Progress 🏗️ in avalanchego Nov 4, 2025
// Operations (Get, Has, Put) are not atomic with the underlying database.
// Concurrent access to the same height can result in cache inconsistencies where
// the cache and database contain different values. This limitation is acceptable
// because concurrent writes to the same height are not an intended use case.
Copy link
Contributor

@rrazvan1 rrazvan1 Nov 5, 2025

Choose a reason for hiding this comment

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

Suggested change
// because concurrent writes to the same height are not an intended use case.
// because concurrent access to the same height is not an intended use case.

Copy link
Contributor Author

@DracoLi DracoLi Nov 5, 2025

Choose a reason for hiding this comment

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

concurrent Get/Has to the same height is fine and expected. its the writes to the same height that is not intended usage and will result in access inconsistencies from cached Has and Get

Copy link
Contributor

Choose a reason for hiding this comment

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

Then it sounds wrong to me:

Concurrent access to the same height can result in cache inconsistencies [...] is acceptable because concurrent writes to the same height are not an intended use case.

Copy link
Contributor Author

@DracoLi DracoLi Nov 7, 2025

Choose a reason for hiding this comment

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

reverted to the original comment. 9792f38
I think both are right depending on how you interpret it but I think we can just call out the writes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: In Progress 🏗️

Development

Successfully merging this pull request may close these issues.

4 participants