Skip to content

Commit

Permalink
perf: Create Ante/Post handler chain once.
Browse files Browse the repository at this point in the history
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 #14164 but maintains the existing recursive behavior.
  • Loading branch information
lcwik committed May 9, 2023
1 parent 0da5e83 commit e45d2c5
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 12 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ Ref: https://keepachangelog.com/en/1.0.0/

### Improvements

* (types) [#16076](https://github.com/cosmos/cosmos-sdk/pull/16076) Optimize `ChainAnteDecorators`/`ChainPostDecorators` to instantiate the functions once instead of on every invocation of the returned `AnteHandler`/`PostHandler`.
* (server) [#16071](https://github.com/cosmos/cosmos-sdk/pull/16071) When `mempool.max-txs` is set to a negative value, use a no-op mempool (effectively disable the app mempool).
* (simapp) [#15958](https://github.com/cosmos/cosmos-sdk/pull/15958) Refactor SimApp for removing the global basic manager.
* (gov) [#15979](https://github.com/cosmos/cosmos-sdk/pull/15979) Improve gov error message when failing to convert v1 proposal to v1beta1.
Expand Down
32 changes: 20 additions & 12 deletions types/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,18 @@ func ChainAnteDecorators(chain ...AnteDecorator) AnteHandler {
return nil
}

// handle non-terminated decorators chain
if (chain[len(chain)-1] != Terminator{}) {
chain = append(chain, Terminator{})
handlerChain := make([]AnteHandler, len(chain)+1)
// Install the terminal AnteHandler.
handlerChain[len(chain)] = func(ctx Context, tx Tx, simulate bool) (Context, error) {
return ctx, nil
}

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
handlerChain[ii] = func(ctx Context, tx Tx, simulate bool) (Context, error) {
return chain[ii].AnteHandle(ctx, tx, simulate, handlerChain[ii+1])
}
}
return handlerChain[0]
}

// ChainPostDecorators chains PostDecorators together with each PostDecorator
Expand All @@ -63,14 +67,18 @@ func ChainPostDecorators(chain ...PostDecorator) PostHandler {
return nil
}

// handle non-terminated decorators chain
if (chain[len(chain)-1] != Terminator{}) {
chain = append(chain, Terminator{})
handlerChain := make([]PostHandler, len(chain)+1)
// Install the terminal PostHandler.
handlerChain[len(chain)] = func(ctx Context, tx Tx, simulate, success bool) (Context, error) {
return ctx, nil
}

return func(ctx Context, tx Tx, simulate, success bool) (Context, error) {
return chain[0].PostHandle(ctx, tx, simulate, success, ChainPostDecorators(chain[1:]...))
for i := 0; i < len(chain); i++ {
ii := i
handlerChain[ii] = func(ctx Context, tx Tx, simulate, success bool) (Context, error) {
return chain[ii].PostHandle(ctx, tx, simulate, success, handlerChain[ii+1])
}
}
return handlerChain[0]
}

// Terminator AnteDecorator will get added to the chain to simplify decorator code
Expand Down

0 comments on commit e45d2c5

Please sign in to comment.