Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Solidity Compiler Optimization
📖 Scenario: You are building a simple smart contract on the Ethereum blockchain. You want to learn how to optimize your Solidity code using compiler settings to save gas costs when deploying and interacting with your contract.
🎯 Goal: Learn how to set up a basic Solidity contract and enable compiler optimization to reduce gas usage.
📋 What You'll Learn
Create a simple Solidity contract with a state variable and a function to update it.
Add a compiler optimization setting variable.
Use the optimization setting in the contract's pragma directive.
Deploy and verify the contract outputs the expected value.
💡 Why This Matters
🌍 Real World
Smart contracts on blockchains like Ethereum must be optimized to save gas fees, which are real money costs for users.
💼 Career
Blockchain developers need to understand compiler optimization to write efficient, cost-effective smart contracts.
Progress0 / 4 steps
1
Create a simple Solidity contract
Write a Solidity contract named SimpleStorage with a public uint variable called storedData initialized to 100. Add a function set(uint x) that updates storedData to x.
Blockchain / Solidity
Hint
Start with pragma solidity ^0.8.0; and define the contract with the variable and function as described.
2
Add compiler optimization setting variable
Create a boolean variable called optimizationEnabled and set it to true to represent enabling compiler optimization.
Blockchain / Solidity
Hint
Just add bool optimizationEnabled = true; inside the contract.
3
Use optimization setting in pragma directive
Modify the pragma solidity directive to include the compiler optimization setting by adding optimizer: { enabled: true, runs: 200 } as a comment above the contract.
Blockchain / Solidity
Hint
Add a comment above pragma solidity showing the optimization settings as optimizer: { enabled: true, runs: 200 }.
4
Print the storedData value
Write a Solidity function called getStoredData that returns the current value of storedData. Then, simulate calling getStoredData after setting storedData to 250 and print the result as Stored data is: 250.
Blockchain / Solidity
Hint
Create a getStoredData function that returns storedData. Then imagine calling set(250) and printing the returned value.
Practice
(1/5)
1. What is the main purpose of enabling compiler optimization in Solidity?
easy
A. To reduce gas costs and improve contract efficiency
B. To add more features to the Solidity language
C. To make the contract code longer and more complex
D. To disable contract deployment on test networks
Solution
Step 1: Understand compiler optimization purpose
Compiler optimization focuses on improving how the code runs, mainly by reducing gas usage.
Step 2: Identify the effect on contracts
Optimized contracts use less gas and run more efficiently on the blockchain.
Final Answer:
To reduce gas costs and improve contract efficiency -> Option A
Quick Check:
Optimization = reduce gas and improve efficiency [OK]
Hint: Optimization saves gas by making code efficient [OK]
Common Mistakes:
Thinking optimization adds new language features
Believing optimization increases contract size
Confusing optimization with deployment settings
2. Which of the following is the correct way to enable optimization when compiling a Solidity contract using solc?
easy
A. solc --optimize-runs Contract.sol
B. solc --no-optimize Contract.sol
C. solc --optimize-runs=off Contract.sol
D. solc --optimize --optimize-runs 200 Contract.sol
Solution
Step 1: Recall correct optimization flags
The correct flags are --optimize to enable optimization and --optimize-runs to set optimization runs.
Step 2: Check each option for syntax correctness
solc --optimize --optimize-runs 200 Contract.sol uses both flags correctly with a numeric value for runs; others are incorrect or incomplete.
Final Answer:
solc --optimize --optimize-runs 200 Contract.sol -> Option D
Quick Check:
Enable optimization with --optimize and runs number [OK]
Hint: Use both --optimize and --optimize-runs with a number [OK]
Common Mistakes:
Omitting --optimize flag
Using invalid or missing runs value
Using incorrect flag syntax
3. Given the following Solidity code compiled with optimization enabled and runs set to 1, what is the expected effect on gas usage when calling increment() multiple times?
contract Counter {
uint public count;
function increment() public {
count += 1;
}
}
medium
A. Gas cost increases exponentially with each call
B. Lower gas cost per call optimized for many calls
C. Higher gas cost per call but optimized for fewer calls
D. No change in gas cost per call
Solution
Step 1: Understand optimize-runs parameter meaning
Setting optimize-runs to 1 tells the compiler to optimize for fewer executions, making each call more expensive but overall smaller code.
Step 2: Analyze gas cost effect on repeated calls
With runs=1, gas cost per call is higher, but contract size is smaller; optimized for few calls.
Final Answer:
Higher gas cost per call but optimized for fewer calls -> Option C
Quick Check:
optimize-runs=1 means optimize for fewer calls [OK]
Hint: Low optimize-runs means higher gas per call, fewer calls optimized [OK]
Common Mistakes:
Assuming runs=1 optimizes for many calls
Thinking gas cost stays the same
Confusing contract size with gas cost
4. You compiled a Solidity contract with optimization enabled but noticed unexpected behavior during testing. Which of the following is the most likely cause?
medium
A. The contract was deployed on the wrong network
B. Optimization changed the logic causing subtle bugs
C. The Solidity version was too old to support optimization
D. The contract source code was missing comments
Solution
Step 1: Understand optimization effects on code
Optimization can rearrange or simplify code, sometimes causing subtle logic changes or bugs.
Step 2: Identify likely cause of unexpected behavior
Unexpected behavior after optimization usually means optimization affected logic; other options are unrelated.
Final Answer:
Optimization changed the logic causing subtle bugs -> Option B
Quick Check:
Optimization can cause subtle logic bugs [OK]
Hint: Test contracts carefully after enabling optimization [OK]
Common Mistakes:
Blaming network deployment instead of optimization
Ignoring compiler version compatibility
Thinking comments affect optimization
5. You want to optimize a Solidity contract that will be called many times after deployment. Which compiler optimization setting should you choose to minimize total gas cost over many calls?
hard
A. Enable optimization with --optimize-runs set to a high number like 10000
B. Disable optimization to keep code simple
C. Enable optimization with --optimize-runs set to 1
D. Use no optimization flags and rely on manual gas saving
Solution
Step 1: Understand optimize-runs impact on gas cost
High optimize-runs value tells the compiler to optimize for many executions, reducing gas cost per call.
Step 2: Choose setting for many calls
Setting --optimize-runs to a high number like 10000 minimizes gas cost over many calls, best for frequently used contracts.
Final Answer:
Enable optimization with --optimize-runs set to a high number like 10000 -> Option A
Quick Check:
High optimize-runs = optimize for many calls [OK]
Hint: Use high optimize-runs for contracts called many times [OK]
Common Mistakes:
Using low optimize-runs for frequently called contracts