Skip to content

Commit

Permalink
Faster MCOPY implementation (#5630)
Browse files Browse the repository at this point in the history
Faster MCOPY implementation

wire in deep call to System.arrayCopy instead of buffer copy.

Signed-off-by: Danno Ferrin <danno.ferrin@swirldslabs.com>
  • Loading branch information
shemnon authored Jun 22, 2023
1 parent ac449dd commit a0e5dd6
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 6 deletions.
14 changes: 14 additions & 0 deletions evm/src/main/java/org/hyperledger/besu/evm/frame/Memory.java
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,20 @@ public void setWord(final long location, final Bytes32 bytes) {
System.arraycopy(bytes.toArrayUnsafe(), 0, memBytes, start, Bytes32.SIZE);
}

/**
* Copies one length of bytes to a new memory location, growing memory if needed.
*
* <p>Copying behaves as if the values are copied to an intermediate buffer before writing.
*
* @param dst where to copy the bytes _to_
* @param src where to copy the bytes _from_
* @param length the number of bytes to copy.
*/
public void copy(final long dst, final long src, final long length) {
ensureCapacityForBytes(Math.max(dst, src), length);
System.arraycopy(memBytes, asByteIndex(src), memBytes, asByteIndex(dst), asByteLength(length));
}

@Override
public String toString() {
return Bytes.wrap(memBytes).toHexString();
Expand Down
20 changes: 20 additions & 0 deletions evm/src/main/java/org/hyperledger/besu/evm/frame/MessageFrame.java
Original file line number Diff line number Diff line change
Expand Up @@ -817,6 +817,26 @@ public void writeMemory(
}
}

/**
* Copies bytes within memory.
*
* <p>Copying behaves as if the values are copied to an intermediate buffer before writing.
*
* @param dst The destination address
* @param src The source address
* @param length the number of bytes to copy
* @param explicitMemoryUpdate true if triggered by a memory opcode, false otherwise
*/
public void copyMemory(
final long dst, final long src, final long length, final boolean explicitMemoryUpdate) {
if (length > 0) {
memory.copy(dst, src, length);
if (explicitMemoryUpdate) {
setUpdatedMemory(dst, memory.getBytes(dst, length));
}
}
}

private void setUpdatedMemory(
final long offset, final long sourceOffset, final long length, final Bytes value) {
final long endIndex = sourceOffset + length;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@
import org.hyperledger.besu.evm.frame.MessageFrame;
import org.hyperledger.besu.evm.gascalculator.GasCalculator;

import org.apache.tuweni.bytes.Bytes;

/** The Memory copy operation. */
public class MCopyOperation extends AbstractOperation {

Expand All @@ -46,10 +44,7 @@ public OperationResult execute(final MessageFrame frame, final EVM evm) {
return new OperationResult(cost, ExceptionalHaltReason.INSUFFICIENT_GAS);
}

// read memory copies into a new buffer, no need to copy here.
final Bytes callData = frame.readMemory(src, length);

frame.writeMemory(dst, length, callData, true);
frame.copyMemory(dst, src, length, true);

return new OperationResult(cost, null);
}
Expand Down

0 comments on commit a0e5dd6

Please sign in to comment.