Bird
Raised Fist0
Blockchain / Solidityprogramming~10 mins

Packing variables for gas savings in Blockchain / Solidity - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Packing variables for gas savings
Declare variables
↓
Check variable sizes
↓
Group small variables
↓
Pack into single storage slot
↓
Deploy contract
↓
Gas cost reduced
This flow shows how small variables are grouped and packed into one storage slot to save gas in blockchain contracts.
Execution Sample
Blockchain / Solidity
contract Example {
  uint128 a;
  uint128 b;
  uint256 c;
}
This Solidity contract declares two 128-bit variables packed into one 256-bit slot, saving gas.
Execution Table
StepVariable DeclaredSize (bits)Storage Slot UsedGas Cost Impact
1uint128 a128Slot 0 (first half)Base cost for slot
2uint128 b128Slot 0 (second half)No extra slot, saves gas
3uint256 c256Slot 1New slot, normal cost
4End--Total gas saved by packing a and b
💡 All variables declared and packed; gas savings realized by combining smaller variables into one slot.
Variable Tracker
VariableStartAfter Declaring aAfter Declaring bAfter Declaring cFinal
aundefinedstored in slot 0 (first 128 bits)stored in slot 0 (first 128 bits)stored in slot 0 (first 128 bits)stored in slot 0 (first 128 bits)
bundefinedundefinedstored in slot 0 (second 128 bits)stored in slot 0 (second 128 bits)stored in slot 0 (second 128 bits)
cundefinedundefinedundefinedstored in slot 1 (256 bits)stored in slot 1 (256 bits)
Key Moments - 2 Insights
Why do uint128 variables a and b share the same storage slot?
Because each uint128 uses 128 bits and two of them fit exactly into one 256-bit storage slot, saving gas by avoiding extra slots. See execution_table rows 1 and 2.
What happens if a variable is larger than 256 bits?
It cannot fit in one slot and will use multiple slots, increasing gas cost. This example uses uint256 max size per slot as shown in row 3.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, which storage slot does variable b use?
ASlot 1
BSlot 0 (second half)
CSlot 0 (first half)
DSlot 2
💡 Hint
Check execution_table row 2 under 'Storage Slot Used'
At which step does a new storage slot get used?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look at execution_table rows 1-3 and see when slot changes
If variable b was uint256 instead of uint128, how would gas cost change?
AGas cost would increase, using a new slot
BGas cost would decrease
CNo change, still one slot
DVariables would pack into half a slot
💡 Hint
Refer to variable sizes and slot usage in execution_table rows 2 and 3
Concept Snapshot
Packing variables means combining smaller variables into one 256-bit storage slot.
Use types like uint128 to fit two variables per slot.
This reduces the number of storage slots used.
Fewer slots mean less gas cost on blockchain.
Always check variable sizes to pack efficiently.
Full Transcript
This visual execution shows how packing variables in blockchain contracts saves gas. We start by declaring variables a and b as uint128, each 128 bits. Since one storage slot is 256 bits, a and b share slot 0, with a in the first half and b in the second half. Variable c is uint256 and uses slot 1 alone. The execution table tracks each step, showing variable sizes, slots used, and gas cost impact. The variable tracker shows how variables occupy slots after each declaration. Key moments clarify why packing saves gas and what happens with larger variables. The quiz tests understanding of slot usage and gas cost changes. Packing variables efficiently reduces blockchain gas fees by minimizing storage slots used.

Practice

(1/5)
1.

What is the main benefit of packing variables in blockchain smart contracts?

easy
A. It makes the code run faster on the CPU
B. It reduces gas costs by using fewer storage slots
C. It increases the contract's storage size
D. It improves the contract's security automatically

Solution

  1. Step 1: Understand storage slots in blockchain

    Each storage slot costs gas. Packing variables reduces the number of slots used.
  2. Step 2: Effect of packing variables

    By grouping smaller variables into one slot, gas usage decreases because fewer slots are needed.
  3. Final Answer:

    It reduces gas costs by using fewer storage slots -> Option B
  4. Quick Check:

    Packing = Less gas [OK]
