From e45d2c5fd895b0cf44c5a7a7ae304378a3383656 Mon Sep 17 00:00:00 2001 From: Lukasz Cwik Date: Tue, 9 May 2023 10:56:08 -0700 Subject: [PATCH 1/4] perf: Create Ante/Post handler chain once. 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. --- CHANGELOG.md | 1 + types/handler.go | 32 ++++++++++++++++++++------------ 2 files changed, 21 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 435110ee3247..e022d05c85f2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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. diff --git a/types/handler.go b/types/handler.go index 1a86c1660551..49f8ac1e9240 100644 --- a/types/handler.go +++ b/types/handler.go @@ -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 @@ -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 From 9ac93f74c67674631beb4636744df9d7f67ed74e Mon Sep 17 00:00:00 2001 From: Lukasz Cwik <126621805+lcwik@users.noreply.github.com> Date: Wed, 17 May 2023 09:38:29 -0700 Subject: [PATCH 2/4] Update types/handler.go Co-authored-by: Aleksandr Bezobchuk --- types/handler.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/types/handler.go b/types/handler.go index 49f8ac1e9240..2796d215e692 100644 --- a/types/handler.go +++ b/types/handler.go @@ -41,7 +41,7 @@ func ChainAnteDecorators(chain ...AnteDecorator) AnteHandler { } handlerChain := make([]AnteHandler, len(chain)+1) - // Install the terminal AnteHandler. + // set the terminal AnteHandler decorator handlerChain[len(chain)] = func(ctx Context, tx Tx, simulate bool) (Context, error) { return ctx, nil } From 0c8ad30e42af8807a3a9ffc96b12e7cf9ad0db3e Mon Sep 17 00:00:00 2001 From: Lukasz Cwik <126621805+lcwik@users.noreply.github.com> Date: Wed, 17 May 2023 09:41:29 -0700 Subject: [PATCH 3/4] Update types/handler.go Co-authored-by: Aleksandr Bezobchuk --- types/handler.go | 1 + 1 file changed, 1 insertion(+) diff --git a/types/handler.go b/types/handler.go index 2796d215e692..a9ee3c622863 100644 --- a/types/handler.go +++ b/types/handler.go @@ -51,6 +51,7 @@ func ChainAnteDecorators(chain ...AnteDecorator) AnteHandler { return chain[ii].AnteHandle(ctx, tx, simulate, handlerChain[ii+1]) } } + return handlerChain[0] } From d35eb681afd9156a4dcb6373ade2ce3d881a85de Mon Sep 17 00:00:00 2001 From: Lukasz Cwik Date: Wed, 17 May 2023 09:44:40 -0700 Subject: [PATCH 4/4] Address PR comments. --- types/handler.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/types/handler.go b/types/handler.go index a9ee3c622863..7b57dd52f599 100644 --- a/types/handler.go +++ b/types/handler.go @@ -41,7 +41,7 @@ func ChainAnteDecorators(chain ...AnteDecorator) AnteHandler { } handlerChain := make([]AnteHandler, len(chain)+1) - // set the terminal AnteHandler decorator + // set the terminal AnteHandler decorator handlerChain[len(chain)] = func(ctx Context, tx Tx, simulate bool) (Context, error) { return ctx, nil } @@ -69,7 +69,7 @@ func ChainPostDecorators(chain ...PostDecorator) PostHandler { } handlerChain := make([]PostHandler, len(chain)+1) - // Install the terminal PostHandler. + // set the terminal PostHandler decorator handlerChain[len(chain)] = func(ctx Context, tx Tx, simulate, success bool) (Context, error) { return ctx, nil }