EIP-5656 · Final · Core · 2021-02-01
EIP-5656: MCOPY - Memory copying instruction
Adds the MCOPY opcode (0x5E), a cheap native way to copy a block of EVM memory, replacing slow MLOAD/MSTORE loops and identity-precompile calls.
What problem does it solve?
Copying bytes inside memory is something compilers do constantly: building ABI-encoded data, slicing bytes, copying structs and arrays. Before this EIP the EVM had no instruction for it, so code had two options:
- Loop over 32-byte words with
MLOAD/MSTORE, paying for each word plus loop overhead, and do awkward masking for a trailing partial word. - Call the identity precompile (address
0x04) and let it copy input to output. That works but paysCALLoverhead, which became expensive after gas repricings.
The spec's own example: copying 256 bytes costs at least 96 gas with unrolled MLOAD/MSTORE and at least 157 gas through the identity precompile (post-EIP-2929), versus 27 gas with MCOPY.
How it works
A new instruction MCOPY at opcode 0x5E takes three stack items and returns none:
| Stack position | Value |
|---|---|
| top | dst – destination offset |
| top - 1 | src – source offset |
| top - 2 | length – number of bytes |
The argument order matches CALLDATACOPY and RETURNDATACOPY.
- It copies
lengthbytes fromsrctodstas if through an intermediate buffer, so overlapping regions are handled correctly (like C'smemmove, notmemcpy). - If
length > 0and either range reaches past the current memory size, memory is expanded and the usual expansion cost is charged. - Gas follows the other copy opcodes:
words = (length + 31) // 32
gas = 3 + 3 * words + memory_expansion_cost
So copying 32 bytes within already-allocated memory costs 6 gas.
Having a dedicated opcode also helps static analysers: unlike a CALL, MCOPY is known to touch only memory.
Sample code
Using mcopy from inline assembly (Solidity 0.8.24+ with the cancun EVM version or later):
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;
library MemCopy {
/// Returns a copy of `data[start : start + len]`.
function slice(bytes memory data, uint256 start, uint256 len)
internal pure returns (bytes memory out)
{
require(start + len <= data.length, "out of range");
out = new bytes(len);
assembly {
// skip the 32-byte length prefix on both arrays
mcopy(add(out, 32), add(add(data, 32), start), len)
}
}
}
Spec test case, overlapping copy MCOPY dst=0 src=1 len=8:
before: 0001020304050607 08...
after: 0102030405060708 08... (gas used: 6)
Things to know
- Status: Final.
MCOPYwent live on mainnet in the Cancun/Deneb ("Dencun") upgrade, alongside EIP-1153 transient storage, EIP-4844 blobs and EIP-6780. - Recent Solidity and Vyper compilers emit
MCOPYautomatically when targeting Cancun or later; if you deploy to a chain that has not adopted it, set an older EVM version or the code will hit an invalid opcode. - Client implementers must avoid allocating a real intermediate buffer, which could be a DoS vector; an in-place
memmove-style copy gives the required semantics.