-
Notifications
You must be signed in to change notification settings - Fork 3.7k
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
|
@@ -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 | ||||
} | ||||
|
||||
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 | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why are you setting an internal variable?
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The loop variable There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ahh yes, I missed the func closure. Indeed that is correct. |
||||
handlerChain[ii] = func(ctx Context, tx Tx, simulate bool) (Context, error) { | ||||
return chain[ii].AnteHandle(ctx, tx, simulate, handlerChain[ii+1]) | ||||
} | ||||
} | ||||
|
||||
return handlerChain[0] | ||||
lcwik marked this conversation as resolved.
Show resolved
Hide resolved
|
||||
} | ||||
|
||||
// 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 | ||||
|
There was a problem hiding this comment.
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.