core/state: replace fastcache code cache with gc-friendly structure (#26092)

This PR replaces fastcache with a pretty simple LRU which does not require explicit closing.
This commit is contained in:
Martin Holst Swende
2022-11-09 08:06:02 +01:00
committed by GitHub
parent 7dc5e785a8
commit 5fded04037
3 changed files with 217 additions and 7 deletions
+7 -7
View File
@@ -20,8 +20,8 @@ import (
"errors"
"fmt"
"github.com/VictoriaMetrics/fastcache"
"github.com/ethereum/go-ethereum/common"
lru2 "github.com/ethereum/go-ethereum/common/lru"
"github.com/ethereum/go-ethereum/core/rawdb"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/ethdb"
@@ -135,7 +135,7 @@ func NewDatabaseWithConfig(db ethdb.Database, config *trie.Config) Database {
db: trie.NewDatabaseWithConfig(db, config),
disk: db,
codeSizeCache: csc,
codeCache: fastcache.New(codeCacheSize),
codeCache: lru2.NewSizeConstrainedLRU(codeCacheSize),
}
}
@@ -143,7 +143,7 @@ type cachingDB struct {
db *trie.Database
disk ethdb.KeyValueStore
codeSizeCache *lru.Cache
codeCache *fastcache.Cache
codeCache *lru2.SizeConstrainedLRU
}
// OpenTrie opens the main account trie at a specific root hash.
@@ -176,12 +176,12 @@ func (db *cachingDB) CopyTrie(t Trie) Trie {
// ContractCode retrieves a particular contract's code.
func (db *cachingDB) ContractCode(addrHash, codeHash common.Hash) ([]byte, error) {
if code := db.codeCache.Get(nil, codeHash.Bytes()); len(code) > 0 {
if code := db.codeCache.Get(codeHash); len(code) > 0 {
return code, nil
}
code := rawdb.ReadCode(db.disk, codeHash)
if len(code) > 0 {
db.codeCache.Set(codeHash.Bytes(), code)
db.codeCache.Add(codeHash, code)
db.codeSizeCache.Add(codeHash, len(code))
return code, nil
}
@@ -192,12 +192,12 @@ func (db *cachingDB) ContractCode(addrHash, codeHash common.Hash) ([]byte, error
// code can't be found in the cache, then check the existence with **new**
// db scheme.
func (db *cachingDB) ContractCodeWithPrefix(addrHash, codeHash common.Hash) ([]byte, error) {
if code := db.codeCache.Get(nil, codeHash.Bytes()); len(code) > 0 {
if code := db.codeCache.Get(codeHash); len(code) > 0 {
return code, nil
}
code := rawdb.ReadCodeWithPrefix(db.disk, codeHash)
if len(code) > 0 {
db.codeCache.Set(codeHash.Bytes(), code)
db.codeCache.Add(codeHash, code)
db.codeSizeCache.Add(codeHash, len(code))
return code, nil
}