Solidity Solidity Gas Optimization 1 — Questions and Answers
Question 1: Which storage location is the cheapest in terms of gas cost in Solidity?
- memory
- storage
- calldata (Correct answer)
- transient storage
Correct answer: calldata
Calldata is the cheapest because it is read-only and stored in the transaction input data, requiring no SSTORE or MSTORE opcodes.
Question 2: What is the gas cost difference between SLOAD (reading storage) in a cold vs warm slot in EIP-2929?
- Cold: 2100 gas, Warm: 100 gas (Correct answer)
- Cold: 800 gas, Warm: 200 gas
- Cold: 5000 gas, Warm: 800 gas
- Cold: 2100 gas, Warm: 800 gas
Correct answer: Cold: 2100 gas, Warm: 100 gas
EIP-2929 introduced cold/warm storage access: first access costs 2100 gas, subsequent accesses in the same transaction cost only 100 gas.
Question 3: Why is it more gas-efficient to use `uint256` instead of `uint8` or `uint128` for standalone state variables?
- The EVM operates on 32-byte words, so smaller types require masking operations that cost extra gas (Correct answer)
- uint256 uses fewer storage slots than smaller types
- Smaller types are deprecated in Solidity 0.8
- The compiler converts all uints to uint256 at compile time anyway
Correct answer: The EVM operates on 32-byte words, so smaller types require masking operations that cost extra gas
The EVM natively works with 32-byte (256-bit) values; using smaller integer types triggers extra masking/padding opcodes unless they are packed together in structs.
Question 4: What gas optimization does Solidity variable packing in structs provide?
- Multiple small variables can share a single 32-byte storage slot, reducing SSTORE/SLOAD calls (Correct answer)
- Packed structs are stored in memory instead of storage automatically
- Packing removes the need for ABI encoding
- It allows using calldata for struct parameters
Correct answer: Multiple small variables can share a single 32-byte storage slot, reducing SSTORE/SLOAD calls
When consecutive struct members fit within 32 bytes, Solidity packs them into one slot, reducing the number of expensive storage reads/writes.
Question 5: What is the gas benefit of marking a function as `view` or `pure`?
- External callers pay zero gas when calling them off-chain via eth_call (Correct answer)
- They skip input validation entirely
- They use calldata instead of memory for all parameters
- They can bypass the require() checks
Correct answer: External callers pay zero gas when calling them off-chain via eth_call
View and pure functions called externally via `eth_call` (not in a transaction) are executed locally by the node and cost zero gas.
Question 6: Why is using `++i` preferred over `i++` in Solidity loops for gas optimization?
- Post-increment creates a temporary copy of the value, costing extra gas (Correct answer)
- Pre-increment skips the overflow check in Solidity 0.8
- ++i uses fewer opcodes in all compiler versions
- i++ is undefined behavior in Solidity
Correct answer: Post-increment creates a temporary copy of the value, costing extra gas
Post-increment (`i++`) stores the old value in a temporary variable before incrementing, which costs slightly more gas than pre-increment (`++i`) which modifies in place.
Which storage location is the cheapest in terms of gas cost in Solidity?