perf: Create Ante/Post handler chain once. (#16076)

Co-authored-by: Aleksandr Bezobchuk <alexanderbez@users.noreply.github.com>
This commit is contained in:
Lukasz Cwik
2023-05-17 18:10:20 +00:00
committed by GitHub
co-authored by Aleksandr Bezobchuk
parent 9329a00e93
commit 46119d1384
2 changed files with 22 additions and 12 deletions
+21 -12
View File
@@ -40,14 +40,19 @@ 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)
// set the terminal AnteHandler decorator
handlerChain[len(chain)] = func(ctx Context, tx Tx, simulate bool) (Context, error) {
return ctx, nil
}
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 func(ctx Context, tx Tx, simulate bool) (Context, error) {
return chain[0].AnteHandle(ctx, tx, simulate, ChainAnteDecorators(chain[1:]...))
}
return handlerChain[0]
}
// ChainPostDecorators chains PostDecorators together with each PostDecorator
@@ -63,14 +68,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)
// set the terminal PostHandler decorator
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