Skip to content

Commit

Permalink
pathdb: adjust plain account and storage cache ratio
Browse files Browse the repository at this point in the history
  • Loading branch information
fynnss committed Apr 15, 2024
1 parent 08878e4 commit d33cc0b
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 8 deletions.
23 changes: 23 additions & 0 deletions core/types/state_account.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package types

import (
"bytes"
"fmt"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/rlp"
Expand Down Expand Up @@ -87,6 +88,17 @@ func SlimAccountRLP(account StateAccount) []byte {
return data
}

func FullToSlimAccountRLP(data []byte) []byte {
if data == nil {
return nil
}
var account StateAccount
if err := rlp.DecodeBytes(data, &account); err != nil {
panic(fmt.Sprintf("full to slim account rlp failed, err %v", err))
}
return SlimAccountRLP(account)
}

// FullAccount decodes the data on the 'slim RLP' format and returns
// the consensus format account.
func FullAccount(data []byte) (*StateAccount, error) {
Expand Down Expand Up @@ -119,3 +131,14 @@ func FullAccountRLP(data []byte) ([]byte, error) {
}
return rlp.EncodeToBytes(account)
}

func MustFullAccountRLP(data []byte) []byte {
if data == nil {
return nil
}
val, err := FullAccountRLP(data)
if err != nil {
panic(fmt.Sprintf("must full account rlp faield, err %v", err))
}
return val
}
3 changes: 2 additions & 1 deletion triedb/pathdb/asyncnodebuffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"sync/atomic"
"time"

"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/trie/triestate"

"github.com/ethereum/go-ethereum/common"
Expand Down Expand Up @@ -449,7 +450,7 @@ func (nc *nodecache) flush(db ethdb.KeyValueStore, clean *cleanCache, id uint64)
clean.plainStorages.Reset()
}
for h, acc := range nc.LatestAccounts {
clean.plainAccounts.Set(h.Bytes(), acc)
clean.plainAccounts.Set(h.Bytes(), types.FullToSlimAccountRLP(acc))
}

for h, storages := range nc.LatestStorages {
Expand Down
18 changes: 11 additions & 7 deletions triedb/pathdb/disklayer.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"github.com/VictoriaMetrics/fastcache"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/rawdb"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/ethdb"
"github.com/ethereum/go-ethereum/log"
Expand Down Expand Up @@ -127,11 +128,14 @@ func newDiskLayer(root common.Hash, id uint64, db *Database, cleans *cleanCache,
// the original disk layer).
if cleans == nil && db.config.CleanCacheSize != 0 {
cleans = &cleanCache{}
cleans.nodes = fastcache.New(db.config.CleanCacheSize * 1 / 2)
cleans.plainAccounts = fastcache.New(db.config.CleanCacheSize * 1 / 4)
cleans.plainStorages = fastcache.New(db.config.CleanCacheSize * 1 / 4)
log.Info("Allocate clean cache in disklayer", "nodes", common.StorageSize(db.config.CleanCacheSize*1/2),
"plainAccount", common.StorageSize(db.config.CleanCacheSize*1/4), "plainStorage", common.StorageSize(db.config.CleanCacheSize*1/4))
nodeCacheSize := db.config.CleanCacheSize * 42 / 100
plainAccountsCacheSize := db.config.CleanCacheSize * 58 / 100 * 1 / 4
plainStoragesCacheSize := db.config.CleanCacheSize * 58 / 100 * 3 / 4
cleans.nodes = fastcache.New(nodeCacheSize)
cleans.plainAccounts = fastcache.New(plainAccountsCacheSize)
cleans.plainStorages = fastcache.New(plainStoragesCacheSize)
log.Info("Allocate clean cache in disklayer", "nodes", common.StorageSize(nodeCacheSize),
"plainAccount", common.StorageSize(plainAccountsCacheSize), "plainStorage", common.StorageSize(plainStoragesCacheSize))
}
return &diskLayer{
root: root,
Expand Down Expand Up @@ -189,11 +193,11 @@ func (dl *diskLayer) Account(hash common.Hash) ([]byte, error) {
}

if data, ok := dl.cleans.plainAccounts.HasGet(nil, hash.Bytes()); ok {
return data, nil
return types.MustFullAccountRLP(data), nil
}

blob := dl.readAccountTrie(hash)
dl.cleans.plainAccounts.Set(hash.Bytes(), blob)
dl.cleans.plainAccounts.Set(hash.Bytes(), types.FullToSlimAccountRLP(blob))
return blob, nil
}

Expand Down

0 comments on commit d33cc0b

Please sign in to comment.