From e9b155637645bf99220fb5e148924a689d7253ed Mon Sep 17 00:00:00 2001 From: otherview Date: Mon, 18 Nov 2024 17:41:48 +0000 Subject: [PATCH 1/2] Add empty cache for inmem ops --- muxdb/backend.go | 2 +- muxdb/cache.go | 21 +++++++++------------ muxdb/empty_cache.go | 22 ++++++++++++++++++++++ muxdb/muxdb.go | 2 +- muxdb/trie_test.go | 2 +- 5 files changed, 34 insertions(+), 15 deletions(-) create mode 100644 muxdb/empty_cache.go diff --git a/muxdb/backend.go b/muxdb/backend.go index f8188a8d3..4d63a2d6e 100644 --- a/muxdb/backend.go +++ b/muxdb/backend.go @@ -17,7 +17,7 @@ import ( // backend is the backend of the trie. type backend struct { Store kv.Store - Cache *cache + Cache Cache HistPtnFactor, DedupedPtnFactor uint32 CachedNodeTTL uint16 } diff --git a/muxdb/cache.go b/muxdb/cache.go index fe0313724..6a3db295f 100644 --- a/muxdb/cache.go +++ b/muxdb/cache.go @@ -17,6 +17,13 @@ import ( "github.com/vechain/thor/v2/trie" ) +type Cache interface { + AddNodeBlob(keyBuf *[]byte, name string, path []byte, ver trie.Version, blob []byte, isCommitting bool) + GetNodeBlob(keyBuf *[]byte, name string, path []byte, ver trie.Version, peek bool) []byte + AddRootNode(name string, n trie.Node) + GetRootNode(name string, ver trie.Version) trie.Node +} + // cache is the cache layer for trie. type cache struct { queriedNodes *directcache.Cache // caches recently queried node blobs. @@ -34,7 +41,7 @@ type cache struct { } // newCache creates a cache object with the given cache size. -func newCache(sizeMB int, rootTTL uint32) *cache { +func newCache(sizeMB int, rootTTL uint32) Cache { sizeBytes := sizeMB * 1024 * 1024 cache := &cache{ queriedNodes: directcache.New(sizeBytes / 4), @@ -65,10 +72,6 @@ func (c *cache) log() { // AddNodeBlob adds encoded node blob into the cache. func (c *cache) AddNodeBlob(keyBuf *[]byte, name string, path []byte, ver trie.Version, blob []byte, isCommitting bool) { - if c == nil { - return - } - // the version part v := binary.AppendUvarint((*keyBuf)[:0], uint64(ver.Major)) v = binary.AppendUvarint(v, uint64(ver.Minor)) @@ -89,9 +92,6 @@ func (c *cache) AddNodeBlob(keyBuf *[]byte, name string, path []byte, ver trie.V // GetNodeBlob returns the cached node blob. func (c *cache) GetNodeBlob(keyBuf *[]byte, name string, path []byte, ver trie.Version, peek bool) []byte { - if c == nil { - return nil - } // the version part v := binary.AppendUvarint((*keyBuf)[:0], uint64(ver.Major)) v = binary.AppendUvarint(v, uint64(ver.Minor)) @@ -130,7 +130,7 @@ func (c *cache) GetNodeBlob(keyBuf *[]byte, name string, path []byte, ver trie.V // AddRootNode add the root node into the cache. func (c *cache) AddRootNode(name string, n trie.Node) { - if c == nil || n == nil { + if n == nil { return } c.roots.lock.Lock() @@ -151,9 +151,6 @@ func (c *cache) AddRootNode(name string, n trie.Node) { // GetRootNode returns the cached root node. func (c *cache) GetRootNode(name string, ver trie.Version) trie.Node { - if c == nil { - return nil - } c.roots.lock.RLock() defer c.roots.lock.RUnlock() diff --git a/muxdb/empty_cache.go b/muxdb/empty_cache.go new file mode 100644 index 000000000..09678e1ee --- /dev/null +++ b/muxdb/empty_cache.go @@ -0,0 +1,22 @@ +package muxdb + +import "github.com/vechain/thor/v2/trie" + +type emptyCache struct{} + +// AddNodeBlob does nothing. +func (e *emptyCache) AddNodeBlob(_ *[]byte, _ string, _ []byte, _ trie.Version, _ []byte, _ bool) { +} + +// GetNodeBlob always returns nil. +func (e *emptyCache) GetNodeBlob(_ *[]byte, _ string, _ []byte, _ trie.Version, _ bool) []byte { + return nil +} + +// AddRootNode does nothing. +func (e *emptyCache) AddRootNode(_ string, _ trie.Node) {} + +// GetRootNode always returns nil. +func (e *emptyCache) GetRootNode(_ string, _ trie.Version) trie.Node { + return nil +} diff --git a/muxdb/muxdb.go b/muxdb/muxdb.go index dad855165..ce83675cd 100644 --- a/muxdb/muxdb.go +++ b/muxdb/muxdb.go @@ -127,7 +127,7 @@ func NewMem() *MuxDB { engine: engine, trieBackend: &backend{ Store: engine, - Cache: nil, + Cache: &emptyCache{}, HistPtnFactor: 1, DedupedPtnFactor: 1, CachedNodeTTL: 32, diff --git a/muxdb/trie_test.go b/muxdb/trie_test.go index 097f93802..19b66a9b2 100644 --- a/muxdb/trie_test.go +++ b/muxdb/trie_test.go @@ -26,7 +26,7 @@ func newTestBackend() *backend { engine := newTestEngine() return &backend{ Store: engine, - Cache: nil, + Cache: &emptyCache{}, HistPtnFactor: 1, DedupedPtnFactor: 1, CachedNodeTTL: 100, From 5d9b776a2c2f307659c5136482062b79a4ff1349 Mon Sep 17 00:00:00 2001 From: otherview Date: Tue, 19 Nov 2024 10:04:26 +0000 Subject: [PATCH 2/2] changing name to dummyCache --- muxdb/cache.go | 18 ++++++++++++++++++ muxdb/empty_cache.go | 22 ---------------------- muxdb/muxdb.go | 2 +- muxdb/trie_test.go | 2 +- 4 files changed, 20 insertions(+), 24 deletions(-) delete mode 100644 muxdb/empty_cache.go diff --git a/muxdb/cache.go b/muxdb/cache.go index 6a3db295f..925e03672 100644 --- a/muxdb/cache.go +++ b/muxdb/cache.go @@ -197,3 +197,21 @@ func (cs *cacheStats) ShouldLog(msg string) (func(), bool) { cs.flag.Store(flag) }, cs.flag.Load() != flag } + +type dummyCache struct{} + +// AddNodeBlob is a no-op. +func (*dummyCache) AddNodeBlob(_ *[]byte, _ string, _ []byte, _ trie.Version, _ []byte, _ bool) {} + +// GetNodeBlob always returns nil. +func (*dummyCache) GetNodeBlob(_ *[]byte, _ string, _ []byte, _ trie.Version, _ bool) []byte { + return nil +} + +// AddRootNode is a no-op. +func (*dummyCache) AddRootNode(_ string, _ trie.Node) {} + +// GetRootNode always returns nil. +func (*dummyCache) GetRootNode(_ string, _ trie.Version) trie.Node { + return nil +} diff --git a/muxdb/empty_cache.go b/muxdb/empty_cache.go deleted file mode 100644 index 09678e1ee..000000000 --- a/muxdb/empty_cache.go +++ /dev/null @@ -1,22 +0,0 @@ -package muxdb - -import "github.com/vechain/thor/v2/trie" - -type emptyCache struct{} - -// AddNodeBlob does nothing. -func (e *emptyCache) AddNodeBlob(_ *[]byte, _ string, _ []byte, _ trie.Version, _ []byte, _ bool) { -} - -// GetNodeBlob always returns nil. -func (e *emptyCache) GetNodeBlob(_ *[]byte, _ string, _ []byte, _ trie.Version, _ bool) []byte { - return nil -} - -// AddRootNode does nothing. -func (e *emptyCache) AddRootNode(_ string, _ trie.Node) {} - -// GetRootNode always returns nil. -func (e *emptyCache) GetRootNode(_ string, _ trie.Version) trie.Node { - return nil -} diff --git a/muxdb/muxdb.go b/muxdb/muxdb.go index ce83675cd..a0f00ae2c 100644 --- a/muxdb/muxdb.go +++ b/muxdb/muxdb.go @@ -127,7 +127,7 @@ func NewMem() *MuxDB { engine: engine, trieBackend: &backend{ Store: engine, - Cache: &emptyCache{}, + Cache: &dummyCache{}, HistPtnFactor: 1, DedupedPtnFactor: 1, CachedNodeTTL: 32, diff --git a/muxdb/trie_test.go b/muxdb/trie_test.go index 19b66a9b2..79bec7124 100644 --- a/muxdb/trie_test.go +++ b/muxdb/trie_test.go @@ -26,7 +26,7 @@ func newTestBackend() *backend { engine := newTestEngine() return &backend{ Store: engine, - Cache: &emptyCache{}, + Cache: &dummyCache{}, HistPtnFactor: 1, DedupedPtnFactor: 1, CachedNodeTTL: 100,