Skip to content

Commit

Permalink
vendor, trie: replace bigCache with fastCache
Browse files Browse the repository at this point in the history
  • Loading branch information
rjl493456442 committed Aug 16, 2019
1 parent c2c4c9f commit 876b176
Show file tree
Hide file tree
Showing 11 changed files with 1,207 additions and 16 deletions.
25 changes: 9 additions & 16 deletions trie/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (
"sync"
"time"

"github.com/allegro/bigcache"
"github.com/VictoriaMetrics/fastcache"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/ethdb"
"github.com/ethereum/go-ethereum/log"
Expand Down Expand Up @@ -69,7 +69,7 @@ const secureKeyLength = 11 + 32
type Database struct {
diskdb ethdb.KeyValueStore // Persistent storage for matured trie nodes

cleans *bigcache.BigCache // GC friendly memory cache of clean node RLPs
cleans *fastcache.Cache // GC friendly memory cache of clean node RLPs
dirties map[common.Hash]*cachedNode // Data and references relationships of dirty nodes
oldest common.Hash // Oldest tracked node, flush-list head
newest common.Hash // Newest tracked node, flush-list tail
Expand Down Expand Up @@ -299,16 +299,9 @@ func NewDatabase(diskdb ethdb.KeyValueStore) *Database {
// before its written out to disk or garbage collected. It also acts as a read cache
// for nodes loaded from disk.
func NewDatabaseWithCache(diskdb ethdb.KeyValueStore, cache int) *Database {
var cleans *bigcache.BigCache
var cleans *fastcache.Cache
if cache > 0 {
cleans, _ = bigcache.NewBigCache(bigcache.Config{
Shards: 1024,
LifeWindow: time.Hour,
MaxEntriesInWindow: cache * 1024,
MaxEntrySize: 512,
HardMaxCacheSize: cache,
Hasher: trienodeHasher{},
})
cleans = fastcache.New(cache * 1024 * 1024)
}
return &Database{
diskdb: diskdb,
Expand Down Expand Up @@ -384,7 +377,7 @@ func (db *Database) insertPreimage(hash common.Hash, preimage []byte) {
func (db *Database) node(hash common.Hash) node {
// Retrieve the node from the clean cache if available
if db.cleans != nil {
if enc, err := db.cleans.Get(string(hash[:])); err == nil && enc != nil {
if enc := db.cleans.Get(nil, hash[:]); enc != nil {
memcacheCleanHitMeter.Mark(1)
memcacheCleanReadMeter.Mark(int64(len(enc)))
return mustDecodeNode(hash[:], enc)
Expand All @@ -404,7 +397,7 @@ func (db *Database) node(hash common.Hash) node {
return nil
}
if db.cleans != nil {
db.cleans.Set(string(hash[:]), enc)
db.cleans.Set(hash[:], enc)
memcacheCleanMissMeter.Mark(1)
memcacheCleanWriteMeter.Mark(int64(len(enc)))
}
Expand All @@ -420,7 +413,7 @@ func (db *Database) Node(hash common.Hash) ([]byte, error) {
}
// Retrieve the node from the clean cache if available
if db.cleans != nil {
if enc, err := db.cleans.Get(string(hash[:])); err == nil && enc != nil {
if enc := db.cleans.Get(nil, hash[:]); enc != nil {
memcacheCleanHitMeter.Mark(1)
memcacheCleanReadMeter.Mark(int64(len(enc)))
return enc, nil
Expand All @@ -438,7 +431,7 @@ func (db *Database) Node(hash common.Hash) ([]byte, error) {
enc, err := db.diskdb.Get(hash[:])
if err == nil && enc != nil {
if db.cleans != nil {
db.cleans.Set(string(hash[:]), enc)
db.cleans.Set(hash[:], enc)
memcacheCleanMissMeter.Mark(1)
memcacheCleanWriteMeter.Mark(int64(len(enc)))
}
Expand Down Expand Up @@ -835,7 +828,7 @@ func (c *cleaner) Put(key []byte, rlp []byte) error {
}
// Move the flushed node into the clean cache to prevent insta-reloads
if c.db.cleans != nil {
c.db.cleans.Set(string(hash[:]), rlp)
c.db.cleans.Set(hash[:], rlp)
}
return nil
}
Expand Down
22 changes: 22 additions & 0 deletions vendor/github.com/VictoriaMetrics/fastcache/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

116 changes: 116 additions & 0 deletions vendor/github.com/VictoriaMetrics/fastcache/README.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

152 changes: 152 additions & 0 deletions vendor/github.com/VictoriaMetrics/fastcache/bigcache.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 876b176

Please sign in to comment.