EIP-5 · Final · Core · 2015-11-22
EIP-5: Gas Usage for `RETURN` and `CALL*`
Proposed charging CALL* for output memory only once the callee has actually written it, so contracts could cheaply receive strings and other dynamically sized return data.
What problem does it solve?
When one contract calls another with CALL, CALLCODE or DELEGATECALL, the caller names an output area in its own memory: a start offset and a size. The return data is copied there. In the early EVM, the caller paid the memory expansion gas for that whole area up front, whether or not the callee wrote anything to it.
The caller usually does not know how large the answer will be. A string or a dynamic array might live in the other contract's storage. If you reserve a small area, you lose data. If you reserve a large one, you pay for memory you never use. This made returning dynamic data costly and inflexible, and it made generic "proxy" contracts that forward any call's output very hard to write.
How it works
EIP-5 changes only the gas and memory rules around CALL*. Opcode signatures stay the same:
- At the start of the call, memory expansion is charged only for the input area (
input_start + input_size), not the output area. - When the callee returns
nbytes, memory grows tooutput_start + min(output_size, n). The caller pays for that growth at this point and the data is written to[output_start, output_start + min(n, output_size)). - The call can therefore run out of gas at the start or at the end of the opcode.
- After the call,
MSIZEreflects the size memory actually grew to.
That last point gives a trick for learning the return size. Set output_start = MSIZE and output_size = 2**256 - 1. After the call, the bytes between the old and new MSIZE are exactly what was returned.
CREATE is unaffected because it writes nothing to the caller's memory.
Sample code
Pseudocode for the gas accounting, before and after:
# before EIP-5: pay for both areas up front
charge_memory(max(input_start + input_size, output_start + output_size))
result = run_callee(...)
write(output_start, result[:output_size])
# with EIP-5: pay for the input now, for the output only once it is known
charge_memory(input_start + input_size)
result = run_callee(...)
n = len(result)
charge_memory(output_start + min(output_size, n)) # may run out of gas here
write(output_start, result[:min(n, output_size)])
The return-size trick described in the spec, as EVM assembly:
MSIZE // output_start = current end of memory
PUSH32 0xff..ff // output_size = 2**256 - 1
... // input area, value, address, gas
CALL
MSIZE // new end of memory; difference = returned size
Things to know
- The spec says the VM never charges more gas than before. Contracts might see more gas left over and a smaller
MSIZEafter a call. The authors judged that unlikely to break real contracts. - The status is Final, but the EVM in use today still charges memory expansion for the full output area at the start of a call.
- In practice, dynamic return data was solved by EIP-211 (Byzantium). It added
RETURNDATASIZEandRETURNDATACOPY, so a caller can check the size first and then copy exactly that many bytes. Solidity and proxy contracts rely on those opcodes. - The
CALL*family here includesDELEGATECALL, which EIP-7 introduced.