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

Improve flat trace generation performance #6472

Merged
merged 17 commits into from
Feb 19, 2024

Conversation

ahamlat
Copy link
Contributor

@ahamlat ahamlat commented Jan 26, 2024

PR description

When testing one of the blocks that has a lot of subcalls (838 internal transactions), we noticed that most of the time was spent on hasRevertInSubCall method.

image

This PR reduced the time to trace a 29 mgas block from more than 5 seconds to 1 second.

Fixed Issue(s)

Copy link

github-actions bot commented Jan 26, 2024

  • I thought about documentation and added the doc-change-required label to this PR if updates are required.
  • I thought about the changelog and included a changelog update if required.
  • If my PR includes database changes (e.g. KeyValueSegmentIdentifier) I have thought about compatibility and performed forwards and backwards compatibility tests

@ahamlat ahamlat marked this pull request as ready for review January 30, 2024 15:03
shemnon
shemnon previously approved these changes Feb 13, 2024
Copy link
Contributor

@shemnon shemnon left a comment

Choose a reason for hiding this comment

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

OPCODE request is optional.

if (i + 1 < transactionTrace.getTraceFrames().size()) {
final TraceFrame next = transactionTrace.getTraceFrames().get(i + 1);
if (next.getDepth() == callFrame.getDepth()) {
if (next.getOpcodeNumber() == 0xFD) { // REVERT opCode
Copy link
Contributor

Choose a reason for hiding this comment

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

Consider adding an OPCODE constant to RevertOperation and ReturnOperation so we can reduce magic constants in the code.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the suggestion, I was thinking actually of an Enum OpCode to store all the opcodes and use only the Enum in the EVM and outside, like Opcode.REVERT.getOpcodeNumber() for 0xFD
public enum Opcode {
STOP(0x00, "STOP"),
ADD(0x01, "ADD"),
MUL(0x02, "MUL"),
...
}
In terms of performance, I guess JIT will replace by the opCodeNumber or the opcode name at runtime

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@shemnon I've added the OpCode enum within the EVM and would like to use it across all EVM opcode operations. An implementation example has been provided for ADDMOD. Does this approach work for you ?

Copy link
Contributor

@shemnon shemnon Feb 15, 2024

Choose a reason for hiding this comment

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

This is not what I meant. Such an OpCode enum breaks extensibility to downstream users use. Please revert.

Copy link
Contributor

Choose a reason for hiding this comment

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

What I intended was something like this, where a constant is added to the Operation classes, not an entirely different enum.

Copy link
Contributor Author

@ahamlat ahamlat Feb 15, 2024

Choose a reason for hiding this comment

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

I actually understood your suggestion but wanted to suggest an implementation that would work for similar use cases in the future, but it looks like it wasn't a good idea. I'm going to revert the last changes.

@shemnon shemnon dismissed their stale review February 15, 2024 18:37

Changes are now problematic.

@ahamlat
Copy link
Contributor Author

ahamlat commented Feb 16, 2024

@shemnon Could you review the last changes ?

if (i + 1 < transactionTrace.getTraceFrames().size()) {
final TraceFrame next = transactionTrace.getTraceFrames().get(i + 1);
if (next.getDepth() == callFrame.getDepth()) {
if (next.getOpcodeNumber() == 0xFD) { // REVERT opCode
Copy link
Contributor

Choose a reason for hiding this comment

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

Might be worth making AbstractOperation.getOpcode() static and doing the comparison to that to avoid creating another range of enums.

Copy link
Contributor Author

@ahamlat ahamlat Feb 16, 2024

Choose a reason for hiding this comment

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

This commit is outdated. Making AbstractOperation.getOpcode() static means that we need to make opcode static in AbstractOperation which is not the case, opcode depends on the implementation.
What suggested by Danno is very similar in some way to what you suggest, add a static constants directly in the implementation, that is stored in the opcode field.

@ahamlat ahamlat merged commit 8d25b24 into hyperledger:main Feb 19, 2024
48 of 55 checks passed
suraneti pushed a commit to suraneti/besu that referenced this pull request Feb 24, 2024
Improve flat trace generation performance

Signed-off-by: Ameziane H <[email protected]>
.filter(traceFrame -> traceFrame.getOpcode().equals("REVERT"))
.anyMatch(traceFrame -> traceFrame.getDepth() == callFrame.getDepth());
for (int i = 0; i < transactionTrace.getTraceFrames().size(); i++) {
if (i + 1 < transactionTrace.getTraceFrames().size()) {
Copy link

Choose a reason for hiding this comment

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

Kind of a random question (Side note: I dont really have Java exp) but what is the point of this If condition? Wouldn't starting the loop at 1 and running it until size() - 1 have the same result? (Or if iterating in reverse has no side-effects, even starting at size() - 1 and running while its > 0)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants