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: Create Ante/Post handler chain once. #16076

Merged
merged 5 commits into from May 17, 2023
Merged

Conversation

lcwik
Copy link
Contributor

@lcwik lcwik commented May 9, 2023

ChainAnteDecorators and ChainPostDecorators would create the chaining handler on each invocation repeatedly. This also had the code checking whether the terminator was at the end of the chain.

Creating all the handlers upfront and only once improved the performance of make test-sim-profile on my local machine from 133.2s to 132s for a ~1% performance improvement.

This is related to #14164 but maintains the existing recursive behavior.

Description


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)

@lcwik lcwik changed the title fix: Create Ante/Post handler chain once. perf: Create Ante/Post handler chain once. May 9, 2023
ChainAnteDecorators and ChainPostDecorators would create the chaining handler on
each invocation repeatedly. This also had the code checking whether the terminator
was at the end of the chain.

Creating all the handlers upfront and only once improved the performance of `make test-sim-profile`
on my local machine from 133.2s to 132s for a ~1% performance improvement.

This is similar to cosmos#14164 but maintains the existing recursive behavior.
@@ -40,14 +40,18 @@ func ChainAnteDecorators(chain ...AnteDecorator) AnteHandler {
return nil
}

// handle non-terminated decorators chain
if (chain[len(chain)-1] != Terminator{}) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Even though we don't use Terminator anymore, I didn't remove it to not break API compatibility with existing users.

Also I choose to use an AnteHandler implementation instead of an AnteDecorator to reduce the call depth by one for invocations that make it to the terminator.

@lcwik lcwik marked this pull request as ready for review May 9, 2023 18:00
@lcwik lcwik requested a review from a team as a code owner May 9, 2023 18:00
@github-prbot github-prbot requested review from a team, kocubinski and tac0turtle and removed request for a team May 9, 2023 18:00
@lcwik
Copy link
Contributor Author

lcwik commented May 9, 2023

Waiting on reviewer to allow for the CI workflows to execute.

Copy link
Contributor

@alexanderbez alexanderbez left a comment

Choose a reason for hiding this comment

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

Hey, thanks for the contribution @lcwik! Personally, I don't think the changes proposed, which harm legibility IMO, are worth the 1% gain.

@lcwik
Copy link
Contributor Author

lcwik commented May 9, 2023

The gain is 1% for the whole simulation which means that this method represents an amount of overhead that is much higher than what it is effectively meant to do. I would agree with you if the 1% improvements was for just the method.

@BrendanChou
Copy link

@alexanderbez Hi Bez, @lcwik is pushing upstream the optimizations that we have in our own fork (dYdX's fork of Cosmos-SDK). This is a good-faith effort to:

  • improve the SDK for others without making breaking changes
  • remove our need to maintain a fork of the SDK (I know it's a general goal of the community to reduce the number of forks maintained of CometBFT and CosmosSDK)

Given the above, I would encourage you to reconsider the acceptance of this PR

types/handler.go Outdated Show resolved Hide resolved
return func(ctx Context, tx Tx, simulate bool) (Context, error) {
return chain[0].AnteHandle(ctx, tx, simulate, ChainAnteDecorators(chain[1:]...))
for i := 0; i < len(chain); i++ {
ii := i
Copy link
Contributor

Choose a reason for hiding this comment

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

why are you setting an internal variable?

Suggested change
ii := i

Copy link
Contributor Author

@lcwik lcwik May 17, 2023

Choose a reason for hiding this comment

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

The loop variable i would be captured by reference by the closure which is why we need use the local ii so that each iteration of the loop captures the current value and not the value the loop ends at. See https://oyvindsk.com/writing/common-golang-mistakes-1 for more details.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ahh yes, I missed the func closure. Indeed that is correct.

types/handler.go Show resolved Hide resolved
types/handler.go Outdated
if (chain[len(chain)-1] != Terminator{}) {
chain = append(chain, Terminator{})
handlerChain := make([]PostHandler, len(chain)+1)
// Install the terminal PostHandler.
Copy link
Contributor

Choose a reason for hiding this comment

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

ditto on same changes as above :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

lcwik and others added 4 commits May 17, 2023 09:38
Co-authored-by: Aleksandr Bezobchuk <alexanderbez@users.noreply.github.com>
Co-authored-by: Aleksandr Bezobchuk <alexanderbez@users.noreply.github.com>
@lcwik
Copy link
Contributor Author

lcwik commented May 17, 2023

@alexanderbez PTAL

return func(ctx Context, tx Tx, simulate bool) (Context, error) {
return chain[0].AnteHandle(ctx, tx, simulate, ChainAnteDecorators(chain[1:]...))
for i := 0; i < len(chain); i++ {
ii := i
Copy link
Contributor

Choose a reason for hiding this comment

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

Ahh yes, I missed the func closure. Indeed that is correct.

@alexanderbez alexanderbez added the backport/v0.47.x PR scheduled for inclusion in the v0.47's next stable release label May 17, 2023
@alexanderbez
Copy link
Contributor

We'll have this backported on 0.47 as well.

@alexanderbez alexanderbez added this pull request to the merge queue May 17, 2023
Merged via the queue into cosmos:main with commit 46119d1 May 17, 2023
44 of 46 checks passed
mergify bot pushed a commit that referenced this pull request May 17, 2023
Co-authored-by: Aleksandr Bezobchuk <alexanderbez@users.noreply.github.com>
(cherry picked from commit 46119d1)

# Conflicts:
#	CHANGELOG.md
@lcwik
Copy link
Contributor Author

lcwik commented May 17, 2023

Thanks a bunch.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport/v0.47.x PR scheduled for inclusion in the v0.47's next stable release
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants