EIPs Explained

EIP-1380 · Stagnant · Core · 2018-08-31

EIP-1380: Reduced gas cost for call to self

Proposed cutting the gas for a CALL from a contract to itself from 700 to 40, so compilers could use cheap self-calls with clean memory instead of JUMP-based internal functions.

What problem does it solve?

Languages like Solidity and Vyper implement internal function calls with JUMP: the same EVM context is reused, and the compiler has to save and restore stack and memory around each call. That is complex, makes "pure" functions hard to guarantee (leftover memory can leak into the callee), and burns through the 1024-item stack limit quickly when calls are nested.

A CALL to the contract's own address would give each internal function a fresh memory and stack, for free in terms of compiler complexity. But at the time every call type cost 700 gas, set by EIP-150 mainly to pay for loading another contract's code from disk. A call to yourself needs no such load, since the code is already in memory.

How it works

After the fork block, CALL, DELEGATECALL, CALLCODE and STATICCALL would cost 40 gas instead of 700 if and only if the destination address equals the current contract's address.

The authors' reasoning: before EIP-150 these calls cost 40. EIP-150 added roughly 660 for disk I/O. Since a self-call does no new I/O, the original 40 is a fair price for starting a new VM instance of already-loaded code.

Everything else about the call is unchanged (value transfer costs, memory expansion, the 63/64 gas rule).

Sample code

What a compiler could emit for an internal function if self-calls were cheap, shown as Solidity:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;

contract SelfCall {
    // Today: internal call compiled to JUMP, shares memory with the caller.
    function _double(uint256 x) internal pure returns (uint256) { return x * 2; }

    // Style EIP-1380 aimed to make cheap: a fresh EVM frame via call-to-self.
    function doubleExt(uint256 x) external pure returns (uint256) { return x * 2; }

    function viaSelfCall(uint256 x) external view returns (uint256) {
        (bool ok, bytes memory out) =
            address(this).staticcall(abi.encodeCall(this.doubleExt, (x)));
        require(ok);
        return abi.decode(out, (uint256));
    }
}

Cost rule in pseudocode:

def call_base_cost(caller, target):
    if block.number >= FORK_BLKNUM and target == caller:
        return 40
    return 700

Things to know

  • Status is Stagnant; it was never scheduled into a hard fork.
  • The pricing it targeted no longer exists. EIP-2929 (Berlin) replaced the flat 700 with cold/warm access pricing, under which an address already accessed in the transaction, including the currently executing contract, is charged the warm rate of 100 gas.
  • No mainstream compiler switched internal functions to self-calls; they still use JUMP.
  • A related idea for cheaper calls to precompiles is EIP-2046.

#gas #evm #call #compilers #core

Details

Authors
Alex Beregszaszi (@axic), Jacques Wagener (@jacqueswww)
Status
Stagnant
Type
Standards Track / Core
Created
2018-08-31
Requires

Machine-readable: Markdown · JSON