Mul speedup (#4756)

Move mul from UInt256 wrapping to direct BigInteger math.

Signed-off-by: Danno Ferrin <danno.ferrin@swirldslabs.com>
pull/4761/head
Danno Ferrin 2 years ago committed by GitHub
parent 7994265325
commit 0a6dcea436
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 5
      evm/src/main/java/org/hyperledger/besu/evm/EVM.java
  2. 5
      evm/src/main/java/org/hyperledger/besu/evm/frame/MessageFrame.java
  3. 18
      evm/src/main/java/org/hyperledger/besu/evm/operation/MulOperation.java

@ -292,8 +292,9 @@ public class EVM {
LOG.trace("MessageFrame evaluation halted because of {}", haltReason);
frame.setExceptionalHaltReason(Optional.of(haltReason));
frame.setState(State.EXCEPTIONAL_HALT);
} else {
frame.decrementRemainingGas(result.getGasCost());
} else if (frame.decrementRemainingGas(result.getGasCost()) < 0) {
frame.setExceptionalHaltReason(Optional.of(ExceptionalHaltReason.INSUFFICIENT_GAS));
frame.setState(State.EXCEPTIONAL_HALT);
}
if (frame.getState() == State.CODE_EXECUTING) {
final int currentPC = frame.getPC();

@ -350,9 +350,10 @@ public class MessageFrame {
* Decrement the amount of remaining gas.
*
* @param amount The amount of gas to deduct
* @return the amount of gas available, after deductions.
*/
public void decrementRemainingGas(final long amount) {
this.gasRemaining -= amount;
public long decrementRemainingGas(final long amount) {
return this.gasRemaining -= amount;
}
/**

@ -18,7 +18,9 @@ import org.hyperledger.besu.evm.EVM;
import org.hyperledger.besu.evm.frame.MessageFrame;
import org.hyperledger.besu.evm.gascalculator.GasCalculator;
import org.apache.tuweni.units.bigints.UInt256;
import java.math.BigInteger;
import org.apache.tuweni.bytes.Bytes;
public class MulOperation extends AbstractFixedCostOperation {
@ -35,14 +37,16 @@ public class MulOperation extends AbstractFixedCostOperation {
}
public static OperationResult staticOperation(final MessageFrame frame) {
final UInt256 value0 = UInt256.fromBytes(frame.popStackItem());
final UInt256 value1 = UInt256.fromBytes(frame.popStackItem());
final UInt256 result = value0.multiply(value1);
BigInteger a = new BigInteger(1, frame.popStackItem().toArrayUnsafe());
BigInteger b = new BigInteger(1, frame.popStackItem().toArrayUnsafe());
BigInteger c = a.multiply(b);
byte[] cBytes = c.toByteArray();
Bytes result = Bytes.wrap(cBytes);
if (cBytes.length > 32) {
result = result.slice(cBytes.length - 32, 32);
}
frame.pushStackItem(result);
return mulSuccess;
}
}

Loading…
Cancel
Save