Solidity Solidity Gas Optimization 2 — Questions and Answers
Question 1: What is the gas cost benefit of using `immutable` vs regular state variables in Solidity?
- Immutable values are embedded in bytecode and read with cheap PUSH opcodes instead of SLOAD (Correct answer)
- Immutable variables are stored in calldata instead of storage
- Immutable removes the need for a constructor
- Immutable variables cost zero gas to declare
Correct answer: Immutable values are embedded in bytecode and read with cheap PUSH opcodes instead of SLOAD
Immutable variables are baked into the contract bytecode at deployment, so reading them costs only a few gas (PUSH opcode) instead of the 100–2100 gas of SLOAD.
Question 2: What is the `constant` keyword in Solidity and how does it save gas compared to storage variables?
- Constants are inlined at compile time as literal values, using zero gas to read (Correct answer)
- Constants are stored in a dedicated cheap storage area
- Constants skip ABI encoding when passed to functions
- Constants are cached in memory automatically
Correct answer: Constants are inlined at compile time as literal values, using zero gas to read
Constants are replaced by their literal values during compilation, so accessing them requires no storage read at all — they are embedded directly in the bytecode.
Question 3: How does short-circuiting in Solidity boolean expressions help with gas optimization?
- If the first condition of `&&` is false, the second is not evaluated, saving the gas for the expensive check (Correct answer)
- Short-circuiting converts OR operations to AND for cheaper evaluation
- It prevents the compiler from generating jump opcodes
- Boolean short-circuiting is handled by the optimizer only in assembly
Correct answer: If the first condition of `&&` is false, the second is not evaluated, saving the gas for the expensive check
In `A && B`, if A is false, B is skipped; placing cheap conditions first avoids executing expensive ones unnecessarily.
Question 4: What gas saving does using `bytes32` instead of `string` for fixed-length identifiers provide?
- bytes32 is stored in a single slot with no length prefix, while string uses dynamic encoding with extra overhead (Correct answer)
- bytes32 is stored in calldata automatically
- string uses 64 bytes per character in storage
- bytes32 skips ABI encoding entirely
Correct answer: bytes32 is stored in a single slot with no length prefix, while string uses dynamic encoding with extra overhead
A `bytes32` occupies exactly one 32-byte storage slot with no extra length or pointer overhead, while short strings under 32 bytes still use dynamic encoding in some contexts.
Question 5: Why does emitting events instead of storing data in storage save gas in Solidity?
- Events are stored in transaction logs, which cost ~8 gas/byte vs 20,000 gas for new storage writes (Correct answer)
- Events bypass the EVM entirely and are handled by the node off-chain
- Events use calldata encoding which is cheaper than storage encoding
- Events are never included in the blockchain state
Correct answer: Events are stored in transaction logs, which cost ~8 gas/byte vs 20,000 gas for new storage writes
Writing to storage (SSTORE) costs 20,000 gas for a new slot, while event logs cost approximately 375 gas plus ~8 gas per byte of data.
Question 6: What is the gas impact of the Solidity optimizer and what setting controls its strength?
- The `runs` parameter tells the optimizer to trade deployment cost for execution cost — higher runs = cheaper calls but larger bytecode (Correct answer)
- The optimizer removes all require() statements below a threshold
- Higher runs values compress storage layout
- The optimizer only affects view and pure functions
Correct answer: The `runs` parameter tells the optimizer to trade deployment cost for execution cost — higher runs = cheaper calls but larger bytecode
Setting `optimizer.runs` to a high number (e.g., 200+) optimizes for repeated execution efficiency at the expense of larger deployment bytecode.
What is the gas cost benefit of using `immutable` vs regular state variables in Solidity?