-
Notifications
You must be signed in to change notification settings - Fork 379
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(x/params): add lru caching #3120
base: master
Are you sure you want to change the base?
Changes from all commits
6e80a1d
9a44ea0
677b875
a62c919
4ad3a8c
488fc44
33c9591
5745829
a73eead
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ | |
"github.com/gnolang/gno/tm2/pkg/amino" | ||
"github.com/gnolang/gno/tm2/pkg/sdk" | ||
"github.com/gnolang/gno/tm2/pkg/store" | ||
"github.com/golang/groupcache/lru" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe this lib is more maintained than the one used: https://github.com/hashicorp/golang-lru |
||
) | ||
|
||
const ( | ||
|
@@ -39,13 +40,15 @@ | |
type ParamsKeeper struct { | ||
key store.StoreKey | ||
prefix string | ||
cache *lru.Cache | ||
} | ||
|
||
// NewParamsKeeper returns a new ParamsKeeper. | ||
func NewParamsKeeper(key store.StoreKey, prefix string) ParamsKeeper { | ||
func NewParamsKeeper(key store.StoreKey, prefix string, maxCacheSize int) ParamsKeeper { | ||
return ParamsKeeper{ | ||
key: key, | ||
prefix: prefix, | ||
cache: lru.New(maxCacheSize), | ||
} | ||
} | ||
|
||
|
@@ -116,6 +119,14 @@ | |
} | ||
|
||
func (pk ParamsKeeper) getIfExists(ctx sdk.Context, key string, ptr interface{}) { | ||
if bz, ok := pk.cache.Get(key); ok { | ||
if bz == nil { | ||
return | ||
} | ||
Comment on lines
+123
to
+125
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what is the meaning of that code? can we have nil values in our cache? why? |
||
amino.UnmarshalJSON(bz.([]byte), ptr) | ||
return | ||
} | ||
|
||
stor := ctx.Store(pk.key) | ||
bz := stor.Get([]byte(key)) | ||
if bz == nil { | ||
|
@@ -125,15 +136,25 @@ | |
if err != nil { | ||
panic(err) | ||
} | ||
pk.cache.Add(key, bz) | ||
} | ||
|
||
func (pk ParamsKeeper) get(ctx sdk.Context, key string, ptr interface{}) { | ||
if bz, ok := pk.cache.Get(key); ok { | ||
err := amino.UnmarshalJSON(bz.([]byte), ptr) | ||
if err != nil { | ||
panic(err) | ||
} | ||
return | ||
} | ||
|
||
stor := ctx.Store(pk.key) | ||
bz := stor.Get([]byte(key)) | ||
err := amino.UnmarshalJSON(bz, ptr) | ||
if err != nil { | ||
panic(err) | ||
} | ||
pk.cache.Add(key, bz) | ||
} | ||
|
||
func (pk ParamsKeeper) set(ctx sdk.Context, key string, value interface{}) { | ||
|
@@ -143,6 +164,7 @@ | |
panic(err) | ||
} | ||
stor.Set([]byte(key), bz) | ||
pk.cache.Remove(key) | ||
} | ||
|
||
func checkSuffix(key, expectedSuffix string) { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is the int representing? Kb? Mb? The number of items?