-
Notifications
You must be signed in to change notification settings - Fork 3.8k
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(sdk.Context): Context.KVStore/TransientStore improve performance #14203
Changes from 4 commits
08d67a0
6046a50
68a1523
940980d
8a9565f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package types_test | ||
|
||
import ( | ||
"github.com/cosmos/cosmos-sdk/store/types" | ||
"github.com/cosmos/cosmos-sdk/testutil" | ||
"testing" | ||
) | ||
|
||
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) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -86,7 +86,8 @@ func NewGasCountingMockContext() *GasCountingMockContext { | |
} | ||
|
||
func (g GasCountingMockContext) KVStore(store sdk.KVStore) sdk.KVStore { | ||
return gaskv.NewStore(store, g.GasMeter, storetypes.KVGasConfig()) | ||
gasConfig := storetypes.KVGasConfig() | ||
return gaskv.NewStore(store, g.GasMeter, gasConfig) | ||
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. Why does this offer any performance improvement? 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. the perf improvement is this: https://github.com/cosmos/cosmos-sdk/pull/14203/files#diff-abad3e7f81deb0a089f7e974e60a081e67a7b21e5c6ba5c7b2be3785a66445afR15 this allows us to save time because of alloc + copy, gasKV config is static for a specific context, meaning that as the context is running the gas configuration (how much we spend on each Read/Write/Iter/etc) does not change, so this is why the copy is unnecessary. To further support this point, the only way to modify the GasConfig is by calling My benches showed it saved a further 10ns 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.
Cool, where can we see the benchmark results that demonstrate the perf increases? It would be super cool if changing the representation of this ~50 byte struct from a value to a pointer managed to produce performance benefits from the reduced copy costs that outweighed the additional costs it incurs on the allocator and GC. |
||
} | ||
|
||
func (g GasCountingMockContext) GasConsumed() storetypes.Gas { | ||
|
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.
Callers who invoke c.KVStore will get a KVStore value with a kvGasConfig that is no longer a copy of the config data, but instead is a pointer to the same storetypes.GasConfig value of the Context from which the KVStore was derived. This is a fundamental change to the semantics of the KVStore method, and of the Context API in general. It's important to establish the safety of large changes like this with comprehensive test coverage.
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.
Ref: #14203 (comment)
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.
+1 if we need testing for this but old behaviour is retained