Solidity theorytheory 0/50 · 0%
Gas · medium

29. Storage layout and gas

Slots, packing and cold versus warm access.

Storage is a map of 32-byte slots. Consecutive small variables share a slot if they fit. The first read of a slot in a transaction is cold (2100 gas); later reads are warm (100). Writing a zero slot to non-zero costs 20000.

uint128 a; uint128 b; // one slot
uint256 c; bool d;    // two slots

Cache storage reads in memory inside loops, and prefer `calldata` for read-only external arguments.

Check your understanding

  1. 1. How large is one storage slot?

  2. 2. Which write is most expensive?

  3. 3. What should you do with storage reads in a loop?