Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

core/state/snapshot: reuse memory data instead of hitting disk #22700

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions core/state/snapshot/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
"github.com/ethereum/go-ethereum/core/rawdb"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/ethdb"
"github.com/ethereum/go-ethereum/ethdb/memorydb"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/metrics"
"github.com/ethereum/go-ethereum/rlp"
Expand Down Expand Up @@ -434,6 +435,25 @@ func (dl *diskLayer) generateRange(root common.Hash, prefix []byte, kind string,
}
meter.Mark(1)
}
// We use the snap data to build up a cache which can be used by the main
// account trie as a short-circuit when resolving hashes
if len(result.keys) > 0 {
var (
cache = memorydb.New()
triedb = trie.NewDatabase(cache)
trie, _ = trie.New(common.Hash{}, triedb)
)
for i, key := range result.keys {
trie.Update(key, result.vals[i])
}
trie.Commit(nil)

it := cache.NewIterator(nil, nil)
for it.Next() {
dl.triedb.Seed(common.BytesToHash(it.Key()), common.CopyBytes(it.Value()))
}
it.Release()
}
tr := result.tr
if tr == nil {
tr, err = trie.New(root, dl.triedb)
Expand Down
12 changes: 12 additions & 0 deletions trie/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -893,3 +893,15 @@ func (db *Database) SaveCachePeriodically(dir string, interval time.Duration, st
}
}
}

// Seed can be used to inject a single trie node into the clean cache. Its main
// purpose is to allow operating on trie nodes that are known to be on disk, but
// if they are available in RAM too (generated from snapshot), instead of loading
// again from persistent storage (expensive), rather inject into the cache to be
// loaded from there (cheap).
func (db *Database) Seed(key common.Hash, val []byte) {
db.lock.RLock()
defer db.lock.RUnlock()

db.cleans.Set(key[:], val)
}