0
0
Blockchain / Solidityprogramming~10 mins

Gas optimization with storage in Blockchain / Solidity - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Gas optimization with storage
Start Transaction
Check Storage Usage
Is Storage Write Needed?
NoSkip Write
Yes
Write to Storage (Expensive)
Use Memory/Stack if Possible (Cheaper)
Minimize Storage Writes
End Transaction
This flow shows how to decide when to write to storage to save gas by minimizing expensive storage operations.
Execution Sample
Blockchain / Solidity
uint256 storageVar;

function update() public {
  uint256 x = 5;
  uint256 y;
  y = x + 10; // memory operation
  storageVar = y; // storage write
}
This code updates a storage variable by first calculating in memory, then writing once to storage to save gas.
Execution Table
StepOperationVariableValueGas CostNote
1Initialize xx50x is in memory, no gas cost
2Declare yy00y is uninitialized
3Calculate y = x + 10y15LowCalculation in memory, cheap
4Write y to storageVarstorageVar15HighStorage write is expensive
5End function--0Transaction ends
💡 Function ends after one storage write to minimize gas
Variable Tracker
VariableStartAfter Step 3After Step 4Final
x5555
y0151515
storageVar001515
Key Moments - 2 Insights
Why do we calculate y in memory before writing to storageVar?
Calculating y in memory (Step 3) is cheaper than multiple storage writes. Writing once to storage (Step 4) saves gas by minimizing expensive storage operations.
What makes storage writes expensive compared to memory operations?
Storage writes (Step 4) cost more gas because they change blockchain state permanently, unlike memory calculations (Step 3) which are temporary and cheaper.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of y after Step 3?
A15
B5
C0
D10
💡 Hint
Check the 'Value' column for y at Step 3 in the execution table
At which step does the expensive storage write happen?
AStep 2
BStep 3
CStep 4
DStep 5
💡 Hint
Look for 'Storage write is expensive' note in the execution table
If we wrote to storageVar twice instead of once, how would gas cost change?
AGas cost would stay the same
BGas cost would increase
CGas cost would decrease
DGas cost would be zero
💡 Hint
Refer to the 'Gas Cost' column and note that storage writes are expensive
Concept Snapshot
Gas optimization with storage:
- Storage writes cost high gas
- Use memory/stack for calculations
- Write to storage only once when needed
- Minimize storage writes to save gas
- Calculate values in memory before storage update
Full Transcript
This visual trace shows how gas optimization works by minimizing storage writes. First, variables are initialized in memory with no gas cost. Calculations happen in memory, which is cheap. Then, only one storage write is done to update the blockchain state, which is expensive. By reducing storage writes, the contract saves gas. The variable tracker shows values changing step-by-step. Key moments explain why memory is preferred for calculations and why storage writes cost more. The quiz tests understanding of variable values and gas costs at each step.