Two half-width boxes fit side by side in one drawer.
SEE IT FOR YOURSELF
Pack small values into Solidity storage slotsStep 1 / 4
In this simple default-layout example, two uint128 values come before one uint256. Each slot holds thirty-two bytes.
In this simple default-layout example, two uint128 values come before one uint256. Each slot holds thirty-two bytes.The first sixteen-byte value occupies the low-order half of slot zero. Low-order bytes are shown on the right.The second uint128 fits in the remaining sixteen bytes. Both values now share slot zero.C needs a complete thirty-two-byte slot, so it begins at slot one. Two packed small values plus C use two slots.
Animated local teaching model. No wallet, RPC or live blockchain data. Some labels and hashes are abbreviated for readability.
Measure the declared types
uint128 occupies sixteen bytes in ordinary packed storage. Two adjacent uint128 fields can therefore fit together in one thirty-two-byte word.
Place the first field low
The first field starts at the lower-order end of the slot. The next field occupies the following higher-order bytes when it fits the remaining space.
Move a field that does not fit
A later uint256 needs a complete slot. It cannot fit into a partly occupied word and is placed in the next available slot under these simple layout rules.
Do not equate packing with guaranteed savings
Updating one packed field may require reading and masking the shared word. Gas effects depend on actual access patterns rather than the variable widths alone.