EVM Storage Optimization: How to Slash Smart Contract Gas Costs by 40%
A practical technical guide for Solidity engineers on storage slot packing, transient storage (EIP-1153), and Yul assembly to minimize costly SSTORE opcodes.
## Introduction
In the Ethereum Virtual Machine (EVM), **storage** is by far the most expensive resource. Executing a fresh `SSTORE` opcode (allocating a non-zero value to a zero slot) costs a staggering 20,000 gas, while an update to an existing warm slot consumes 2,900 gas.
By contrast, memory operations cost as little as 3 gas. For any protocol aiming for high throughput and user adoption, optimizing storage layout is not a premature optimization — it is foundational engineering.
1. Storage Slot Packing Mechanics
The EVM organizes permanent contract state into 32-byte (256-bit) slots indexed sequentially from `0` to $2^{256} - 1$. Variables declared consecutively are packed into the same 32-byte slot only if their combined sizes fit within 32 bytes.
The Inefficient Pattern
// Consumes 3 separate 32-byte slots (96 bytes)
contract InefficientVault {
uint128 public depositAmount; // Slot 0 (uses 16 bytes, wastes 16 bytes)
address public owner; // Slot 1 (uses 20 bytes, wastes 12 bytes)
uint128 public rewardRate; // Slot 2 (uses 16 bytes, wastes 16 bytes)
}
The Optimized Pattern
By ordering variables by size, we pack them into a single 32-byte slot:
// Packed into a SINGLE 32-byte slot (32 bytes total)
contract OptimizedVault {
address public owner; // 20 bytes
uint64 public depositAmount; // 8 bytes
uint32 public rewardRate; // 4 bytes
// Total = 20 + 8 + 4 = 32 bytes -> Slot 0
}
Reading and writing `owner`, `depositAmount`, and `rewardRate` simultaneously in a single transaction now requires only one `SLOAD` and one `SSTORE`, reducing execution cost drastically.
2. Transient Storage (EIP-1153)
With the Cancun upgrade, Ethereum introduced **Transient Storage** via `TSTORE` and `TLOAD`. Transient storage behaves like regular storage but is discarded automatically at the end of the transaction.
Reentrancy Locks: SSTORE vs TSTORE
Traditional reentrancy guards write `2` to a storage slot on entry and `1` on exit: - Legacy SSTORE reentrancy guard: ~5,000 gas - Modern TSTORE reentrancy guard: ~200 gas (over **95% savings**)
// Gas-efficient reentrancy guard using EIP-1153
abstract contract TransientReentrancyGuard {
bytes32 private constant REENTRANCY_SLOT = keccak256("cryptocoders.transient.guard");modifier nonReentrant() { assembly { if tload(REENTRANCY_SLOT) { // Revert with custom error mstore(0x00, 0xab143c06) // ReentrancyGuardReentrantCall() revert(0x1c, 0x04) } tstore(REENTRANCY_SLOT, 1) } _; assembly { tstore(REENTRANCY_SLOT, 0) } } } ```
3. Calldata vs Memory for Read-Only Arrays
When passing external function arrays, always specify `calldata` instead of `memory`. `calldata` provides a zero-copy pointer directly to the transaction payload, eliminating memory allocation overhead.
// Avoid: creates an expensive heap copy of the entire array
function processBatchMemory(uint256[] memory ids) external { ... }// Best Practice: zero memory expansion cost function processBatchCalldata(uint256[] calldata ids) external { ... } ```
Conclusion
Understanding the EVM at the opcode level enables Crypto Coders to deliver high-performance protocols that save our clients and their end users millions in cumulative gas fees.
