Ordering of storage variables and struct members affects how they can be packed tightly.
For example, declaring your storage variables in the order of uint128
, uint128
, uint256
instead of uint128
, uint256
, uint128
, as the former will only take up two slots of storage whereas the latter will take up three.
- State Vars Ordering
- Declaration -> Packing
- Packing -> Gas Costs
- SLOADs & SSTOREs
uint128
,uint128
,uint256
- Two Storage Slots
uint128
,uint256
,uint128
- Three Storage Slots
Storage, Stack Memory, Storage, and Flow Operations, Storage Layout, Storage Packing, [Storage Layout & Inheritance], Storage Layout for Mappings & Dynamic Arrays