Solidity manages state through a rigid storage layout where variables are assigned to 32-byte slots. Understanding how these types behave across storage, memory, and calldata is foundational for writing gas-efficient smart contracts.
Value types
Value types are passed by value, meaning they are always copied when used as function arguments or in assignments. This category includes booleans, integers of varying bit-widths, addresses, and fixed-size byte arrays.
uint8touint256(steps of 8 bits): Unsigned integers.int8toint256(steps of 8 bits): Signed integers.address: A 20-byte value representing an Ethereum account.bool: A 1-byte value holding true or false.bytes1tobytes32: Fixed-size byte arrays.enum: User-defined types that default touint8.
Since Solidity 0.8.0, arithmetic operations revert on overflow or underflow by default. Developers can opt-out of these checks using unchecked { ... } blocks to save gas in scenarios where overflow is mathematically impossible or desired.
Reference types and data location
Reference types, including structs, arrays, strings, and mappings, require an explicit data location declaration. Data location dictates where the variable is stored and how it behaves during execution.
| Location | Persistence | Mutability | Gas Cost |
| :--- | :--- | :--- | :--- |
| storage | Persistent | Mutable | High |
| memory | Volatile | Mutable | Moderate |
| calldata | Input-only | Immutable | Lowest |
storage refers to the contract's permanent state on the blockchain. memory is used for temporary data during a function execution. calldata is a special location for input parameters that are read-only and is typically the cheapest way to pass data into a function.
Mappings and dynamic arrays
Mappings function as hash tables and are not iterable. The value corresponding to a key is located at keccak256(abi.encode(key, slot)), where slot is the storage index of the mapping variable.
Dynamic arrays require two storage areas. The variable slot itself stores the length of the array. The elements are located at keccak256(slot) as the base address, with individual indices calculated as base + index.
- Mappings cannot be passed to functions in memory.
bytesandstringare special dynamic arrays that are stored in a slightly optimized manner.
Packing and slot arithmetic
Solidity attempts to pack multiple variables into a single 32-byte storage slot if their combined size is less than or equal to 32 bytes. This happens sequentially as variables are declared.
uint128 a; // Slot 0, bytes 0-15
uint128 b; // Slot 0, bytes 16-31
uint256 c; // Slot 1, bytes 0-31
If a variable cannot fit into the remaining space of a current slot, Solidity moves it to the next slot. Proper ordering of variables significantly impacts gas consumption during contract deployment and state updates. Grouping smaller types together allows the EVM to perform single SSTORE operations for multiple variables.
Choosing types that save gas
Optimizing gas involves balancing readability with strict type constraints. Using uint256 is often cheaper than uint8 for local variables because the EVM is optimized for 32-byte word operations.
However, for struct members, uint8 is highly beneficial as it allows for tight packing within storage slots. Developers should avoid abi.encodePacked when hashing multiple dynamic arguments, as this can lead to hash collisions; abi.encode is the safer, standard approach for generating unique identifiers.