Skip to content

Commit

Permalink
Merge pull request #518 from qianbin/apply-directcache
Browse files Browse the repository at this point in the history
muxdb/internal/trie: use directcache to cache encoded trie nodes
  • Loading branch information
qianbin authored May 7, 2022
2 parents 697a84d + 76cb52e commit 8bf871b
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 38 deletions.
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ require (
github.com/beevik/ntp v0.2.0
github.com/btcsuite/btcd v0.0.0-20171128150713-2e60448ffcc6 // indirect
github.com/cespare/cp v1.1.1 // indirect
github.com/coocood/freecache v1.2.0
github.com/davecgh/go-spew v1.1.0
github.com/deckarep/golang-set v1.7.1 // indirect
github.com/elastic/gosigar v0.10.5
Expand All @@ -31,8 +30,9 @@ require (
github.com/pborman/uuid v0.0.0-20170612153648-e790cca94e6c
github.com/pkg/errors v0.8.0
github.com/pmezard/go-difflib v1.0.0
github.com/qianbin/directcache v0.9.1
github.com/rjeczalik/notify v0.9.1 // indirect
github.com/stretchr/testify v1.1.4
github.com/stretchr/testify v1.4.0
github.com/syndtr/goleveldb v1.0.1-0.20200815110645-5c35d600f0ca
github.com/vechain/go-ecvrf v0.0.0-20200326080414-5b7e9ee61906
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9
Expand Down
18 changes: 8 additions & 10 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
github.com/OneOfOne/xxhash v1.2.2 h1:KMrpdQIwFcEqXDklaen+P1axHaj9BSKzvpUUfnHldSE=
github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU=
github.com/aristanetworks/goarista v0.0.0-20180222005525-c41ed3986faa h1:yCVE1EVBfyjHQn7TAfnD1Q4MMHGW/jdZjVJsXQeuRQw=
github.com/aristanetworks/goarista v0.0.0-20180222005525-c41ed3986faa/go.mod h1:D/tb0zPVXnP7fmsLZjtdUhSsumbK/ij54UXjjVgMGxQ=
github.com/beevik/ntp v0.2.0 h1:sGsd+kAXzT0bfVfzJfce04g+dSRfrs+tbQW8lweuYgw=
Expand All @@ -8,10 +6,8 @@ github.com/btcsuite/btcd v0.0.0-20171128150713-2e60448ffcc6 h1:Eey/GGQ/E5Xp1P2Ly
github.com/btcsuite/btcd v0.0.0-20171128150713-2e60448ffcc6/go.mod h1:Dmm/EzmjnCiweXmzRIAiUWCInVmPgjkzgv5k4tVyXiQ=
github.com/cespare/cp v1.1.1 h1:nCb6ZLdB7NRaqsm91JtQTAme2SKJzXVsdPIPkyJr1MU=
github.com/cespare/cp v1.1.1/go.mod h1:SOGHArjBr4JWaSDEVpWpo/hNg6RoKrls6Oh40hiwW+s=
github.com/cespare/xxhash v1.1.0 h1:a6HrQnmkObjyL+Gs60czilIUGqrzKutQD6XZog3p+ko=
github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc=
github.com/coocood/freecache v1.2.0 h1:p8RhjN6Y4DRBIMzdRlm1y+M7h7YJxye3lGW8/VvzCz0=
github.com/coocood/freecache v1.2.0/go.mod h1:OKrEjkGVoxZhyWAJoeFi5BMLUJm2Tit0kpGkIr7NGYY=
github.com/cespare/xxhash/v2 v2.1.2 h1:YRXhKfTDauu4ajMg1TPgFO5jnlC2HCbmLXMcTG5cbYE=
github.com/cespare/xxhash/v2 v2.1.2/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/deckarep/golang-set v1.7.1 h1:SCQV0S6gTtp6itiFrTqI+pfmJ4LN85S1YzhDf9rTHJQ=
Expand Down Expand Up @@ -87,12 +83,13 @@ github.com/pkg/errors v0.8.0 h1:WdK/asTD0HN+q6hsWO3/vpuAkAr+tw6aNJNDFFf0+qw=
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/qianbin/directcache v0.9.1 h1:U80py0I6KXcTWvA//6kfHOIXOzyyM1Y24GlkjNGysgc=
github.com/qianbin/directcache v0.9.1/go.mod h1:gZBpa9NqO1Qz7wZKO7t7atBA76bT8X0eM01PdveW4qc=
github.com/rjeczalik/notify v0.9.1 h1:CLCKso/QK1snAlnhNR/CNvNiFU2saUtjV0bx3EwNeCE=
github.com/rjeczalik/notify v0.9.1/go.mod h1:rKwnCoCGeuQnwBtTSPL9Dad03Vh2n40ePRrjvIXnJho=
github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72 h1:qLC7fQah7D6K1B0ujays3HV9gkFtllcxhzImRR7ArPQ=
github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA=
github.com/stretchr/testify v1.1.4 h1:ToftOQTytwshuOSj6bDSolVUa3GINfJP/fg3OkkOzQQ=
github.com/stretchr/testify v1.1.4/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/vechain/go-ecvrf v0.0.0-20200326080414-5b7e9ee61906 h1:llHjJ24ov25y8DtT+Aw5uJl7HCRWoJHRmhH7Y9/TYQI=
github.com/vechain/go-ecvrf v0.0.0-20200326080414-5b7e9ee61906/go.mod h1:HM7kygiu1D0CdotRa2u8z4I8AW9sRShPSjpLuIw0kyE=
github.com/vechain/goleveldb v1.0.1-0.20220107084823-eb292ce39601 h1:aVghsG74P5IPh0l2JDjg8WmshVJDrlNGJkBxH4mKPIE=
Expand Down Expand Up @@ -145,6 +142,7 @@ gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkep
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw=
gopkg.in/urfave/cli.v1 v1.20.0 h1:NdAVW6RYxDif9DhDHaAortIu956m2c0v+09AZBPTbE0=
gopkg.in/urfave/cli.v1 v1.20.0/go.mod h1:vuBzUtMdQeixQj8LVd+/98pzhxNGQoyuPBlsXHOQNO0=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU=
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
39 changes: 13 additions & 26 deletions muxdb/internal/trie/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,35 +11,30 @@ import (
"sync/atomic"
"time"

"github.com/coocood/freecache"
lru "github.com/hashicorp/golang-lru"
"github.com/qianbin/directcache"
"github.com/vechain/thor/trie"
)

// Cache is the cache layer for trie.
type Cache struct {
// caches recently queried node blobs. Using full node key as key.
queriedNodes *freecache.Cache
queriedNodes *directcache.Cache
// caches newly committed node blobs. Using node path as key.
committedNodes *freecache.Cache
committedNodes *directcache.Cache
// caches root nodes.
roots *lru.ARCCache
nodeStats cacheStats
rootStats cacheStats
lastLogTime int64
}

// implements freecache.Timer
type dummyTimer struct{}

func (dummyTimer) Now() uint32 { return 0 }

// NewCache creates a cache object with the given cache size.
func NewCache(sizeMB int, rootCap int) *Cache {
sizeBytes := sizeMB * 1024 * 1024
var cache Cache
cache.queriedNodes = freecache.NewCacheCustomTimer(sizeBytes/4, dummyTimer{})
cache.committedNodes = freecache.NewCacheCustomTimer(sizeBytes-sizeBytes/4, dummyTimer{})
cache.queriedNodes = directcache.New(sizeBytes / 4)
cache.committedNodes = directcache.New(sizeBytes - sizeBytes/4)
cache.roots, _ = lru.NewARC(rootCap)
cache.lastLogTime = time.Now().UnixNano()
return &cache
Expand Down Expand Up @@ -84,11 +79,11 @@ func (c *Cache) AddNodeBlob(name string, seq sequence, path []byte, blob []byte,
v.buf = appendUint32(v.buf[:0], cNum)
v.buf = append(v.buf, blob...)

_ = c.committedNodes.Set(k.buf, v.buf, 0)
_ = c.committedNodes.Set(k.buf, v.buf)
} else {
// querying cache key: name + path + distinctNum + commitNum
k.buf = appendUint32(k.buf, cNum)
_ = c.queriedNodes.Set(k.buf, blob, 0)
_ = c.queriedNodes.Set(k.buf, blob)
}
}

Expand All @@ -99,12 +94,8 @@ func (c *Cache) GetNodeBlob(name string, seq sequence, path []byte, peek bool, d
}

cNum, dNum := seq.CommitNum(), seq.DistinctNum()
lookupQueried := c.queriedNodes.GetFn
lookupCommitted := c.committedNodes.GetFn
if peek {
lookupQueried = c.queriedNodes.PeekFn
lookupCommitted = c.committedNodes.PeekFn
}
lookupQueried := c.queriedNodes.AdvGet
lookupCommitted := c.committedNodes.AdvGet

k := bufferPool.Get().(*buffer)
defer bufferPool.Put(k)
Expand All @@ -115,13 +106,11 @@ func (c *Cache) GetNodeBlob(name string, seq sequence, path []byte, peek bool, d

// lookup from committing cache
var blob []byte
lookupCommitted(k.buf, func(b []byte) error {
if lookupCommitted(k.buf, func(b []byte) {
if binary.BigEndian.Uint32(b) == cNum {
blob = append(dst, b[4:]...)
}
return nil
})
if len(blob) > 0 {
}, peek) && len(blob) > 0 {
if !peek {
c.nodeStats.Hit()
}
Expand All @@ -130,11 +119,9 @@ func (c *Cache) GetNodeBlob(name string, seq sequence, path []byte, peek bool, d

// fallback to querying cache
k.buf = appendUint32(k.buf, cNum)
lookupQueried(k.buf, func(b []byte) error {
if lookupQueried(k.buf, func(b []byte) {
blob = append(dst, b...)
return nil
})
if len(blob) > 0 {
}, peek); len(blob) > 0 {
if !peek {
c.nodeStats.Hit()
}
Expand Down

0 comments on commit 8bf871b

Please sign in to comment.