Packing variables helps save gas by storing multiple small values together in one storage slot. This reduces the cost of storing data on the blockchain.
Packing variables for gas savings in Blockchain / Solidity
contract Example {
// Variables packed into one 32-byte slot
uint8 a;
uint8 b;
uint16 c;
bool d;
}Variables of smaller size (like uint8, bool) can be packed together if declared consecutively.
Order matters: place smaller variables together to fit into one 32-byte storage slot.
contract Packed {
uint8 a;
uint8 b;
uint16 c;
bool d;
}contract NotPacked {
uint256 a;
uint8 b;
bool c;
}struct Data {
uint128 x;
uint128 y;
uint8 z;
bool flag;
}This contract packs four small variables into one storage slot to save gas. The getValues function returns all values.
pragma solidity ^0.8.0; contract GasSaver { // Packed variables uint8 public a = 1; uint8 public b = 2; uint16 public c = 300; bool public d = true; // Function to get all values function getValues() public view returns (uint8, uint8, uint16, bool) { return (a, b, c, d); } }
Always declare smaller variables together to maximize packing.
Changing variable order can reduce or increase gas costs.
Use tools like Remix or Hardhat to check gas usage before and after packing.
Packing variables reduces gas by storing multiple small variables in one slot.
Order and size of variables affect packing efficiency.
Good packing leads to cheaper smart contract deployment and transactions.
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
