---
eip: 1380
label: EIP-1380
title: Reduced gas cost for call to self
status: Stagnant
type: Standards Track
category: Core
created: 2018-08-31
requires: 150
official: https://eips.ethereum.org/EIPS/eip-1380
source: https://github.com/ethereum/EIPs/blob/master/EIPS/eip-1380.md
canonical: https://eips.huatuo.xyz/zh/eip-1380/
lang: zh
tldr: 提议把合约对自身的 CALL 的 gas 从 700 降到 40，让编译器可以使用廉价且内存干净的自调用，而不是基于 JUMP 的内部函数。
tags: [gas, evm, call, compilers, core]
---

# EIP-1380: Reduced gas cost for call to self

> Reduce the gas cost for call instructions, when the goal is to run a new instance of the currently loaded contract.

**TL;DR:** 提议把合约对自身的 CALL 的 gas 从 700 降到 40，让编译器可以使用廉价且内存干净的自调用，而不是基于 JUMP 的内部函数。

## 它解决什么问题？

Solidity 和 Vyper 等语言用 `JUMP` 来实现内部函数调用：复用同一个 EVM 上下文，编译器必须在每次调用前后保存和恢复栈与内存。这很复杂，使"pure"函数难以得到保证（残留的内存可能泄漏给被调用者），而且调用嵌套时会很快耗尽 1024 项的栈上限。

对合约自身地址的 `CALL` 可以为每个内部函数提供全新的内存和栈，而在编译器复杂度方面不需要任何代价。但当时所有类型的调用都要 700 gas，这是 [EIP-150](/eip-150/) 设定的，主要用来支付从磁盘加载另一个合约代码的开销。对*自己*的调用不需要这样的加载，因为代码已经在内存中了。

## 它如何运作

在分叉区块之后，当且仅当目标地址等于当前合约的地址时，`CALL`、`DELEGATECALL`、`CALLCODE` 和 `STATICCALL` 的费用将是 **40 gas 而不是 700**。

作者的理由是：在 EIP-150 之前，这些调用的费用是 40。EIP-150 为磁盘 I/O 增加了大约 660。由于自调用不会进行新的 I/O，原来的 40 对于为已加载的代码启动一个新的虚拟机实例来说是公平的价格。

调用的其他方面保持不变（value 转账费用、内存扩展、63/64 gas 规则）。

## 示例代码

如果自调用很便宜，编译器可能为内部函数生成的代码，用 Solidity 展示如下：

```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));
    }
}
```

用伪代码表示的费用规则：

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

## 需要注意

- 状态为 **Stagnant**；它从未被排入任何硬分叉。
- 它所针对的定价已不复存在。[EIP-2929](/eip-2929/)（Berlin）用冷/热访问定价取代了统一的 700，在新规则下，交易中已经访问过的地址（包括当前正在执行的合约）按热访问费率 100 gas 收费。
- 没有主流编译器把内部函数改成自调用；它们仍然使用 `JUMP`。
- 关于让预编译合约调用更便宜的相关想法是 [EIP-2046](/eip-2046/)。


---

This is an unofficial plain-language summary. Read the full specification: https://eips.ethereum.org/EIPS/eip-1380
