Skip to content

Cache codes using KVCachedFile#434

Open
ihtefyw wants to merge 9 commits into
mainfrom
luigi/sonic_2.2_code_cache_final
Open

Cache codes using KVCachedFile#434
ihtefyw wants to merge 9 commits into
mainfrom
luigi/sonic_2.2_code_cache_final

Conversation

@ihtefyw

@ihtefyw ihtefyw commented Jul 13, 2026

Copy link
Copy Markdown
Contributor

In the MPT implementation, all codes are kept in memory in a hash map. This increases memory consumption significantly and can possibly crash Carmen.
This PR adds a caching layer on top of the codes by using the KVCachedFile. Most of the existing code logic was generalized and moved to OffsetFile, a KVFile which stores the offset of each element in memory.
The cache is set to 20,000 codes + 2,000 for the flush buffer, which equals to a worst case memory usage of ~1GB.

Notable changes:

  • Because now getting a code could result in a file lookup, GetCodeForHash and GetCodes return an error now, and the interface and implementations have been updated accordingly.
  • getReferencedCodes in database/mpt/io/live.go was still materializing the whole code file in memory. It has been modified to write the codes while scanning them, and renamed to writeReferencedCodes

Closes https://github.com/0xsoniclabs/sonic-admin/issues/844

@ihtefyw
ihtefyw requested a review from HerbertJordan July 13, 2026 21:48
@ihtefyw
ihtefyw force-pushed the luigi/kv-cached-file branch from 1ab2162 to 84c80e6 Compare July 13, 2026 21:49
@ihtefyw
ihtefyw force-pushed the luigi/sonic_2.2_code_cache_final branch 2 times, most recently from 0ec86af to a976281 Compare July 14, 2026 08:03
@ihtefyw
ihtefyw requested review from Copilot and removed request for HerbertJordan July 14, 2026 08:04

Copilot AI left a comment

Copy link
Copy Markdown

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 introduces an on-disk caching layer for contract bytecode in the MPT implementation by replacing the in-memory code map with common.KVCachedFile, reducing steady-state memory pressure at the cost of possible file-backed lookups. It also updates the GetCodeForHash API to return an error to reflect potential I/O failures.

Changes:

  • Replace in-memory code storage with KVCachedFile-backed storage with configured cache and flush-buffer thresholds.
  • Update GetCodeForHash to return ([]byte, error) and propagate errors through MptState, ArchiveTrie, and export/visitor flows.
  • Update and extend tests/mocks to validate error forwarding and persistence behavior.

Reviewed changes

Copilot reviewed 8 out of 10 changed files in this pull request and generated 5 comments.

Show a summary per file
File Description
go/database/mpt/verification_test.go Adjusts verification setup for the new codes file behavior.
go/database/mpt/state.go Updates LiveState/MptState code retrieval API to return errors.
go/database/mpt/state_test.go Updates tests and adds cases ensuring codes read errors are forwarded.
go/database/mpt/state_mocks.go Regenerates mocks for the updated GetCodeForHash signature.
go/database/mpt/io/live.go Updates visitor/export path to handle GetCodeForHash errors.
go/database/mpt/io/live_mocks.go Regenerates mocks for updated visitor interface.
go/database/mpt/codes.go Replaces in-memory codes map with KVCachedFile and updates read/write helpers.
go/database/mpt/codes_test.go Updates/extends tests for cached-file-backed codes persistence and lookups.
go/database/mpt/archive_trie.go Updates archive trie code retrieval to propagate errors.
go/database/mpt/archive_trie_test.go Adds tests validating error forwarding from LiveState/GetCodeForHash.
Files not reviewed (2)
  • go/database/mpt/io/live_mocks.go: Generated file
  • go/database/mpt/state_mocks.go: Generated file

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

Comment thread go/database/mpt/codes.go Outdated
Comment thread go/database/mpt/codes.go
Comment thread go/database/mpt/codes.go Outdated
Comment thread go/database/mpt/codes.go Outdated
Comment thread go/database/mpt/io/live.go
@ihtefyw
ihtefyw force-pushed the luigi/kv-cached-file branch from 8b09ac7 to ec123e2 Compare July 20, 2026 13:01
@ihtefyw
ihtefyw force-pushed the luigi/sonic_2.2_code_cache_final branch 3 times, most recently from 2e9406a to eb4ad67 Compare July 20, 2026 13:09
@ihtefyw
ihtefyw force-pushed the luigi/kv-cached-file branch from ec123e2 to 26718d5 Compare July 20, 2026 13:17
@ihtefyw
ihtefyw force-pushed the luigi/sonic_2.2_code_cache_final branch from eb4ad67 to fbe0162 Compare July 20, 2026 13:20
@ihtefyw
ihtefyw requested a review from Copilot July 20, 2026 13:21
@ihtefyw
ihtefyw force-pushed the luigi/kv-cached-file branch from 26718d5 to 398e739 Compare July 20, 2026 13:26
@ihtefyw
ihtefyw force-pushed the luigi/sonic_2.2_code_cache_final branch from fbe0162 to 724e408 Compare July 20, 2026 13:27

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 8 out of 10 changed files in this pull request and generated 7 comments.

