mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-25 22:26:42 +00:00
trie: Write cache misses at commit time instead of immediately.
This commit is contained in:
parent
c4e5dc3bea
commit
199b4de693
1 changed files with 21 additions and 16 deletions
|
|
@ -89,8 +89,10 @@ func (dc *DirectCache) Get(key []byte) []byte {
|
|||
func (dc *DirectCache) TryGet(key []byte) ([]byte, error) {
|
||||
start := time.Now()
|
||||
|
||||
dirty := dc.dirty[string(key)]
|
||||
|
||||
// Use the underlying object for dirty keys
|
||||
if !dc.dirty[string(key)] {
|
||||
if !dirty {
|
||||
cacheKey := dc.makeKey(key)
|
||||
if cached, ok := dc.getCached(cacheKey); ok {
|
||||
directCacheHitTimer.UpdateSince(start)
|
||||
|
|
@ -103,14 +105,16 @@ func (dc *DirectCache) TryGet(key []byte) ([]byte, error) {
|
|||
return value, err
|
||||
}
|
||||
|
||||
// If the cache isn't comprehensive, update it
|
||||
if !dc.complete && !dc.dirty[string(key)] {
|
||||
if err := dc.putCache(dc.db, key, value); err != nil && glog.V(logger.Error) {
|
||||
glog.Errorf("Error updating cache: %v", err)
|
||||
}
|
||||
if !dc.dirty[string(key)] {
|
||||
// Flag the key as dirty so it gets written at commit time
|
||||
dc.dirty[string(key)] = true
|
||||
}
|
||||
|
||||
// Don't count fetches of dirty data as cache misses
|
||||
if !dirty {
|
||||
directCacheMissTimer.UpdateSince(start)
|
||||
}
|
||||
|
||||
directCacheMissTimer.UpdateSince(start)
|
||||
return value, nil
|
||||
}
|
||||
|
||||
|
|
@ -126,15 +130,8 @@ func (dc *DirectCache) getCached(key []byte) ([]byte, bool) {
|
|||
return nil, false
|
||||
}
|
||||
|
||||
return data.Value, dc.validator.IsCanonChainBlock(data.BlockNum, data.BlockHash)
|
||||
}
|
||||
|
||||
func (dc *DirectCache) putCache(dbw DatabaseWriter, key, value []byte) error {
|
||||
enc, _ := rlp.EncodeToBytes(cachedValue{value, dc.blockNum, dc.blockHash})
|
||||
if err := dbw.Put(append(dc.keyPrefix, key...), enc); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
canonical := dc.validator.IsCanonChainBlock(data.BlockNum, data.BlockHash)
|
||||
return data.Value, canonical
|
||||
}
|
||||
|
||||
func (dc *DirectCache) Update(key, value []byte) {
|
||||
|
|
@ -173,3 +170,11 @@ func (dc *DirectCache) CommitTo(dbw DatabaseWriter) (root common.Hash, err error
|
|||
dc.dirty = make(map[string]bool)
|
||||
return dc.data.CommitTo(dbw)
|
||||
}
|
||||
|
||||
func (dc *DirectCache) putCache(dbw DatabaseWriter, key, value []byte) error {
|
||||
enc, _ := rlp.EncodeToBytes(cachedValue{value, dc.blockNum, dc.blockHash})
|
||||
if err := dbw.Put(append(dc.keyPrefix, key...), enc); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue