Storage layout in Blockchain / Solidity - Time & Space Complexity
When working with blockchain storage, it's important to understand how accessing and organizing data affects speed.
We want to know how the time to read or write data changes as the stored data grows.
Analyze the time complexity of the following storage access pattern.
// Solidity example
contract StorageExample {
uint[] public data;
function addData(uint value) public {
data.push(value); // add value to storage array
}
function readData(uint index) public view returns (uint) {
return data[index]; // read value at index
}
}
This code stores numbers in a list on the blockchain and allows adding and reading by position.
Look at what repeats when using this storage layout.
- Primary operation: Accessing an element by index in the storage array.
- How many times: Each read or write accesses one storage slot directly.
Accessing or adding data takes about the same time no matter how many items are stored.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 1 storage read or write |
| 100 | 1 storage read or write |
| 1000 | 1 storage read or write |
Pattern observation: The time stays about the same regardless of how many items are stored.
Time Complexity: O(1)
This means reading or writing a single item takes a constant amount of time, no matter how big the storage is.
[X] Wrong: "Accessing data in a storage array gets slower as the array grows because it has to look through all items."
[OK] Correct: Each item is stored at a fixed position, so the contract can jump directly to it without checking others.
Understanding storage layout and its time cost helps you design smart contracts that run efficiently and save gas.
"What if we stored data in a linked list instead of an array? How would the time complexity of reading an item change?"
Practice
storage layout in a blockchain smart contract primarily describe?Solution
Step 1: Understand storage layout concept
Storage layout defines how contract variables are arranged and saved in storage slots on the blockchain.Step 2: Differentiate from other concepts
It is not about function order, gas cost, or consensus, but specifically about variable storage.Final Answer:
How contract variables are stored in storage slots -> Option CQuick Check:
Storage layout = variable storage arrangement [OK]
- Confusing storage layout with function execution order
- Thinking storage layout relates to gas costs
- Mixing storage layout with consensus mechanisms
uint256 variable named count stored in contract storage?Solution
Step 1: Recall Solidity variable declaration
In Solidity, storage variables are declared simply by specifying the type and name, e.g.,uint256 count;.Step 2: Identify incorrect syntax
Options A, B, and D use invalid or unsupported syntax for storage variable declaration.Final Answer:
uint256 count; -> Option AQuick Check:
Correct Solidity declaration = uint256 count; [OK]
- Adding 'storage' keyword in variable declaration
- Using 'var' keyword incorrectly
- Assigning 'storage' as a value
contract Example {
uint128 a = 1;
uint128 b = 2;
uint256 c = 3;
}How are the variables
a, b, and c stored in storage slots?Solution
Step 1: Understand slot packing rules
Twouint128variables fit into one 256-bit slot because 128 + 128 = 256 bits.Step 2: Assign slots based on order and size
aandbshare slot 0;cis auint256and occupies slot 1 alone.Final Answer:
aandbshare slot 0;cis in slot 1 -> Option AQuick Check:
Slot packing = smaller vars share slots [OK]
- Assuming each variable always gets its own slot
- Mixing variable order in slots
- Ignoring bit size when packing variables
contract Test {
uint256 x;
bool y;
uint256 z;
}What is the main storage layout issue here?
Solution
Step 1: Analyze variable sizes and order
uint256uses full 256-bit slot;booluses 8 bits but placed between twouint256variables.Step 2: Identify wasted storage
Becausebool yis betweenxandz, it cannot share slots efficiently, wasting space.Final Answer:
Thebool ywastes storage slot space betweenxandz-> Option DQuick Check:
Storage gaps caused by small vars between large vars [OK]
- Thinking variable order causes syntax errors
- Assuming all vars always share slots
- Believing too many slots cause runtime errors
contract Optimize {
uint128 a;
uint256 b;
uint128 c;
bool d;
}Which variable order best minimizes storage slots used?
Solution
Step 1: Group smaller variables to pack slots
Placinguint128 a,uint128 c, andbool dtogether allows packing into fewer slots.Step 2: Place large
uint256 bseparatelyuint256 brequires a full slot, so placing it last avoids gaps.Step 3: Evaluate options
uint128 a; uint128 c; bool d; uint256 b; groups small vars first, then large var last, minimizing slots used.Final Answer:
uint128 a; uint128 c; bool d; uint256 b; -> Option BQuick Check:
Group small vars first, large vars last [OK]
- Scattering small vars causing wasted slots
- Placing large vars before small vars
- Ignoring bool size when packing
Ready to prove it?
Pass the test and this pattern moves unknown