Hint: Packing saves gas by reducing storage slots used [OK]
Common Mistakes:
  • Thinking packing speeds up CPU execution
  • Believing packing increases storage size
  • Assuming packing improves security automatically
2.

Which of the following Solidity variable declarations correctly packs two uint8 variables into one storage slot?

contract Example {
    uint8 a;
    uint8 b;
}
easy
A. Both a and b are declared consecutively as uint8
B. a is uint8 and b is uint256
C. a is uint256 and b is uint8
D. Both a and b are declared as uint256

Solution

  1. Step 1: Check variable types for packing

    Variables of the same small type declared consecutively can be packed into one slot.
  2. Step 2: Analyze given declarations

    Both a and b are uint8, so they fit together in one 256-bit slot.
  3. Final Answer:

    Both a and b are declared consecutively as uint8 -> Option A
  4. Quick Check:

    Same small types together = packed [OK]
Hint: Use same small types consecutively to pack [OK]
Common Mistakes:
  • Mixing large and small types breaks packing
  • Declaring variables far apart prevents packing
  • Assuming different types pack automatically
3.

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?

medium
A. 1 storage slot
B. 3 storage slots
C. 2 storage slots
D. 4 storage slots

Solution

  1. Step 1: Calculate sizes of variables

    a is 128 bits, b and c are 64 bits each.
  2. Step 2: Pack variables into 256-bit slots

    a (128 bits) starts the slot, b and c (64+64=128 bits) fit exactly in the remaining space of the same slot.
  3. Final Answer:

    1 storage slot -> Option A
  4. Quick Check:

    128 + 64 + 64 = 256 bits = 1 slot [OK]
Hint: Sum consecutive vars' bits: 128+64+64=256 -> 1 slot [OK]
Common Mistakes:
  • Counting each variable as one slot
  • Ignoring packing into remaining space after uint128
  • Assuming different sizes cannot pack together
4.

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;
}
medium
A. Variables b and c must be declared before a
B. Variables a and d should be uint8 to pack
C. All variables are packed correctly without error
D. Variables b and c cannot be packed because d breaks the sequence

Solution

  1. Step 1: Understand packing order

    Packing works best when small variables are declared consecutively without large variables interrupting.
  2. Step 2: Analyze variable order

    b and c are small but separated by large a and d, so they cannot be packed together.
  3. Final Answer:

    Variables b and c cannot be packed because d breaks the sequence -> Option D
  4. Quick Check:

    Interrupting large vars break packing [OK]
Hint: Keep small vars together without large vars interrupting [OK]
Common Mistakes:
  • Assuming all small vars pack regardless of order
  • Changing types unnecessarily
  • Thinking order does not affect packing
5.

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?

hard
A.
struct Data {
    uint8 flag;
    uint256 id;
    uint16 count;
    uint8 status;
}
B.
struct Data {
    uint16 count;
    uint256 id;
    uint8 flag;
    uint8 status;
}
C.
struct Data {
    uint8 flag;
    uint8 status;
    uint16 count;
    uint256 id;
}
D.
struct Data {
    uint8 flag;
    uint16 count;
    uint256 id;
    uint8 status;
}

Solution

  1. Step 1: Order variables from smallest to largest

    Place small variables (uint8, uint16) together first to pack tightly.
  2. Step 2: Place largest variable last

    uint256 id is largest and should be last to avoid breaking packing of smaller vars.
  3. Final Answer:

    struct Data { uint8 flag; uint8 status; uint16 count; uint256 id; } -> Option C
  4. Quick Check:

    Small vars first, large last = best packing [OK]
Hint: Put small vars first, big vars last for best packing [OK]
Common Mistakes:
  • Placing large vars before small ones
  • Mixing variable sizes without order
  • Ignoring packing benefits in structs
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard