EIPs Explained

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 TSTORE and later return it, paying for TLOAD + MSTORE loops and memory expansion.
  • Returning values held in persistent storage has the same overhead with SLOAD.
  • Forwarding (part of) another call's return data needs RETURNDATACOPY into 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 / TRETURN pop slot and length. They read slot, slot+1, ..., write each as its 32-byte big-endian word (exactly as MSTORE would), concatenate them and return the first length bytes. A partial final slot contributes only its leading bytes; unset slots read as zeros.
  • RRETURN pops offset and length and returns RETURNDATA[offset : offset+length]. Out-of-bounds reads fail, with the same rules as RETURNDATACOPY (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: MRETURN is byte-for-byte the old RETURN.
  • Compilers would need to adopt the opcodes before typical Solidity code benefits.

#evm #opcode #gas #transient-storage #core

Details

Authors
Josh Weintraub (@jhweintraub)
Status
Stagnant
Type
Standards Track / Core
Created
2025-01-31
Requires

Machine-readable: Markdown · JSON