Introduction

Hi, I'm cardene, a software engineer and technical writer focused on blockchain and AI.

I've also authored Japanese technical books on specification-driven development with LLMs and decentralized application (DApp) development.

I also build and publish a variety of tools, libraries, and open-source projects.

EIP-5 proposes changing how gas is charged for memory used when calling another contract or function.

This article translates, summarizes, and expands on the following historical proposal.

More EIP explainers:

Overview

EIP-5 addresses the difficulty of calling functions that return strings or other dynamically sized arrays in the Ethereum Virtual Machine (EVM).

Under the call semantics considered by the proposal, a caller must specify the output buffer size in advance. It can specify a large buffer, but expanding memory for that buffer consumes gas even when the callee returns much less data. This makes dynamically sized return values expensive to accommodate.

EIP-5 proposes charging for output memory expansion according to the amount of data actually written when the call returns, rather than the entire requested output range.

The objective is to avoid paying for unnecessary output memory expansion, particularly when contracts exchange large or complex data structures.

Historical context: this article describes the proposed EIP-5 behavior, not the memory-expansion semantics of the modern Ethereum EVM. Modern contracts can obtain dynamically sized return data using the return-data mechanism introduced by EIP-211.

Specification

The proposed changes concern CALL, CALLCODE, and DELEGATECALL, collectively referred to as CALL* in the proposal. CREATE is unaffected because it does not copy its result into a caller-specified output memory buffer.

The proposal describes call arguments using the names gas, address, value, input_start, input_size, output_start, and output_size. Individual opcodes differ in their arguments; for example, DELEGATECALL has no separate value argument.

At the beginning of a call, memory expansion gas is charged for the input region ending at input_start + input_size. Under the proposed rules, expansion for the requested output region is deferred.

If the callee returns n bytes, the caller's output region extends only as far as output_start + min(output_size, n), subject to the applicable memory-expansion rules. The caller pays the resulting expansion cost, and up to the requested number of return bytes are copied into that region.

Consequently, the caller could run out of gas either when the call begins or when it completes.

After the call, MSIZE would report the resulting expanded memory size.

The intention is to make memory allocation and gas consumption more closely reflect the amount of return data copied, especially for contracts that exchange large amounts of data.

For background on interactions between contracts, see the following articles.

https://chaldene.net/upgradable-contracts

https://chaldene.net/solidity-delegatecall

For CALL, CALLCODE, and DELEGATECALL, see the following material.

Motivation

When invoking a subroutine, it is useful to specify a dedicated output region rather than allowing it to overwrite arbitrary caller memory.

However, knowing the output size before calling another contract can be difficult. The relevant data may be stored in the callee's storage, and determining its size may require an additional call.

Charging for memory that is never written is also unnecessary from the perspective of this proposal.

EIP-5 suggests allowing the caller to reserve a very large logical output region at the end of its memory. As the callee returns, only the portion needed for the actual output would be expanded and charged.

This would allow contracts to return dynamically sized strings and arrays more flexibly.

The proposal illustrates an approach using MSIZE as output_start and 2**256-1 as output_size. The caller would then inspect MSIZE after the call to identify the expanded output region.

Because EVM memory expansion is rounded to 32-byte words, the change in MSIZE alone does not generally identify the exact number of returned bytes. It identifies the expanded memory extent.

The proposal is particularly relevant to proxy contracts that forward both input and output data. A generic proxy should not need to know the callee's return size in advance and should be able to forward the actual return data.

This historical approach must not be copied into a modern CALL implementation. Requesting an enormous output buffer can cause an out-of-gas failure under current EVM memory-expansion rules. Modern proxies typically use RETURNDATASIZE and RETURNDATACOPY instead.

Subroutines

A subroutine is a reusable unit of code that performs a particular task. Different parts of a program can invoke the same subroutine rather than duplicating its instructions.

Reusability

A shared subroutine reduces code duplication and simplifies maintenance.

Structure

Dividing a program into smaller units makes development, testing, and reasoning about behavior more manageable.

Abstraction

A subroutine encapsulates functionality behind an interface, allowing callers to use it without understanding every implementation detail.

Subroutines are commonly called functions or methods. They can accept input parameters and return results, making them useful for composing more complex programs.

Rationale

The proposal aims to solve the output-size problem with minimal changes to the EVM.

Other approaches considered changing the call opcodes themselves or modifying their argument lists. Another possibility was applying different gas accounting only when the requested output size equaled 2**256-1.

However, the implementation would still need to handle memory expansion at different points around the call operation, so special-casing that value would not necessarily simplify the implementation.

An earlier design also considered returning the output size on the stack. The proposal instead considered the MSIZE-based mechanism sufficient for its intended use cases and more compatible with existing opcode interfaces.

Historical discussion is available here:

Compatibility

The proposed changes could affect contracts that observe their remaining gas or memory size. The proposal nevertheless argues that most existing contracts would be unlikely to depend on the affected behavior.

Gas accounting

The proposed rules avoid charging for the portion of an output buffer that remains unused. The original proposal argues that this does not increase the memory-expansion gas charged by the VM compared with the earlier rules.

Contracts should generally not rely on exact amounts of unused gas being returned by a subcall. Changes in gas accounting or in the timing of charges can affect such assumptions.

Memory size

MSIZE is commonly used to identify the current active memory extent. Changing when memory expands can affect existing code in two ways.

First, memory allocations could overlap. A contract might expect CALL to expand memory for an entire output slice even when the callee writes nothing into that slice. Under the proposed rules, a subsequent MSIZE-based allocation could overlap the unused portion.

Second, memory addresses could change. An object allocated using MSIZE could be placed at a different address because the previous call expanded less memory than before.

The proposal argues that well-designed memory objects should not depend on their absolute locations, and objects allocated as a single contiguous region, such as arrays, would generally be less affected by this issue.

These compatibility arguments describe the proposed change rather than establish that existing contracts are universally safe under it.

Implementation

EVM implementers would need to defer output memory expansion until the end of the call and check that sufficient gas remains before performing that expansion.

A representative use case in the original proposal is reserving a logical output region of 2**256-1 bytes without immediately expanding memory to that size.

Historical Python implementation examples are available on Vitalik Buterin's website:

The objective is to make output memory management more efficient while ensuring that expansion is subject to gas checks.

These links and the proposed implementation are retained for historical reference; their availability and execution behavior have not been independently tested as part of this translation.

Citation

Christian Reitwiessner c@ethdev.com, "EIP-5: Gas Usage for RETURN and CALL*," Ethereum Improvement Proposals, no. 5, November 2015. [Online serial]. Available: https://eips.ethereum.org/EIPS/eip-5.

Conclusion

EIP-5 explores a way to defer output memory-expansion charges until a contract call returns, reducing the cost of accommodating dynamically sized output. It illustrates an early design discussion surrounding generic contract calls and proxies.

Questions are welcome via Twitter @cardene777.

Other publications:

https://chaldene.net/