-
Notifications
You must be signed in to change notification settings - Fork 4.2k
perf(context): avoid unnecessary copies in KVStore, TransientStore and CacheContext methods #14354
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
Merged
Merged
Changes from 13 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
08d67a0
feat: Context.KVStore uses local ms field instead of calling MultiStore
6046a50
feat: sdk.Context KVStore and TransientStore to use pointer receiver
68a1523
feat: sdk.Context KVStore and TransientStore to gasMeter field
940980d
feat: gaskv config as pointer
8a9565f
chore: code cleanup
5b25cd0
Merge branch 'main' into tip/improve-context-KVStore
0b5e022
revert: behavioural changes
f1fd7fb
revert: docs
0ae513f
revert: tests
cd7c27f
add: transient tests
d2ffbb4
revert: oneline instantiation
6f09435
add: cache context bench
2052646
chore: lint
869e3d5
Merge branch 'main' into tip/improve-context-KVStore
alexanderbez 6f89bcd
Update CHANGELOG.md
testinginprod File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -278,20 +278,20 @@ func (c Context) Value(key interface{}) interface{} { | |
|
|
||
| // KVStore fetches a KVStore from the MultiStore. | ||
| func (c Context) KVStore(key storetypes.StoreKey) KVStore { | ||
| return gaskv.NewStore(c.MultiStore().GetKVStore(key), c.GasMeter(), c.kvGasConfig) | ||
| return gaskv.NewStore(c.ms.GetKVStore(key), c.gasMeter, c.kvGasConfig) | ||
| } | ||
|
|
||
| // TransientStore fetches a TransientStore from the MultiStore. | ||
| func (c Context) TransientStore(key storetypes.StoreKey) KVStore { | ||
| return gaskv.NewStore(c.MultiStore().GetKVStore(key), c.GasMeter(), c.transientKVGasConfig) | ||
| return gaskv.NewStore(c.ms.GetKVStore(key), c.gasMeter, c.transientKVGasConfig) | ||
| } | ||
|
|
||
| // CacheContext returns a new Context with the multi-store cached and a new | ||
| // EventManager. The cached context is written to the context when writeCache | ||
| // is called. Note, events are automatically emitted on the parent context's | ||
| // EventManager when the caller executes the write. | ||
| func (c Context) CacheContext() (cc Context, writeCache func()) { | ||
| cms := c.MultiStore().CacheMultiStore() | ||
| cms := c.ms.CacheMultiStore() | ||
| cc = c.WithMultiStore(cms).WithEventManager(NewEventManager()) | ||
|
|
||
| writeCache = func() { | ||
|
Comment on lines
290
to
297
Contributor
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. Change potentially affects state. Call sequence: |
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| package types_test | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/cosmos/cosmos-sdk/store/types" | ||
| "github.com/cosmos/cosmos-sdk/testutil" | ||
| ) | ||
|
|
||
| func BenchmarkContext_KVStore(b *testing.B) { | ||
| key := types.NewKVStoreKey(b.Name() + "_TestCacheContext") | ||
|
|
||
| ctx := testutil.DefaultContext(key, types.NewTransientStoreKey("transient_"+b.Name())) | ||
|
|
||
| b.ResetTimer() | ||
| for i := 0; i < b.N; i++ { | ||
| _ = ctx.KVStore(key) | ||
| } | ||
| } | ||
|
|
||
| func BenchmarkContext_TransientStore(b *testing.B) { | ||
| key := types.NewKVStoreKey(b.Name() + "_TestCacheContext") | ||
|
|
||
| ctx := testutil.DefaultContext(key, types.NewTransientStoreKey("transient_"+b.Name())) | ||
|
|
||
| b.ResetTimer() | ||
| for i := 0; i < b.N; i++ { | ||
| _ = ctx.TransientStore(key) | ||
| } | ||
| } | ||
|
|
||
| func BenchmarkContext_CacheContext(b *testing.B) { | ||
| key := types.NewKVStoreKey(b.Name() + "_TestCacheContext") | ||
|
|
||
| ctx := testutil.DefaultContext(key, types.NewTransientStoreKey("transient_"+b.Name())) | ||
|
|
||
| b.ResetTimer() | ||
| for i := 0; i < b.N; i++ { | ||
| _, _ = ctx.CacheContext() | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.