trie: Write cache misses at commit time instead of immediately.

This commit is contained in:
Nick Johnson 2016-10-25 17:10:52 +01:00
parent c4e5dc3bea
commit 199b4de693

View file

@ -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
}