SSTORE Cost: Zero-to-Nonzero Is 20× Nonzero-to-Nonzero
Bottom line: Writing a storage slot from zero costs 22,100 gas (20,000 plus warm access); overwriting a non-zero slot costs 2,900. A loop that initialises one slot per iteration and a loop that updates existing slots differ by an order of magnitude — and the refund only comes back if the slot is zeroed later.
The schedule, EIP-2200/3529
| Operation | Gas |
|---|---|
| SLOAD warm | 100 |
| SLOAD cold | 2,100 |
| SSTORE 0 → nonzero | 22,100 |
| SSTORE nonzero → nonzero | 2,900 |
| SSTORE nonzero → 0 | 2,900 (refund 4,800) |
| SSTORE 0 → nonzero → 0, same tx | 100 with refund |
Refunds are capped at 20% of transaction gas since EIP-3529; a contract built on refund farming pays more than it recovers.
What this means for design
- Initialise in one loop, update in another. A 100-item map created and updated in the same loop pays 22,100 per item; two loops pay 22,100 once and 2,900 after.
- Pack small values. Eight
uint8fields in one slot is one 22,100 write instead of eight. - Do not clear and rewrite within a transaction unless the refund cap allows it; the same slot written three times is 22,100 + 2,900 + 2,900.
Measuring
forge snapshot on a per-function basis, and forge test --gas-report. Compare storage layout across versions; a struct field reorder can silently change which slots are zero.
1 | // 22,100 per item |
The trap with mappings
A mapping inside a loop each iteration hits a different cold slot: 2,100 + 22,100 = 24,200 per item. An array of structs packed together amortises the cold access across items in the same slot. Layout choices dominate optimisation effort here far more than micro-tweaks to arithmetic.
Operating notes
Snapshot gas before and after a storage change, not the total transaction. forge snapshot --diff gives the per-function delta, which is the only figure a layout change should be judged on.
Pack aggressively. Eight uint8 in one slot is one 22,100 write instead of eight; four uint64 similarly. Reorder struct fields by size and the compiler packs them — the same fields in declaration order can occupy four slots instead of one.
Mappings versus arrays
Mapping writes never share a slot: each iteration is a cold access plus a fresh write, about 24,200 gas. An array of packed structs amortises cold access across items in the same slot. Where iteration order does not matter, the array is cheaper by an order of magnitude.
Refund ceiling
Since EIP-3529 refunds cap at 20% of consumed gas. Clearing ten slots in a transaction recovers 48,000 of refundable value but only 20% of the total is payable — build the refund into the estimate rather than assuming all of it.
What to watch in review
A loop with an SSTORE from zero each iteration, a struct written field-by-field instead of assigned wholesale, and any delete inside a loop. Those three account for most avoidable storage cost, and all three are visible in the diff.
FAQ
Why does writing a zero slot cost so much more than updating a non-zero slot?
Writing a storage slot from zero to non-zero costs 22,100 gas, which is 20,000 plus warm access. Overwriting a slot that already holds a non-zero value costs 2,900 gas. The gap exists because the zero-to-nonzero write creates new state, while the overwrite replaces existing state.
How can I avoid paying the zero-to-nonzero cost repeatedly in a loop?
Split initialisation and updates into two separate loops. A loop that creates and updates each item pays 22,100 per item, while two loops pay 22,100 once per slot and 2,900 for each subsequent update. Initialise in one loop, then update in another.
Why is a mapping in a loop more expensive than an array of packed structs?
Each mapping iteration hits a different cold slot, costing 2,100 for cold access plus 22,100 for the write, about 24,200 per item. An array of packed structs amortises the cold access across items sharing a slot. Where iteration order does not matter, the array is cheaper by an order of magnitude.
How much of an SSTORE refund can I actually recover?
Since EIP-3529, refunds are capped at 20% of transaction gas. Clearing ten slots recovers 48,000 of refundable value, but only 20% of the total is payable. Build the refund into your estimate rather than assuming all of it comes back.
How should I measure the gas impact of a storage layout change?
Snapshot gas before and after the storage change, not the total transaction. Use forge snapshot --diff for the per-function delta, which is the only figure a layout change should be judged on. Also compare storage layout across versions, since a struct field reorder can silently change which slots are zero.











