From f15bc8d34515abc0558b650da7408a971bb44f89 Mon Sep 17 00:00:00 2001 From: Delweng Date: Wed, 23 Jul 2025 21:12:57 +0800 Subject: [PATCH] a simple index cache Signed-off-by: Delweng --- triedb/pathdb/history_index.go | 18 +++++++++++++++--- triedb/pathdb/history_reader.go | 5 +++++ 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/triedb/pathdb/history_index.go b/triedb/pathdb/history_index.go index 77b58b3a88..ceaeadced7 100644 --- a/triedb/pathdb/history_index.go +++ b/triedb/pathdb/history_index.go @@ -78,15 +78,27 @@ type indexReader struct { timings *readTimings } -// loadIndexData loads the index data associated with the specified state. -func loadIndexData(db ethdb.KeyValueReader, state stateIdent, timings *readTimings) ([]*indexBlockDesc, error) { +func readHistoryIndexWithCache(db ethdb.KeyValueReader, state stateIdent) []byte { + key := state.String() + if val, ok := historyIndexCache.Get(key); ok { + return val + } var blob []byte - start := time.Now() if state.account { blob = rawdb.ReadAccountHistoryIndex(db, state.addressHash) } else { 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 { timings.kvdbIndex = time.Since(start) } diff --git a/triedb/pathdb/history_reader.go b/triedb/pathdb/history_reader.go index 799a773f94..246f7fb4e5 100644 --- a/triedb/pathdb/history_reader.go +++ b/triedb/pathdb/history_reader.go @@ -26,11 +26,16 @@ import ( "time" "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/ethdb" "github.com/ethereum/go-ethereum/log" ) +var ( + historyIndexCache = lru.NewCache[string, []byte](10000) +) + // stateIdent represents the identifier of a state element, which can be // either an account or a storage slot. type stateIdent struct {