EIP-7877 · Stagnant · Core · 2025-01-31
EIP-7877: Enhanced RETURN opcodes
Proposes SRETURN, TRETURN and RRETURN opcodes that return data straight from storage, transient storage or return data, skipping the copy into memory that RETURN requires.
What problem does it solve?
RETURN can only return bytes from memory. If the data you want to return sits elsewhere, you first copy it into memory and then return it:
- With EIP-1153 transient storage, many callbacks stash data in
TSTOREand later return it, paying forTLOAD+MSTOREloops and memory expansion. - Returning values held in persistent storage has the same overhead with
SLOAD. - Forwarding (part of) another call's return data needs
RETURNDATACOPYinto memory first.
EIP-7877 lets a contract name the source directly, which saves gas and makes the bytecode simpler to analyze.
How it works
Three new opcodes, and RETURN gets an alias:
| Opcode | Byte | Returns |
|---|---|---|
MRETURN |
0xf3 |
Memory (just a new name for RETURN, unchanged) |
SRETURN |
0xf6 |
Consecutive storage slots |
TRETURN |
0xf7 |
Consecutive transient-storage slots |
RRETURN |
0xf8 |
A slice of the current return-data buffer |
SRETURN/TRETURNpopslotandlength. They readslot,slot+1, ..., write each as its 32-byte big-endian word (exactly asMSTOREwould), concatenate them and return the firstlengthbytes. A partial final slot contributes only its leading bytes; unset slots read as zeros.RRETURNpopsoffsetandlengthand returnsRETURNDATA[offset : offset+length]. Out-of-bounds reads fail, with the same rules asRETURNDATACOPY(EIP-211).
Proposed gas: SRETURN charges 2,100 per cold slot and 100 per warm slot; TRETURN 100 per slot; RRETURN like RETURNDATACOPY minus memory expansion (3 + 3 per word). The renaming has precedent in EIP-6 (SUICIDE to SELFDESTRUCT), and the gas-saving motive mirrors EIP-3855 PUSH0.
Sample code
Returning two words from transient storage, today vs. proposed (assembly pseudocode):
// Today: copy to memory, then RETURN
assembly {
mstore(0x00, tload(0))
mstore(0x20, tload(1))
return(0x00, 0x40)
}
// With EIP-7877 (not valid Solidity today; illustrative)
assembly {
treturn(0, 0x40) // slots 0 and 1, 64 bytes, no memory touched
}
Opcode-level view:
6040 PUSH1 0x40 // length = 64 bytes
5f PUSH0 // slot = 0
f7 TRETURN
Things to know
- Status: Stagnant. No upgrade has scheduled it, and the gas formulas are written as a rough proposal ("should be similar to the cost of accessing data now").
- The spec suggests activating it in a future EVM version once EOF is implemented; EOF itself has not shipped on mainnet.
- Existing contracts are unaffected:
MRETURNis byte-for-byte the oldRETURN. - Compilers would need to adopt the opcodes before typical Solidity code benefits.