The Ethereum Virtual Machine (EVM) functions as a 256-bit stack-based architecture, designed for deterministic execution of smart contracts across a distributed network. Understanding how data resides in stack, memory, and storage is fundamental for developers aiming to optimize gas efficiency and avoid common traps like the 'Stack Too Deep' error.
A stack machine, not a register machine
Unlike traditional CPU architectures that rely on registers, the EVM utilizes a last-in-first-out (LIFO) stack. This design simplifies the implementation of contract code but introduces specific constraints on how data can be accessed during computation.
Operations can only access the top 16 items of the stack directly. Any data located deeper than the 16th position is inaccessible until items above it are popped or manipulated.
This architecture necessitates frequent use of DUP and SWAP instructions by Solidity compilers. When a function has too many local variables, the compiler cannot reach the required data, triggering a 'Stack Too Deep' error.
Three places to put data
The EVM provides distinct storage environments, each with unique persistence guarantees and cost structures. Developers must choose the appropriate medium based on data size and longevity requirements.
- Stack: Limited to 1024 words (32 bytes each). Extremely fast but ephemeral.
- Memory: Byte-addressed, linear, and volatile. Expands as needed during contract execution.
- Storage: Persistent key-value mapping on the blockchain. Slots are 32 bytes wide.
| Data Type | Persistence | Accessibility | Gas Cost | | :--- | :--- | :--- | :--- | | Stack | Ephemeral | Top 16 items | Very Low | | Memory | Ephemeral | Byte-indexed | Medium (Variable) | | Storage | Persistent | Key-Value | Very High |
What each one costs
Memory usage incurs a quadratic cost function. The first 512 bytes are relatively cheap, but costs scale quickly as memory grows to prevent unbounded loops from stalling nodes. This is an essential security measure against potential denial-of-service (DoS) vectors.
Storage costs are significantly higher due to the requirement for global consensus and persistence. A SSTORE opcode that changes a zero slot to a non-zero value costs 20,000 gas. Subsequent updates to a dirty slot are cheaper at 5,000 gas.
EIP-3529 changed how refunds work, capping them at one-fifth of the total gas expended by the transaction. This prevents developers from incentivizing 'self-destruct' cycles to artificially lower transaction costs.
Storage layout and slots
Contract storage is a massive, sparse array of $2^{256}$ slots. Each slot holds 32 bytes. When variables are declared in Solidity, the compiler assigns them sequential slots, packing smaller types like uint8 into a single 32-byte slot to save gas.
Transient storage, introduced via EIP-1153, provides a new mechanism for temporary data via TLOAD and TSTORE. This data is cleared automatically at the end of the transaction, providing a highly efficient alternative to storage for reentrancy locks.
Developers must be aware of the 24,576-byte limit on deployed bytecode mandated by EIP-170. EIP-3860 further constrains init code to 49,152 bytes, reinforcing the need for modular, lean contract design.
Consequences for contract design
Calldata is the cheapest location for input data, costing only 4 gas per zero byte and 16 gas per non-zero byte. Smart contracts should be designed to read as much information as possible from calldata rather than loading data into memory or storage.
Proper layout planning in storage can minimize expensive SSTORE operations. Grouping frequently updated variables together in the same slot allows multiple values to be updated in a single write operation, effectively amortizing the gas cost.
Ultimately, efficient contract design focuses on minimizing the footprint of the state. By leveraging transient storage and optimizing calldata parsing, developers ensure their contracts remain performant and sustainable under peak network load.