MZERO instruction

I would like to float the idea of an MZERO opcode here. It would take two arguments, ptr and len, and would set each byte in the memory between [ptr, ptr+len] to the value zero.

Zeroing out memory is a common task, mostly for overwriting previously potentially-used memory (i.e. during memory allocation). Solidity sometimes does tricks like calldatacopy(ptr, calldatasize(), len) to use the zero-padding feature of calldatacopy, codecopy or extcodecopy. See also this Solidity proposal for making that pattern more prevalent.

While this pattern works, it sounds like an abuse of those opcodes and we could do better, both in terms of clarity, cost, and aiding analysis/optimisation. The usual counter argument is that it “wastes” the opcode space and it is yet another instruction.

P.S. There’s an adjacent MCOPY proposal already.

1 Like