Skip to content
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

perf(context): avoid unnecessary copies in KVStore, TransientStore and CacheContext methods #14354

Merged
merged 15 commits into from
Dec 19, 2022

Conversation

testinginprod
Copy link
Contributor

@testinginprod testinginprod commented Dec 19, 2022

Description

Avoids unnecessary context copies on the KVStore, TransientStore and CacheContext methods, whilst retaining the copy semantics.

_@_-MBP types % benchstat before_cache_context.txt after_cache_context.txt 
name                     old time/op  new time/op  delta
Context_CacheContext-10  1.72µs ± 5%  1.69µs ± 6%  -1.71%  (p=0.001 n=17+18)


_@_-MBP types % benchstat before_kv_context.txt after_kv_context.txt   
name                old time/op  new time/op  delta
Context_KVStore-10   136ns ± 4%    81ns ± 3%  -40.46%  (p=0.000 n=18+18)


_@_-MBP types % benchstat before_transient_context.txt after_transient_context.txt
name                       old time/op  new time/op  delta
Context_TransientStore-10   135ns ± 2%    82ns ± 2%  -39.59%  (p=0.000 n=18+18)

Author Checklist

All items are required. Please add a note to the item if the item is not applicable and
please add links to any relevant follow up issues.

I have...

  • included the correct type prefix in the PR title
  • added ! to the type prefix if API or client breaking change
  • targeted the correct branch (see PR Targeting)
  • provided a link to the relevant issue or specification
  • followed the guidelines for building modules
  • included the necessary unit and integration tests
  • added a changelog entry to CHANGELOG.md
  • included comments for documenting Go code
  • updated the relevant documentation or specification
  • reviewed "Files changed" and left comments if necessary
  • confirmed all CI checks have passed

Reviewers Checklist

All items are required. Please add a note if the item is not applicable and please add
your handle next to the items reviewed if you only reviewed selected items.

I have...

  • confirmed the correct type prefix in the PR title
  • confirmed ! in the type prefix if API or client breaking change
  • confirmed all author checklist items have been addressed
  • reviewed state machine logic
  • reviewed API design and naming
  • reviewed documentation is accurate
  • reviewed tests and test coverage
  • manually tested (if applicable)

@testinginprod testinginprod requested a review from a team as a code owner December 19, 2022 09:37
Comment on lines 291 to 298
// 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() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change potentially affects state.

Call sequence:

(github.com/cosmos/cosmos-sdk/types.Context).CacheContext (types/context.go:293)
(*github.com/cosmos/cosmos-sdk/baseapp.BaseApp).getContextForTx (types/context.go:570)
(*github.com/cosmos/cosmos-sdk/baseapp.BaseApp).runTx (types/context.go:618)
(*github.com/cosmos/cosmos-sdk/baseapp.BaseApp).DeliverTx (types/context.go:326)

@testinginprod testinginprod marked this pull request as draft December 19, 2022 09:40
@testinginprod testinginprod changed the title perf(context): avoid copies in KVStore and TransientStore methods perf(context): avoid unnecessary copies in KVStore, TransientStore and CacheContext methods Dec 19, 2022
@testinginprod testinginprod marked this pull request as ready for review December 19, 2022 10:04
@sonarcloud
Copy link

sonarcloud bot commented Dec 19, 2022

[Cosmos SDK] Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

No Coverage information No Coverage information
No Duplication information No Duplication information

@alexanderbez alexanderbez requested review from angbrav and removed request for angbrav December 19, 2022 15:27
Copy link
Collaborator

@yihuang yihuang left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Copy link
Member

@tac0turtle tac0turtle left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't fully understand why this added so much overhead, but happy to see it reduced

@testinginprod testinginprod enabled auto-merge (squash) December 19, 2022 16:49
@testinginprod
Copy link
Contributor Author

testinginprod commented Dec 19, 2022

I don't fully understand why this added so much overhead, but happy to see it reduced

sdk.Context is fully implemented with value receives and is passed around like a value rather than being passed with pointers, sdk.Context is a huge bag of values whose size cannot be ignored I guess... I think it might be a problem considering how frequently we pass it around (antes, keepers, etc.), each time we're making a copy.

@testinginprod testinginprod merged commit c918b14 into main Dec 19, 2022
@testinginprod testinginprod deleted the tip/improve-context-KVStore branch December 19, 2022 18:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants