a simple index cache

Signed-off-by: Delweng <delweng@gmail.com>
This commit is contained in:
Delweng 2025-07-23 21:12:57 +08:00 committed by jsvisa
parent 604daadaac
commit f15bc8d345
2 changed files with 20 additions and 3 deletions

View file

@ -78,15 +78,27 @@ type indexReader struct {
timings *readTimings timings *readTimings
} }
// loadIndexData loads the index data associated with the specified state. func readHistoryIndexWithCache(db ethdb.KeyValueReader, state stateIdent) []byte {
func loadIndexData(db ethdb.KeyValueReader, state stateIdent, timings *readTimings) ([]*indexBlockDesc, error) { key := state.String()
if val, ok := historyIndexCache.Get(key); ok {
return val
}
var blob []byte var blob []byte
start := time.Now()
if state.account { if state.account {
blob = rawdb.ReadAccountHistoryIndex(db, state.addressHash) blob = rawdb.ReadAccountHistoryIndex(db, state.addressHash)
} else { } else {
blob = rawdb.ReadStorageHistoryIndex(db, state.addressHash, state.storageHash) blob = rawdb.ReadStorageHistoryIndex(db, state.addressHash, state.storageHash)
} }
if len(blob) > 0 {
historyIndexCache.Add(key, blob)
}
return blob
}
// loadIndexData loads the index data associated with the specified state.
func loadIndexData(db ethdb.KeyValueReader, state stateIdent, timings *readTimings) ([]*indexBlockDesc, error) {
start := time.Now()
blob := readHistoryIndexWithCache(db, state)
if timings != nil { if timings != nil {
timings.kvdbIndex = time.Since(start) timings.kvdbIndex = time.Since(start)
} }

View file

@ -26,11 +26,16 @@ import (
"time" "time"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/lru"
"github.com/ethereum/go-ethereum/core/rawdb" "github.com/ethereum/go-ethereum/core/rawdb"
"github.com/ethereum/go-ethereum/ethdb" "github.com/ethereum/go-ethereum/ethdb"
"github.com/ethereum/go-ethereum/log" "github.com/ethereum/go-ethereum/log"
) )
var (
historyIndexCache = lru.NewCache[string, []byte](10000)
)
// stateIdent represents the identifier of a state element, which can be // stateIdent represents the identifier of a state element, which can be
// either an account or a storage slot. // either an account or a storage slot.
type stateIdent struct { type stateIdent struct {