aboutsummaryrefslogtreecommitdiffstats
path: root/trie/cache.go
diff options
context:
space:
mode:
authorJeffrey Wilcke <jeffrey@ethereum.org>2015-06-30 06:44:23 +0800
committerJeffrey Wilcke <jeffrey@ethereum.org>2015-06-30 06:44:23 +0800
commit7c4ed8055cc036214279e3ebded74c58d6808d05 (patch)
treeb35a10b7a41c5e1f03300c7afb51f8a76202dbf8 /trie/cache.go
parent9d8b512b27f691fc1980b850e04eb436a3938626 (diff)
parent992e4f83cb05946fa53132a9184d4ac3f38b62bc (diff)
downloadgo-tangerine-7c4ed8055cc036214279e3ebded74c58d6808d05.tar.gz
go-tangerine-7c4ed8055cc036214279e3ebded74c58d6808d05.tar.zst
go-tangerine-7c4ed8055cc036214279e3ebded74c58d6808d05.zip
Merge pull request #1357 from obscuren/core-optimisations-2
core: optimisations
Diffstat (limited to 'trie/cache.go')
-rw-r--r--trie/cache.go26
1 files changed, 18 insertions, 8 deletions
diff --git a/trie/cache.go b/trie/cache.go
index 4c76c6cba..cb805d2f2 100644
--- a/trie/cache.go
+++ b/trie/cache.go
@@ -1,6 +1,11 @@
package trie
-import "github.com/ethereum/go-ethereum/logger/glog"
+import (
+ "github.com/ethereum/go-ethereum/compression/rle"
+ "github.com/ethereum/go-ethereum/ethdb"
+ "github.com/ethereum/go-ethereum/logger/glog"
+ "github.com/syndtr/goleveldb/leveldb"
+)
type Backend interface {
Get([]byte) ([]byte, error)
@@ -8,12 +13,13 @@ type Backend interface {
}
type Cache struct {
+ batch *leveldb.Batch
store map[string][]byte
backend Backend
}
func NewCache(backend Backend) *Cache {
- return &Cache{make(map[string][]byte), backend}
+ return &Cache{new(leveldb.Batch), make(map[string][]byte), backend}
}
func (self *Cache) Get(key []byte) []byte {
@@ -26,19 +32,23 @@ func (self *Cache) Get(key []byte) []byte {
}
func (self *Cache) Put(key []byte, data []byte) {
+ // write the data to the ldb batch
+ self.batch.Put(key, rle.Compress(data))
self.store[string(key)] = data
}
+// Flush flushes the trie to the backing layer. If this is a leveldb instance
+// we'll use a batched write, otherwise we'll use regular put.
func (self *Cache) Flush() {
- for k, v := range self.store {
- if err := self.backend.Put([]byte(k), v); err != nil {
+ if db, ok := self.backend.(*ethdb.LDBDatabase); ok {
+ if err := db.LDB().Write(self.batch, nil); err != nil {
glog.Fatal("db write err:", err)
}
+ } else {
+ for k, v := range self.store {
+ self.backend.Put([]byte(k), v)
+ }
}
-
- // This will eventually grow too large. We'd could
- // do a make limit on storage and push out not-so-popular nodes.
- //self.Reset()
}
func (self *Cache) Copy() *Cache {