In this track: Storage, memory and layout
advanced · 4 MIN READ

Pack adjacent value types

Compatible small fields can share one slot.

LET’S PICTURE IT

Two half-width boxes fit side by side in one drawer.

SEE IT FOR YOURSELF
Pack small values into Solidity storage slotsStep 1 / 4
Pack small values into Solidity storage slotsFirstuint128 A16 bytesSeconduint128 B16 bytesThirduint256 C32 bytesSimple elementary types only; not a complete compiler layoutSlot 0 · 32 bytesUnused · 16 BA · 16 BHigh-order bytes ← → low-order bytesSlot 0 · fullB · 16 BA · 16 BDeclaration order and sizes determine this packingSlot 0BASlot 1C · 32 bytesTry different sequences in the storage-layout tool

In this simple default-layout example, two uint128 values come before one uint256. Each slot holds thirty-two bytes.

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.

Inspect the layout

uint128 a; // slot 0, byte offset 0
uint128 b; // slot 0, byte offset 16
uint256 c; // slot 1, byte offset 0

This simple layout assumes no earlier inherited variables or custom storage base.

TURN UNDERSTANDING INTO PRACTICEOpen the Storage layout →
QUICK CHECK · NO PRESSURE

Can two adjacent uint128 fields share one ordinary slot?

Your progress stays in this browser.
Keep exploring the original referenceSolidity documentation ↗

Original explanations by EtherTools. Examples are educational and source-linked; this site is independent of the Ethereum Foundation. Our approach.