Ethereum Overview of the Ethereum Virtual Machine (EVM) Data Storage Mechanism

Ethereum The Ethereum Virtual Machine (EVM) is the runtime environment for executing smart contracts, and its efficient and secure data storage mechanism is the cornerstone of the stable operation of the entire Ethereum ecosystem. Understanding how the EVM processes and stores data is crucial for smart contract developers to optimize contract performance, reduce gas costs, and ensure contract security.The EVM primarily provides five data storage areas, each with its own unique characteristics, lifecycle, and gas cost model.

Storage: Persistent Storage for Smart Contracts

以太坊虚拟机(EVM)数据存储机制深度解析

Storage is the most central storage area for smart contracts, used to preserve the contract’s permanent state data. Its main characteristics include:

  • Persistence: Data is permanently stored on the blockchain and remains accessible across different function calls and transactions.
  • Structure: Each smart contract has its own independent storage space, organized as a massive key-value map with a total of 2^256 slots, each 32 bytes (256 bits) in size.
  • Data Structure Storage Methods:
    • Primitive Types: Fixed-size variables (such asuint256) occupy storage slots sequentially in the order they are declared, starting from slot 0. The EVM attempts to pack variables smaller than 32 bytes into the same slot to save space and gas.
    • Static Arrays: Elements are stored sequentially in consecutive slots.
    • Dynamic arrays: The first slot stores the array length, and the actual elements begin to be stored starting fromkeccak256(slot)the calculated position.
    • Mappings: Similar to a hash table, key-value pairs are stored non-contiguously. The storage location for each key-value pair is determined by hashing the key and the mapping’s location in memory.
    • Structs: Variables within a struct are stored sequentially; the EVM attempts to pack multiple variables into a single 32-byte storage slot.
  • Gas Cost: Writing data to Storage incurs the highest gas cost. Modifying a storage slot from zero to a non-zero value costs approximately 20,000 gas. The first read (cold SLOAD) costs approximately 2,100 gas, while subsequent reads (warm SLOAD) cost approximately 100 gas.

As of April 2026, the design of storage pricing and gas accounting is still under active exploration, with the goal of optimizing control over EVM storage growth. This includes methods such as real-time gas accounting, deferred gas accounting, separating storage costs, and controlling storage growth based on volume.

Memory: Temporary storage during function execution

Memory is a temporary, mutable storage area where data exists only during the execution of a contract function. Once the function execution ends, the data in Memory is released.

以太坊虚拟机(EVM)数据存储机制深度解析

  • Uses: Suitable for intermediate calculations, temporary variables, and data operations within a single function call.
  • Gas Cost: Cheaper than Storage, but Gas costs increase quadratically as memory usage rises.

Calldata: Read-only parameters for external function calls

Calldata is a special, read-only, immutable data location primarily used to store parameters for external function calls. It is a byte-addressable space that is more efficient than Memory and is particularly suitable for passing dynamic data that does not require modification.

  • Usage: Input parameters for external function calls.
  • Gas Cost: Cheaper than Memory. Each non-zero byte of Calldata costs 16 Gas, while a zero-byte value costs 4 Gas.

Stack: The EVM Execution Stack

以太坊虚拟机(EVM)数据存储机制深度解析

The EVM is a stack-based virtual machine. The stack is a Last-In, First-Out (LIFO) data structure used to store temporary values during smart contract execution. EVM opcodes directly manipulate data on the stack.

  • Characteristics: Each stack element is 32 bytes, with a maximum depth of 1,024 elements.
  • Limitations: The stack cannot directly store complex types such as arrays, strings, or maps.
  • Gas Cost: The gas cost per operation is relatively low, but there is a depth limit.

Transient Storage: Transient storage for the duration of a single transaction

Transient Storage, introduced via EIP-1153, is a non-persistent storage mechanism where data exists only for the duration of a single transaction and is cleared once the transaction ends. It provides a low-cost way to share data between different function calls within the same transaction without permanently writing the data to the blockchain.

  • Availability: Starting with Solidity v0.8.30, transient storage is supported for value types (such asuint256, bool) via thetransientkeyword. Support for reference types is expected to be provided in future versions.
  • Gas Cost: Compared to permanent writes to Storage, Transient Storage is significantly less expensive because it does not involve permanent modifications to on-chain state.

Gas Cost Comparison and Optimization Strategies

以太坊虚拟机(EVM)数据存储机制深度解析

Understanding the differences in gas costs across various storage types is key for smart contract developers to optimize their code.Storage has the highest write costs, so unnecessary on-chain storage should be minimized. Memory and Calldata are suitable for temporary data and function parameter passing; among these, Calldata is typically more cost-effective for passing large amounts of dynamic data due to its read-only nature. Transient Storage offers a new, low-cost option for sharing data across functions within a single transaction.

Smart contract developers should select the appropriate storage location based on the data’s persistence, mutability, access frequency, and gas costs.For example, reordering struct variables to minimize slot usage, using static arrays whenever possible, and making judicious use of maps are all effective methods for optimizing storage layout and reducing gas costs. At the same time, auditors need a deep understanding of the EVM’s memory model and storage mechanisms to identify potential vulnerabilities and opportunities for gas optimization.