Skip to content

Commit

Permalink
Reduce megamorphic call sites in AbstractCallOperation (hyperledger#4288
Browse files Browse the repository at this point in the history
)

The four instances of gas in the CALL operations are all implemented
identically.  Refactor the method into the abstract parent.

Signed-off-by: Danno Ferrin <danno.ferrin@gmail.com>
  • Loading branch information
shemnon authored and eum602 committed Nov 3, 2023
1 parent 862eb51 commit f93c1a5
Show file tree
Hide file tree
Showing 5 changed files with 5 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
*/
package org.hyperledger.besu.evm.operation;

import static org.hyperledger.besu.evm.internal.Words.clampedToLong;

import org.hyperledger.besu.datatypes.Address;
import org.hyperledger.besu.datatypes.Wei;
import org.hyperledger.besu.evm.Code;
Expand Down Expand Up @@ -57,7 +59,9 @@ protected AbstractCallOperation(
* @param frame The current message frame
* @return the additional gas to provide the call operation
*/
protected abstract long gas(MessageFrame frame);
protected long gas(final MessageFrame frame) {
return clampedToLong(frame.getStackItem(0));
}

/**
* Returns the account the call is being made to.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,6 @@ public CallCodeOperation(final GasCalculator gasCalculator) {
super(0xF2, "CALLCODE", 7, 1, 1, gasCalculator);
}

@Override
protected long gas(final MessageFrame frame) {
try {
return frame.getStackItem(0).trimLeadingZeros().toLong();
} catch (final ArithmeticException | IllegalArgumentException ae) {
return Long.MAX_VALUE;
}
}

@Override
protected Address to(final MessageFrame frame) {
return Words.toAddress(frame.getStackItem(1));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,6 @@ public CallOperation(final GasCalculator gasCalculator) {
super(0xF1, "CALL", 7, 1, 1, gasCalculator);
}

@Override
protected long gas(final MessageFrame frame) {
try {
return frame.getStackItem(0).trimLeadingZeros().toLong();
} catch (final ArithmeticException | IllegalArgumentException ae) {
return Long.MAX_VALUE;
}
}

@Override
protected Address to(final MessageFrame frame) {
return Words.toAddress(frame.getStackItem(1));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,6 @@ public DelegateCallOperation(final GasCalculator gasCalculator) {
super(0xF4, "DELEGATECALL", 6, 1, 1, gasCalculator);
}

@Override
protected long gas(final MessageFrame frame) {
try {
return frame.getStackItem(0).trimLeadingZeros().toLong();
} catch (final ArithmeticException | IllegalArgumentException ae) {
return Long.MAX_VALUE;
}
}

@Override
protected Address to(final MessageFrame frame) {
return Words.toAddress(frame.getStackItem(1));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,6 @@ public StaticCallOperation(final GasCalculator gasCalculator) {
super(0xFA, "STATICCALL", 6, 1, 1, gasCalculator);
}

@Override
protected long gas(final MessageFrame frame) {
try {
return frame.getStackItem(0).trimLeadingZeros().toLong();
} catch (final ArithmeticException | IllegalArgumentException ae) {
return Long.MAX_VALUE;
}
}

@Override
protected Address to(final MessageFrame frame) {
return Words.toAddress(frame.getStackItem(1));
Expand Down

0 comments on commit f93c1a5

Please sign in to comment.