Skip to content

dataTriesHolder with eviction cache#7191

Merged
BeniaminDrasovean merged 13 commits intofeat/collapse-trie-based-on-sizefrom
collapse-trie-based-on-size
Mar 16, 2026
Merged

dataTriesHolder with eviction cache#7191
BeniaminDrasovean merged 13 commits intofeat/collapse-trie-based-on-sizefrom
collapse-trie-based-on-size

Conversation

@BeniaminDrasovean
Copy link
Copy Markdown
Contributor

@BeniaminDrasovean BeniaminDrasovean commented Aug 14, 2025

Reasoning behind the pull request

  • The current implementation of dataTriesHolder is a simple cache that holds data tries. The cache is cleaned after each commit operation

Proposed changes

  • Add a new implementation of dataTriesHolder as a linked list with memory management and eviction capabilities.
  • Add config values for the size of the cache and for the size of the main trie

Testing procedure

  • Normal system test

Pre-requisites

Based on the Contributing Guidelines the PR author and the reviewers must check the following requirements are met:

  • was the PR targeted to the correct branch?
  • if this is a larger feature that probably needs more than one PR, is there a feat branch created?
  • if this is a feat branch merging, do all satellite projects have a proper tag inside go.mod?

Copy link
Copy Markdown
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 implements a dataTriesHolder as a linked list with memory management and eviction capabilities. The changes introduce proper memory limits for tries, replace the simple tries holder with a more sophisticated implementation, and update the trie creation API to include memory size parameters.

  • Implements a new linked-list-based dataTriesHolder with memory-aware eviction
  • Adds memory size parameters to trie creation throughout the codebase
  • Replaces the original simple tries holder with a more advanced memory-managed implementation

Reviewed Changes

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

Show a summary per file
File Description
state/triesHolder/dataTriesHolder.go Implements new linked-list-based data tries holder with memory management and LRU eviction
state/triesHolder/triesHolder.go Refactors original tries holder to be simpler without Replace method
trie/patriciaMerkleTrie.go Adds maxSizeInMemory parameter to trie constructor and validation
state/accountsDB.go Updates to use new data tries holder with memory limits
config/config.go Adds configuration options for trie memory limits
Multiple test files Updates test files to use new trie creation API with memory parameters

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

Comment thread state/triesHolder/dataTriesHolder.go
Comment thread state/accountsDB.go
Comment thread testscommon/storage/snapshotPruningStorerStub.go
@BeniaminDrasovean BeniaminDrasovean marked this pull request as ready for review October 8, 2025 12:16
@BeniaminDrasovean BeniaminDrasovean changed the title dataTriesHolder as linked list dataTriesHolder with eviction cache Oct 21, 2025
Base automatically changed from collect-trie-size-in-mem to feat/collapse-trie-based-on-size March 2, 2026 14:38
…sed-on-size

# Conflicts:
#	dataRetriever/factory/resolverscontainer/metaResolversContainerFactory_test.go
#	dataRetriever/factory/resolverscontainer/shardResolversContainerFactory_test.go
#	epochStart/bootstrap/process.go
#	epochStart/metachain/systemSCs_test.go
#	factory/api/apiResolverFactory.go
#	factory/processing/blockProcessorCreator_test.go
#	factory/state/stateComponents.go
#	genesis/process/memoryComponents.go
#	integrationTests/state/stateTrie/stateTrie_test.go
#	integrationTests/testInitializer.go
#	integrationTests/testProcessorNode.go
#	integrationTests/vm/staking/componentsHolderCreator.go
#	state/accountsDB.go
#	state/accountsDB_test.go
#	state/factory/accountsAdapterAPICreator_test.go
#	state/storagePruningManager/storagePruningManager_test.go
#	testscommon/integrationtests/factory.go
#	trie/snapshotTrieStorageManager_test.go
#	trie/trieStorageManagerInEpoch_test.go
#	update/factory/dataTrieFactory.go
#	update/genesis/import.go
Copy link
Copy Markdown
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

Copilot reviewed 59 out of 59 changed files in this pull request and generated 2 comments.

Comments suppressed due to low confidence (1)

state/triesHolder/triesHolder.go:43

  • Get and GetAll acquire the write lock (Lock) even though they only read from the map. Since this uses an RWMutex, switching these to RLock/RUnlock would allow concurrent readers and improve contention under read-heavy access patterns.

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

Comment thread epochStart/bootstrap/process.go Outdated
Comment thread testscommon/components/components.go Outdated
@codecov
Copy link
Copy Markdown

codecov Bot commented Mar 3, 2026

Codecov Report

❌ Patch coverage is 76.74419% with 50 lines in your changes missing coverage. Please review.
✅ Project coverage is 77.53%. Comparing base (fe6d54f) to head (96ad288).
⚠️ Report is 4421 commits behind head on feat/collapse-trie-based-on-size.

Files with missing lines Patch % Lines
state/triesHolder/dataTriesHolder.go 80.53% 14 Missing and 8 partials ⚠️
update/genesis/import.go 0.00% 20 Missing ⚠️
state/accountsDB.go 71.42% 1 Missing and 1 partial ⚠️
state/peerAccountsDB.go 50.00% 1 Missing and 1 partial ⚠️
state/syncer/baseAccountsSyncer.go 0.00% 2 Missing ⚠️
update/factory/dataTrieFactory.go 0.00% 2 Missing ⚠️
Additional details and impacted files
@@                         Coverage Diff                          @@
##           feat/collapse-trie-based-on-size    #7191      +/-   ##
====================================================================
+ Coverage                             76.64%   77.53%   +0.89%     
====================================================================
  Files                                   823      884      +61     
  Lines                                110227   123727   +13500     
====================================================================
+ Hits                                  84484    95934   +11450     
- Misses                                20019    21441    +1422     
- Partials                               5724     6352     +628     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Comment thread epochStart/metachain/systemSCs_test.go Outdated
storageFactory "github.com/multiversx/mx-chain-go/storage/factory"
"github.com/multiversx/mx-chain-go/storage/storageunit"
"github.com/multiversx/mx-chain-go/testscommon"
common2 "github.com/multiversx/mx-chain-go/testscommon/common"
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

maybe rename "common2" in something else

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done.

Comment thread genesis/process/memoryComponents.go Outdated
"github.com/multiversx/mx-chain-go/trie"
)

const tenMbSize = uint64(10485760)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

maybe move this in the common package and use it everywhere

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done.

"github.com/multiversx/mx-chain-core-go/hashing/blake2b"
"github.com/multiversx/mx-chain-core-go/marshal"
"github.com/multiversx/mx-chain-go/integrationTests"
trie2 "github.com/multiversx/mx-chain-go/testscommon/common"
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

maybe rename "trie2" in something else

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done.

"github.com/multiversx/mx-chain-core-go/hashing/sha256"
crypto "github.com/multiversx/mx-chain-crypto-go"
"github.com/multiversx/mx-chain-go/epochStart/notifier"
common2 "github.com/multiversx/mx-chain-go/testscommon/common"
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

also here

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done.

"github.com/multiversx/mx-chain-go/common/errChan"
"github.com/multiversx/mx-chain-go/integrationTests"
"github.com/multiversx/mx-chain-go/state/parsers"
trie2 "github.com/multiversx/mx-chain-go/testscommon/common"
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

also here

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done.

Comment thread state/triesHolder/dataTriesHolder.go Outdated
return tries
}

func (dth *dataTriesHolder) getDirtyTrie(key string) common.Trie {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

maybe rename in getDirtyTrieNoLock ( similar with putNoLock )

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done.

Comment thread state/accountsDB_test.go Outdated
"github.com/multiversx/mx-chain-go/state/storagePruningManager/disabled"
"github.com/multiversx/mx-chain-go/state/storagePruningManager/evictionWaitingList"
"github.com/multiversx/mx-chain-go/testscommon"
common2 "github.com/multiversx/mx-chain-go/testscommon/common"
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

rename

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done.

Comment thread testscommon/integrationtests/factory.go Outdated
"github.com/multiversx/mx-chain-go/storage/factory"
"github.com/multiversx/mx-chain-go/storage/storageunit"
"github.com/multiversx/mx-chain-go/testscommon"
common2 "github.com/multiversx/mx-chain-go/testscommon/common"
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

rename

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done.


var emptyTrieHash = make([]byte, 32)

const tenMBSize = uint64(10485760)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

this const is defined in a lot of places

Copy link
Copy Markdown
Contributor Author

@BeniaminDrasovean BeniaminDrasovean Mar 12, 2026

Choose a reason for hiding this comment

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

It is refactored in the next PR.

Comment thread trie/trieStorageManagerInEpoch_test.go Outdated

"github.com/multiversx/mx-chain-go/storage"
"github.com/multiversx/mx-chain-go/testscommon"
storage2 "github.com/multiversx/mx-chain-go/testscommon/storage"
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

rename

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done.

Comment on lines +218 to +219
tenMBSize := uint64(10485760)
dataTrie, err := trie.NewTrie(b.trieStorageManager, b.marshalizer, b.hasher, b.enableEpochsHandler, tenMBSize)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

is this func still used? is it ok to set static size limit?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

It is not used anywhere.

Comment thread state/triesHolder/triesHolder_test.go Outdated
Comment on lines +70 to +75
for i := 0; i < numTries; i++ {
go func(key int) {
dth.Put([]byte(strconv.Itoa(key)), &trieMock.TrieStub{})
wg.Done()
}(i)
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

trigger all methods here?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done.

Comment thread trie/factory/trieCreator_test.go Outdated
Identifier: dataRetriever.UserAccountsUnit.String(),
EnableEpochsHandler: &enableEpochsHandlerMock.EnableEpochsHandlerStub{},
StatsCollector: disabled.NewStateStatistics(),
MaxSizeInMemory: 10 * 1024 * 1024, // 10 MB
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

use tenMBSize const here also

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done.

oldRoot []byte
chanClose chan struct{}
sizeInMemory int
maxSizeInMem uint64
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

max size in mem will be used later on in another PR?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yes. It will be moved to another component and used there.

Comment on lines +68 to +71
if len(dth.evictedBuffer) > 0 {
// this means that this trie was evicted while being dirty
delete(dth.evictedBuffer, keyString)
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

why try to delete current key if there are entries on evictedBuffer? just to make sure it's removed from evicted in case it might be there?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

If a dirty trie is evicted from the cache (this is an edgecase, if the cache is large enough it should never happen), we still need to commit it, so we keep it in dth.evictedBuffer until the commit is triggered. But if that trie is accessed again after it was evicted from the cache but before it was committed, we re-add it to the cache, so we can remove it from the dth.evictedBuffer.

@BeniaminDrasovean BeniaminDrasovean merged commit a1758e0 into feat/collapse-trie-based-on-size Mar 16, 2026
11 checks passed
@BeniaminDrasovean BeniaminDrasovean deleted the collapse-trie-based-on-size branch March 16, 2026 14:30
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.

4 participants