diff --git a/core/state/snapshot/disklayer.go b/core/state/snapshot/disklayer.go index f302652eb752..474182f1dd8a 100644 --- a/core/state/snapshot/disklayer.go +++ b/core/state/snapshot/disklayer.go @@ -19,7 +19,7 @@ package snapshot import ( "sync" - "github.com/allegro/bigcache" + "github.com/VictoriaMetrics/fastcache" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core/rawdb" "github.com/ethereum/go-ethereum/ethdb" @@ -30,7 +30,7 @@ import ( type diskLayer struct { journal string // Path of the snapshot journal to use on shutdown db ethdb.KeyValueStore // Key-value store containing the base snapshot - cache *bigcache.BigCache // Cache to avoid hitting the disk for direct access + cache *fastcache.Cache // Cache to avoid hitting the disk for direct access root common.Hash // Root hash of the base snapshot stale bool // Signals that the layer became stale (state progressed) @@ -80,17 +80,15 @@ func (dl *diskLayer) AccountRLP(hash common.Hash) ([]byte, error) { if dl.stale { return nil, ErrSnapshotStale } - key := string(hash[:]) - // Try to retrieve the account from the memory cache - if blob, err := dl.cache.Get(key); err == nil { + if blob := dl.cache.Get(nil, hash[:]); blob != nil { snapshotCleanHitMeter.Mark(1) snapshotCleanReadMeter.Mark(int64(len(blob))) return blob, nil } // Cache doesn't contain account, pull from disk and cache for later blob := rawdb.ReadAccountSnapshot(dl.db, hash) - dl.cache.Set(key, blob) + dl.cache.Set(hash[:], blob) snapshotCleanMissMeter.Mark(1) snapshotCleanWriteMeter.Mark(int64(len(blob))) @@ -109,10 +107,10 @@ func (dl *diskLayer) Storage(accountHash, storageHash common.Hash) ([]byte, erro if dl.stale { return nil, ErrSnapshotStale } - key := string(append(accountHash[:], storageHash[:]...)) + key := append(accountHash[:], storageHash[:]...) // Try to retrieve the storage slot from the memory cache - if blob, err := dl.cache.Get(key); err == nil { + if blob := dl.cache.Get(nil, key); blob != nil { snapshotCleanHitMeter.Mark(1) snapshotCleanReadMeter.Mark(int64(len(blob))) return blob, nil diff --git a/core/state/snapshot/generate.go b/core/state/snapshot/generate.go index a2197557e334..445a6ebd9f56 100644 --- a/core/state/snapshot/generate.go +++ b/core/state/snapshot/generate.go @@ -22,7 +22,7 @@ import ( "math/big" "time" - "github.com/allegro/bigcache" + "github.com/VictoriaMetrics/fastcache" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core/rawdb" "github.com/ethereum/go-ethereum/crypto" @@ -196,13 +196,7 @@ func generateSnapshot(db ethdb.KeyValueStore, journal string, root common.Hash) return nil, err } // New snapshot generated, construct a brand new base layer - cache, _ := bigcache.NewBigCache(bigcache.Config{ // TODO(karalabe): dedup - Shards: 1024, - LifeWindow: time.Hour, - MaxEntriesInWindow: 512 * 1024, - MaxEntrySize: 512, - HardMaxCacheSize: 512, - }) + cache := fastcache.New(512 * 1024 * 1024) return &diskLayer{ journal: journal, db: db, diff --git a/core/state/snapshot/snapshot.go b/core/state/snapshot/snapshot.go index edca5781d670..d35d698399bc 100644 --- a/core/state/snapshot/snapshot.go +++ b/core/state/snapshot/snapshot.go @@ -22,9 +22,8 @@ import ( "fmt" "os" "sync" - "time" - "github.com/allegro/bigcache" + "github.com/VictoriaMetrics/fastcache" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core/rawdb" "github.com/ethereum/go-ethereum/ethdb" @@ -323,7 +322,7 @@ func diffToDisk(bottom *diffLayer) *diskLayer { if len(data) > 0 { // Account was updated, push to disk rawdb.WriteAccountSnapshot(batch, hash, data) - base.cache.Set(string(hash[:]), data) + base.cache.Set(hash[:], data) if batch.ValueSize() > ethdb.IdealBatchSize { if err := batch.Write(); err != nil { @@ -334,13 +333,13 @@ func diffToDisk(bottom *diffLayer) *diskLayer { } else { // Account was deleted, remove all storage slots too rawdb.DeleteAccountSnapshot(batch, hash) - base.cache.Set(string(hash[:]), nil) + base.cache.Set(hash[:], nil) it := rawdb.IterateStorageSnapshots(base.db, hash) for it.Next() { if key := it.Key(); len(key) == 65 { // TODO(karalabe): Yuck, we should move this into the iterator batch.Delete(key) - base.cache.Delete(string(key[1:])) + base.cache.Del(key[1:]) } } it.Release() @@ -351,10 +350,10 @@ func diffToDisk(bottom *diffLayer) *diskLayer { for storageHash, data := range storage { if len(data) > 0 { rawdb.WriteStorageSnapshot(batch, accountHash, storageHash, data) - base.cache.Set(string(append(accountHash[:], storageHash[:]...)), data) + base.cache.Set(append(accountHash[:], storageHash[:]...), data) } else { rawdb.DeleteStorageSnapshot(batch, accountHash, storageHash) - base.cache.Set(string(append(accountHash[:], storageHash[:]...)), nil) + base.cache.Set(append(accountHash[:], storageHash[:]...), nil) } } if batch.ValueSize() > ethdb.IdealBatchSize { @@ -401,17 +400,10 @@ func loadSnapshot(db ethdb.KeyValueStore, journal string, root common.Hash) (sna if baseRoot == (common.Hash{}) { return nil, errors.New("missing or corrupted snapshot") } - cache, _ := bigcache.NewBigCache(bigcache.Config{ // TODO(karalabe): dedup - Shards: 1024, - LifeWindow: time.Hour, - MaxEntriesInWindow: 512 * 1024, - MaxEntrySize: 512, - HardMaxCacheSize: 512, - }) base := &diskLayer{ journal: journal, db: db, - cache: cache, + cache: fastcache.New(512 * 1024 * 1024), root: baseRoot, } // Load all the snapshot diffs from the journal, failing if their chain is broken diff --git a/core/state/snapshot/snapshot_test.go b/core/state/snapshot/snapshot_test.go index 40edf79e84d6..9c872a8955a7 100644 --- a/core/state/snapshot/snapshot_test.go +++ b/core/state/snapshot/snapshot_test.go @@ -19,9 +19,8 @@ package snapshot import ( "fmt" "testing" - "time" - "github.com/allegro/bigcache" + "github.com/VictoriaMetrics/fastcache" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core/rawdb" ) @@ -31,11 +30,10 @@ import ( // to check internal corner case around the bottom-most memory accumulator. func TestDiskLayerExternalInvalidationFullFlatten(t *testing.T) { // Create an empty base layer and a snapshot tree out of it - cache, _ := bigcache.NewBigCache(bigcache.DefaultConfig(time.Minute)) base := &diskLayer{ db: rawdb.NewMemoryDatabase(), root: common.HexToHash("0x01"), - cache: cache, + cache: fastcache.New(1024 * 500), } snaps := &Tree{ layers: map[common.Hash]snapshot{ @@ -77,11 +75,10 @@ func TestDiskLayerExternalInvalidationFullFlatten(t *testing.T) { // layer to check the usual mode of operation where the accumulator is retained. func TestDiskLayerExternalInvalidationPartialFlatten(t *testing.T) { // Create an empty base layer and a snapshot tree out of it - cache, _ := bigcache.NewBigCache(bigcache.DefaultConfig(time.Minute)) base := &diskLayer{ db: rawdb.NewMemoryDatabase(), root: common.HexToHash("0x01"), - cache: cache, + cache: fastcache.New(1024 * 500), } snaps := &Tree{ layers: map[common.Hash]snapshot{ @@ -126,11 +123,10 @@ func TestDiskLayerExternalInvalidationPartialFlatten(t *testing.T) { // to check internal corner case around the bottom-most memory accumulator. func TestDiffLayerExternalInvalidationFullFlatten(t *testing.T) { // Create an empty base layer and a snapshot tree out of it - cache, _ := bigcache.NewBigCache(bigcache.DefaultConfig(time.Minute)) base := &diskLayer{ db: rawdb.NewMemoryDatabase(), root: common.HexToHash("0x01"), - cache: cache, + cache: fastcache.New(1024 * 500), } snaps := &Tree{ layers: map[common.Hash]snapshot{ @@ -175,11 +171,10 @@ func TestDiffLayerExternalInvalidationFullFlatten(t *testing.T) { // layer to check the usual mode of operation where the accumulator is retained. func TestDiffLayerExternalInvalidationPartialFlatten(t *testing.T) { // Create an empty base layer and a snapshot tree out of it - cache, _ := bigcache.NewBigCache(bigcache.DefaultConfig(time.Minute)) base := &diskLayer{ db: rawdb.NewMemoryDatabase(), root: common.HexToHash("0x01"), - cache: cache, + cache: fastcache.New(1024 * 500), } snaps := &Tree{ layers: map[common.Hash]snapshot{ @@ -240,11 +235,10 @@ func TestPostCapBasicDataAccess(t *testing.T) { } } // Create a starting base layer and a snapshot tree out of it - cache, _ := bigcache.NewBigCache(bigcache.DefaultConfig(time.Minute)) base := &diskLayer{ db: rawdb.NewMemoryDatabase(), root: common.HexToHash("0x01"), - cache: cache, + cache: fastcache.New(1024 * 500), } snaps := &Tree{ layers: map[common.Hash]snapshot{