Solidity Solidity Gas Optimization 3 — Questions and Answers
Question 1: What is the gas benefit of using `unchecked` arithmetic blocks in Solidity 0.8+?
- It skips the automatic overflow/underflow checks, saving ~20 gas per operation (Correct answer)
- It allows arithmetic to use fewer storage slots
- It converts all math to bitwise operations automatically
- It disables the ABI encoder for numeric types
Correct answer: It skips the automatic overflow/underflow checks, saving ~20 gas per operation
Wrapping arithmetic in `unchecked {}` disables the compiler-inserted overflow checks, saving gas when you have already proven the values cannot overflow.
Question 2: Why is it more gas-efficient to revert early in a function rather than at the end?
- Reverting early returns unused gas to the caller before expensive operations execute (Correct answer)
- Late reverts cost a fixed 21,000 gas penalty
- Early reverts bypass the ABI decoder
- The EVM charges double gas for late reverts
Correct answer: Reverting early returns unused gas to the caller before expensive operations execute
Gas is consumed as each opcode executes; reverting before expensive storage reads/writes returns the remaining gas unspent.
Question 3: What is 'gas griefing' in the context of forwarding gas with `call` in Solidity?
- A malicious callee can consume all forwarded gas, causing the caller to run out and revert (Correct answer)
- The caller steals gas from the callee by setting a low gas limit
- Gas griefing is a compiler bug affecting loops
- It occurs when two contracts mutually call each other
Correct answer: A malicious callee can consume all forwarded gas, causing the caller to run out and revert
When a contract forwards all available gas to an external call, a malicious recipient can burn it all, forcing the calling contract to revert due to out-of-gas.
Question 4: What is the gas cost difference between deploying a contract and calling a function on an existing contract?
- Deployment costs at least 32,000 base gas plus code storage; function calls cost 21,000 base plus execution (Correct answer)
- Deployment and calls both cost exactly 21,000 gas base
- Function calls on existing contracts are always free
- Deployment costs are measured in ETH, not gas
Correct answer: Deployment costs at least 32,000 base gas plus code storage; function calls cost 21,000 base plus execution
Contract creation has a base cost of 32,000 gas plus 200 gas per byte of bytecode stored, making deployment significantly more expensive than function calls.
Question 5: How do Solidity custom errors (introduced in 0.8.4) save gas compared to `require` with string messages?
- Custom errors encode only a 4-byte selector in revert data, while string messages store and return the full string (Correct answer)
- Custom errors skip the revert opcode entirely
- Custom errors are stored in a separate cheaper memory area
- String messages are charged at 20,000 gas per character
Correct answer: Custom errors encode only a 4-byte selector in revert data, while string messages store and return the full string
Custom errors revert with just a 4-byte function selector (plus encoded parameters), whereas string error messages cost gas to encode, store, and return.
Question 6: What gas optimization does 'lazy initialization' or 'default value avoidance' provide in Solidity?
- Avoiding writing 0 to storage saves 20,000 gas because SSTORE to a non-zero slot costs less than creating a new slot (Correct answer)
- Zero values are stored in calldata instead of storage
- Default values are inlined by the optimizer as constants
- Variables initialized to zero skip the constructor
Correct answer: Avoiding writing 0 to storage saves 20,000 gas because SSTORE to a non-zero slot costs less than creating a new slot
Writing a non-zero value to a zero storage slot costs 20,000 gas (SSTORE cold write), but resetting a slot to zero triggers a gas refund — so avoid unnecessary zero-to-nonzero writes.
What is the gas benefit of using `unchecked` arithmetic blocks in Solidity 0.8+?