Packing variables for gas savings in Blockchain / Solidity - Time & Space Complexity
When writing smart contracts, saving gas means saving money. Packing variables helps reduce storage cost.
We want to see how packing affects the number of operations as data size grows.
Analyze the time complexity of this Solidity snippet packing variables:
contract Packed {
uint128 a;
uint128 b;
uint256 c;
function setValues(uint128 _a, uint128 _b, uint256 _c) public {
a = _a;
b = _b;
c = _c;
}
}
This code packs two 128-bit variables into one 256-bit storage slot, then sets values.
Look for repeated actions that cost gas.
- Primary operation: Writing to storage slots.
- How many times: Three assignments happen, but two fit in one slot.
As you add more variables, storage writes increase.
| Input Size (variables) | Approx. Storage Writes |
|---|---|
| 2 (packed) | 1 |
| 3 (unpacked) | 3 |
| 10 (packed) | 5 |
Pattern observation: Packing reduces the number of storage writes roughly by grouping variables.
Time Complexity: O(n)
This means the number of storage writes grows linearly with the number of variables, but packing reduces the constant factor.
[X] Wrong: "Packing variables changes the time complexity class."
[OK] Correct: Packing only reduces how many times you write, but the growth still depends on the number of variables.
Understanding how packing affects gas cost shows you know how to write efficient smart contracts, a valuable skill in blockchain development.
"What if we added dynamic arrays instead of fixed variables? How would the time complexity change?"
Practice
What is the main benefit of packing variables in blockchain smart contracts?
Solution
Step 1: Understand storage slots in blockchain
Each storage slot costs gas. Packing variables reduces the number of slots used.Step 2: Effect of packing variables
By grouping smaller variables into one slot, gas usage decreases because fewer slots are needed.Final Answer:
It reduces gas costs by using fewer storage slots -> Option BQuick Check:
Packing = Less gas [OK]
- Thinking packing speeds up CPU execution
- Believing packing increases storage size
- Assuming packing improves security automatically
Which of the following Solidity variable declarations correctly packs two uint8 variables into one storage slot?
contract Example {
uint8 a;
uint8 b;
}Solution
Step 1: Check variable types for packing
Variables of the same small type declared consecutively can be packed into one slot.Step 2: Analyze given declarations
Bothaandbareuint8, so they fit together in one 256-bit slot.Final Answer:
Bothaandbare declared consecutively asuint8-> Option AQuick Check:
Same small types together = packed [OK]
- Mixing large and small types breaks packing
- Declaring variables far apart prevents packing
- Assuming different types pack automatically
Consider this Solidity contract snippet:
contract Test {
uint128 a = 1;
uint64 b = 2;
uint64 c = 3;
}How many storage slots does this contract use for these variables?
Solution
Step 1: Calculate sizes of variables
ais 128 bits,bandcare 64 bits each.Step 2: Pack variables into 256-bit slots
a(128 bits) starts the slot,bandc(64+64=128 bits) fit exactly in the remaining space of the same slot.Final Answer:
1 storage slot -> Option AQuick Check:
128 + 64 + 64 = 256 bits = 1 slot [OK]
- Counting each variable as one slot
- Ignoring packing into remaining space after uint128
- Assuming different sizes cannot pack together
Identify the error in this Solidity contract that tries to pack variables for gas savings:
contract Faulty {
uint256 a;
uint8 b;
uint256 d;
uint8 c;
}Solution
Step 1: Understand packing order
Packing works best when small variables are declared consecutively without large variables interrupting.Step 2: Analyze variable order
bandcare small but separated by largeaandd, so they cannot be packed together.Final Answer:
Variablesbandccannot be packed becausedbreaks the sequence -> Option DQuick Check:
Interrupting large vars break packing [OK]
- Assuming all small vars pack regardless of order
- Changing types unnecessarily
- Thinking order does not affect packing
You want to optimize this Solidity struct for gas savings by packing variables:
struct Data {
uint8 flag;
uint256 id;
uint16 count;
uint8 status;
}Which rearrangement packs variables best into the fewest storage slots?
Solution
Step 1: Order variables from smallest to largest
Place small variables (uint8,uint16) together first to pack tightly.Step 2: Place largest variable last
uint256 idis largest and should be last to avoid breaking packing of smaller vars.Final Answer:
struct Data { uint8 flag; uint8 status; uint16 count; uint256 id; } -> Option CQuick Check:
Small vars first, large last = best packing [OK]
- Placing large vars before small ones
- Mixing variable sizes without order
- Ignoring packing benefits in structs
Ready to prove it?
Pass the test and this pattern moves unknown