Files not reviewed (2)
  • go/database/mpt/io/live_mocks.go: Generated file
  • go/database/mpt/state_mocks.go: Generated file

Comment thread go/database/mpt/codes.go Outdated
Comment thread go/database/mpt/codes.go Outdated
Comment thread go/database/mpt/codes.go Outdated
Comment thread go/database/mpt/codes.go Outdated
Comment thread go/common/offset_file.go Outdated
Comment thread go/common/offset_file.go Outdated
Comment thread go/database/mpt/state_test.go
@ihtefyw
ihtefyw force-pushed the luigi/sonic_2.2_code_cache_final branch from 724e408 to 876010b Compare July 20, 2026 13:33
@ihtefyw
ihtefyw force-pushed the luigi/kv-cached-file branch from 398e739 to 5e52d3b Compare July 20, 2026 13:40
@ihtefyw
ihtefyw force-pushed the luigi/sonic_2.2_code_cache_final branch 2 times, most recently from ccbf504 to 58aa66e Compare July 20, 2026 13:48
@ihtefyw
ihtefyw requested a review from Copilot July 20, 2026 13:48
@ihtefyw
ihtefyw force-pushed the luigi/sonic_2.2_code_cache_final branch from 58aa66e to 8cd3be7 Compare July 20, 2026 13:51

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 8 out of 10 changed files in this pull request and generated 3 comments.

Files not reviewed (2)
  • go/database/mpt/io/live_mocks.go: Generated file
  • go/database/mpt/state_mocks.go: Generated file

Comment thread go/database/mpt/codes.go Outdated
Comment thread go/common/offset_file.go Outdated
Comment thread go/database/mpt/codes.go Outdated
@ihtefyw
ihtefyw force-pushed the luigi/kv-cached-file branch 2 times, most recently from 229b751 to b3bcaa4 Compare July 21, 2026 08:28

@HerbertJordan HerbertJordan left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Some initial feedback covering things that I think should be addressed before merging this. The changes may also be substantial enough that looking through the current tests in detail is premature, so I skipped that for now. Let me know what you think.

Comment thread go/common/offset_file.go Outdated
Comment thread go/common/offset_file.go Outdated
Comment thread go/database/mpt/codes.go Outdated
Comment thread go/common/offset_file.go Outdated
Comment thread go/common/offset_file.go Outdated
@ihtefyw
ihtefyw force-pushed the luigi/sonic_2.2_code_cache_final branch 2 times, most recently from 4ce79f3 to 6e49a70 Compare July 21, 2026 13:33
@ihtefyw
ihtefyw changed the base branch from luigi/kv-cached-file to luigi/offset_file July 21, 2026 13:38
@ihtefyw
ihtefyw force-pushed the luigi/offset_file branch from 4666842 to 5bd407d Compare July 22, 2026 07:30
@ihtefyw
ihtefyw force-pushed the luigi/sonic_2.2_code_cache_final branch from 6e49a70 to 51b3801 Compare July 22, 2026 07:32
@ihtefyw
ihtefyw force-pushed the luigi/offset_file branch from 5bd407d to 4b66aaa Compare July 22, 2026 07:37
@ihtefyw
ihtefyw force-pushed the luigi/sonic_2.2_code_cache_final branch 3 times, most recently from 873acbb to fabac29 Compare July 22, 2026 08:48
@ihtefyw
ihtefyw force-pushed the luigi/offset_file branch 3 times, most recently from 3c01c44 to f3ef979 Compare July 22, 2026 11:46
@ihtefyw
ihtefyw force-pushed the luigi/sonic_2.2_code_cache_final branch from fabac29 to f40583a Compare July 22, 2026 11:46
@ihtefyw
ihtefyw requested a review from HerbertJordan July 22, 2026 14:07
Base automatically changed from luigi/offset_file to main July 23, 2026 13:39
@ihtefyw
ihtefyw force-pushed the luigi/sonic_2.2_code_cache_final branch from f842b7e to 68ca639 Compare July 23, 2026 13:57

@HerbertJordan HerbertJordan left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Integration looks fine, but some functions still materialize the full code map. Those should be eliminated.

I did not check the unit tests.

Comment thread go/database/mpt/codes.go Outdated
Comment thread go/database/mpt/codes.go Outdated
Comment thread go/database/mpt/codes.go Outdated
Comment thread go/database/mpt/codes.go Outdated
Comment thread go/database/mpt/codes.go Outdated
@ihtefyw
ihtefyw force-pushed the luigi/sonic_2.2_code_cache_final branch 2 times, most recently from 783a190 to da1097e Compare July 24, 2026 13:12
@ihtefyw
ihtefyw force-pushed the luigi/sonic_2.2_code_cache_final branch from 6358d41 to 1ea17d0 Compare July 24, 2026 14:34
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.

3 participants