Gas optimization helps save money and speed up transactions on Layer 2 blockchains. It makes using blockchain cheaper and faster.
Gas optimization for L2 in Blockchain / Solidity
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Syntax
Blockchain / Solidity
// Example: Solidity function optimized for gas on L2 function add(uint a, uint b) external pure returns (uint) { unchecked { return a + b; } }
Use unchecked to skip overflow checks when safe, saving gas.
Minimize storage writes and external calls to reduce gas costs.
Examples
Blockchain / Solidity
// Using unchecked to save gas
function increment(uint x) external pure returns (uint) {
unchecked {
return x + 1;
}
}Blockchain / Solidity
// Packing variables to save storage
contract Example {
uint128 a;
uint128 b;
}Blockchain / Solidity
// Avoiding unnecessary state changes
function setValue(uint newValue) external {
if (value != newValue) {
value = newValue;
}
}Sample Program
This contract shows two ways to save gas on L2: using unchecked for addition and avoiding storage writes when not needed.
Blockchain / Solidity
pragma solidity ^0.8.20; contract GasOptimized { uint128 public count; // Increment count safely with gas optimization function increment() external { unchecked { count += 1; } } // Set count only if different to save gas function setCount(uint128 newCount) external { if (count != newCount) { count = newCount; } } }
Important Notes
Gas costs on L2 are lower but still matter for user experience and cost.
Always test your contract on testnets to measure gas savings.
Use tools like Hardhat or Tenderly to analyze gas usage.
Summary
Gas optimization reduces transaction fees and speeds up L2 blockchain use.
Techniques include using unchecked math, packing variables, and minimizing storage writes.
Testing and measuring gas helps find the best optimizations.
Practice
1. Which of the following is a common method to reduce gas costs on Layer 2 blockchains?
easy
Solution
Step 1: Understand calldata vs memory
Calldata is cheaper than memory because it is read-only and does not require copying data.Step 2: Identify gas saving method
Using calldata for function inputs reduces gas compared to memory or storage.Final Answer:
Using calldata instead of memory for function inputs -> Option DQuick Check:
Calldata is cheaper than memory [OK]
Hint: Choose calldata for inputs to save gas on L2 [OK]
Common Mistakes:
- Thinking increasing block size reduces gas
- Assuming more storage variables save gas
- Using loops without optimization
2. Which Solidity syntax correctly declares a function parameter to use calldata for gas optimization on L2?
easy
Solution
Step 1: Recall parameter data location keywords
Solidity allows memory, storage, or calldata for reference types in parameters.Step 2: Identify calldata usage
Calldata is specified explicitly asstring calldatafor external functions to save gas.Final Answer:
function foo(string calldata data) external {} -> Option BQuick Check:
Calldata keyword used correctly [OK]
Hint: Use 'calldata' keyword for external function parameters [OK]
Common Mistakes:
- Using memory instead of calldata for external inputs
- Omitting data location keyword
- Using storage incorrectly in parameters
3. What will be the gas cost difference when using
unchecked math in Solidity on L2 compared to normal math?medium
Solution
Step 1: Understand unchecked math
Unchecked math skips overflow and underflow checks, saving gas.Step 2: Compare gas costs
Skipping checks reduces gas cost compared to normal safe math operations.Final Answer:
Gas cost decreases because overflow checks are skipped -> Option AQuick Check:
Unchecked math saves gas by skipping checks [OK]
Hint: Unchecked math skips checks, lowering gas [OK]
Common Mistakes:
- Assuming unchecked math adds overhead
- Believing gas cost is unchanged
- Ignoring overflow risks
4. Given this Solidity snippet on L2, what is the main issue causing higher gas usage?
uint256 public count;
function increment() external {
count = count + 1;
}medium
Solution
Step 1: Identify gas cost in arithmetic
Normal addition includes overflow checks increasing gas.Step 2: Suggest unchecked usage
Wrapping increment inunchecked { count += 1; }saves gas by skipping checks.Final Answer:
Missing unchecked block for increment -> Option AQuick Check:
Unchecked block reduces gas for safe increments [OK]
Hint: Use unchecked for simple increments to save gas [OK]
Common Mistakes:
- Thinking public visibility affects gas here
- Confusing external with view function
- Assuming code is already optimized
5. You want to optimize a Layer 2 contract that stores multiple small variables. Which approach best reduces gas usage?
hard
Solution
Step 1: Understand storage slot packing
Multiple small variables like uint8 can fit into one 256-bit slot, saving gas.Step 2: Compare storage strategies
Separating variables wastes slots; dynamic arrays add overhead; calldata usage unrelated here.Final Answer:
Pack multiple uint8 variables into a single uint256 storage slot -> Option CQuick Check:
Storage packing reduces gas by slot sharing [OK]
Hint: Pack small vars into one slot to save gas [OK]
Common Mistakes:
- Using separate slots wastes gas
- Overusing dynamic arrays increases cost
- Ignoring calldata benefits in storage
