mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-26 06:36:43 +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) {
|
func (dc *DirectCache) TryGet(key []byte) ([]byte, error) {
|
||||||
start := time.Now()
|
start := time.Now()
|
||||||
|
|
||||||
|
dirty := dc.dirty[string(key)]
|
||||||
|
|
||||||
// Use the underlying object for dirty keys
|
// Use the underlying object for dirty keys
|
||||||
if !dc.dirty[string(key)] {
|
if !dirty {
|
||||||
cacheKey := dc.makeKey(key)
|
cacheKey := dc.makeKey(key)
|
||||||
if cached, ok := dc.getCached(cacheKey); ok {
|
if cached, ok := dc.getCached(cacheKey); ok {
|
||||||
directCacheHitTimer.UpdateSince(start)
|
directCacheHitTimer.UpdateSince(start)
|
||||||
|
|
@ -103,14 +105,16 @@ func (dc *DirectCache) TryGet(key []byte) ([]byte, error) {
|
||||||
return value, err
|
return value, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// If the cache isn't comprehensive, update it
|
if !dc.dirty[string(key)] {
|
||||||
if !dc.complete && !dc.dirty[string(key)] {
|
// Flag the key as dirty so it gets written at commit time
|
||||||
if err := dc.putCache(dc.db, key, value); err != nil && glog.V(logger.Error) {
|
dc.dirty[string(key)] = true
|
||||||
glog.Errorf("Error updating cache: %v", err)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Don't count fetches of dirty data as cache misses
|
||||||
|
if !dirty {
|
||||||
directCacheMissTimer.UpdateSince(start)
|
directCacheMissTimer.UpdateSince(start)
|
||||||
|
}
|
||||||
|
|
||||||
return value, nil
|
return value, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -126,15 +130,8 @@ func (dc *DirectCache) getCached(key []byte) ([]byte, bool) {
|
||||||
return nil, false
|
return nil, false
|
||||||
}
|
}
|
||||||
|
|
||||||
return data.Value, dc.validator.IsCanonChainBlock(data.BlockNum, data.BlockHash)
|
canonical := dc.validator.IsCanonChainBlock(data.BlockNum, data.BlockHash)
|
||||||
}
|
return data.Value, canonical
|
||||||
|
|
||||||
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
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (dc *DirectCache) Update(key, value []byte) {
|
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)
|
dc.dirty = make(map[string]bool)
|
||||||
return dc.data.CommitTo(dbw)
|
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