Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

core/state, trie: fix memleak from fastcache #26071

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion core/state/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package state
import (
"errors"
"fmt"
"runtime"

"github.com/VictoriaMetrics/fastcache"
"github.com/ethereum/go-ethereum/common"
Expand Down Expand Up @@ -131,12 +132,14 @@ func NewDatabase(db ethdb.Database) Database {
// large memory cache.
func NewDatabaseWithConfig(db ethdb.Database, config *trie.Config) Database {
csc, _ := lru.New(codeSizeCacheSize)
return &cachingDB{
cdb := &cachingDB{
db: trie.NewDatabaseWithConfig(db, config),
disk: db,
codeSizeCache: csc,
codeCache: fastcache.New(codeCacheSize),
}
runtime.SetFinalizer(cdb, (*cachingDB).finalizer)
Copy link
Member

Choose a reason for hiding this comment

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

I feel like it's unnecessary to use fastcache here as the codeCache. fastCache is mostly for caching large amount of data for GC-friendly purpose. Obviously not the case here.

return cdb
}

type cachingDB struct {
Expand All @@ -155,6 +158,11 @@ func (db *cachingDB) OpenTrie(root common.Hash) (Trie, error) {
return tr, nil
}

// must call Reset() to reclaim memory used by fastcache
func (db *cachingDB) finalizer() {
db.codeCache.Reset()
}

// OpenStorageTrie opens the storage trie of an account.
func (db *cachingDB) OpenStorageTrie(stateRoot common.Hash, addrHash, root common.Hash) (Trie, error) {
tr, err := trie.NewStateTrie(trie.StorageTrieID(stateRoot, addrHash, root), db.db)
Expand Down
8 changes: 8 additions & 0 deletions trie/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,9 +301,17 @@ func NewDatabaseWithConfig(diskdb ethdb.KeyValueStore, config *Config) *Database
}},
preimages: preimage,
}
runtime.SetFinalizer(db, (*Database).finalizer)
return db
}

// must call Reset() to reclaim memory used by fastcache
func (db *Database) finalizer() {
if db.cleans != nil {
db.cleans.Reset()
}
}

// insert inserts a simplified trie node into the memory database.
// All nodes inserted by this function will be reference tracked
// and in theory should only used for **trie nodes** insertion.
Expand Down