Exploring grouping storage slots in the database layer
The original article was posted in BNB Chain’s forum. This article adds on a few data points and narratives specific to Ethereum. Enjoy reading!
TL;DR
We rebuilt the complete contract storage of BSC, 7.21 billion slots, in three layouts: one record per slot, one record per 16 neighbouring slots, and one record per 32. Grouping made the table 20% to 22% smaller. It did not make reads or writes faster. The per-slot layout was fastest or tied in every synthetic workload we ran.
The state itself explains most of this. In both Ethereum and BSC, ~88% of 16-slot groups contain exactly one slot. Hence, for most of the state there is nothing to group.
This also bears on Ethereum’s proposed binary tree, EIP-8297, which groups storage into stems of 256 slots. On today’s state, 91% of those stems would hold a single slot.
This post walks through how a typical client stores contract storage, how contracts lay out their slots, what Ethereum and BSC’s slots actually look like, why grouping looks attractive, an analysis of the grouping scheme, and what all this means for EIP-8297.
How contract storage is stored in database
In our experiment here, we used Reth. Other execution clients (e.g. Nethermind and geth) also keep a similar flat key-value store for state reads which is separated from the state trie store.
Just as a quick introduction, Reth keeps current state in MDBX, which is a memory-mapped B+tree. Contract storage is one table. The key is the contract, and each of its slots is a value under that key, stored as the 32-byte slot key followed by the slot’s value and kept sorted by slot key. MDBX calls this a DUPSORT table.
Before version 2.0, Reth kept two tables of this shape. PlainStorageState table uses the address and the slot key as written by the EVM. HashedStorages table uses keccak256(address) and keccak256(slot key), the order the state trie needs. Since version 2.0, new nodes keep only HashedStorages. The trie needs the hashed table in any case, and keeping both meant storing the state twice and running an extra hashing step during sync (reth#21115).
How contract storage is laid out
The EVM gives every contract at most 2^256 slots, and the compiler decides which of them a contract uses. Solidity typically lays storage out like this:
| Declaration | Where the data goes | Neighbouring slots used? |
|---|---|---|
| State variables | Slots 0, 1, 2, … in declaration order. Small variables share a slot. | Yes |
| Struct, fixed-size array | Consecutive slots from its starting slot | Yes |
Dynamic array at slot p |
Length in p. Elements in consecutive slots from keccak256(p). |
Yes, for the elements |
Mapping at slot p |
The value for key k starts at keccak256(k . p) |
Only if the value spans several slots |
In most EVM chains, a mapping of addresses to balances is the common case, which means every holder’s balance sits alone at a hash-derived position, with no neighbouring slot in use.
A mapping to a struct is different. For example, if each entry is a three-slot struct, then the three slots are consecutive: h, h+1, h+2. Dynamic arrays also produce long runs of consecutive slots.
What BSC’s slots look like
We took the complete state snapshot of BSC at block 115,991,001: 7,214,474,186 non-zero slots in 29,791,378 contracts. We split each contract’s slot space into aligned windows of 16 consecutive positions, slots 0 to 15, 16 to 31 and so on, and call each window that holds at least one slot a group. We then counted how many slots each group holds.
| Occupied slots in the group | Groups | Share of groups | Share of all slots |
|---|---|---|---|
| 1 | 4,193,189,492 | 87.9% | 58.1% |
| 2 to 4 | 351,573,583 | 7.4% | 12.7% |
| 5 to 8 | 126,056,224 | 2.6% | 10.0% |
| 9 to 15 | 45,975,211 | 1.0% | 7.3% |
| 16 (full) | 53,451,980 | 1.1% | 11.9% |
| Total | 4,770,246,490 |
As shown, most of the state is isolated slots. 58% of all slots have no neighbour inside their group, and the average group holds 1.5 slots. The remaining 42% of slots do cluster, at about 5 slots per group, and one slot in eight sits in a completely full group.
Widening the group to 32 positions changes little:
| Occupied slots in the group | Groups | Share of groups | Share of all slots |
|---|---|---|---|
| 1 | 4,162,075,176 | 88.9% | 57.7% |
| 2 to 4 | 323,583,512 | 6.9% | 11.7% |
| 5 to 8 | 125,719,663 | 2.7% | 9.8% |
| 9 to 16 | 31,162,143 | 0.7% | 4.8% |
| 17 to 31 | 16,033,363 | 0.3% | 5.3% |
| 32 (full) | 24,208,955 | 0.5% | 10.7% |
| Total | 4,682,782,812 |
The number of groups falls only from 4.77 billion to 4.68 billion, and 57.7% of slots are still alone.
So looking at the empirical analysis as a whole, 42% of slots share a group with at least one other slot, and 58% do not.
What Ethereum’s slots look like
We rebuilt Ethereum mainnet’s contract storage at block 24,873,999 from every storage write since genesis, as recorded in Xatu’s execution traces, keeping each slot’s last value. That gives 1,558,216,440 non-zero slots in 26,650,417 contracts.
We used the same groups as for BSC: aligned windows of 16 consecutive positions in each contract’s slot space.
| Occupied slots in the group | Groups | Share of groups | Share of all slots |
|---|---|---|---|
| 1 | 1,029,669,416 | 89.3% | 66.1% |
| 2 to 4 | 80,908,007 | 7.0% | 13.4% |
| 5 to 8 | 32,621,392 | 2.8% | 11.0% |
| 9 to 15 | 3,093,403 | 0.3% | 2.2% |
| 16 (full) | 7,144,127 | 0.6% | 7.3% |
| Total | 1,153,436,345 |
Ethereum’s state groups even less than BSC’s. 66% of all slots have no neighbour inside their group, against 58% on BSC, and the average group holds 1.35 slots. The 34% of slots that do cluster average about 4 slots per group, and one slot in fourteen sits in a full group, against one in eight on BSC.
Widening the group to 32 positions changes little here either:
| Occupied slots in the group | Groups | Share of groups | Share of all slots |
|---|---|---|---|
| 1 | 1,022,972,738 | 89.8% | 65.7% |
| 2 to 4 | 73,375,728 | 6.4% | 12.1% |
| 5 to 8 | 36,415,763 | 3.2% | 12.2% |
| 9 to 16 | 2,473,997 | 0.2% | 1.8% |
| 17 to 31 | 977,465 | 0.1% | 1.4% |
| 32 (full) | 3,374,975 | 0.3% | 6.9% |
| Total | 1,139,590,666 |
We also counted the two widths that page-based designs use: 128 slots (MIP-8) and 256 slots (EIP-6800, and EIP-8297 below).
| Group width | Groups | Slots per group | Groups with one slot | Slots alone in their group | Slots in full groups |
|---|---|---|---|---|---|
| 16 | 1,153,436,345 | 1.35 | 89.3% | 66.1% | 7.3% |
| 32 | 1,139,590,666 | 1.37 | 89.8% | 65.7% | 6.9% |
| 128 | 1,128,725,045 | 1.38 | 90.1% | 65.3% | 6.2% |
| 256 | 1,126,710,284 | 1.38 | 90.2% | 65.2% | 5.8% |
Going from 16 positions to 256 removes only 2.3% of the groups. A slot that is alone in a window of 16 is almost always alone in a window of 256 as well.
The isolated slots are the hash-derived ones. Splitting the slots by key size, at 16-slot groups:
| Slot keys | Share of all slots | Slots per group | Slots alone in their group | Slots in full groups |
|---|---|---|---|---|
| Below 2^16 (state variables, fixed structs) | 4.3% | 2.39 | 23.5% | 5.2% |
| Hash-derived (mapping entries, dynamic arrays) | 95.7% | 1.32 | 68.0% | 7.4% |
Slots at small keys cluster as the layout table predicts, but they are a small part of the state. The full groups that do exist are almost all hash-derived: runs of dynamic array elements and multi-slot structs inside mappings.
The grouping idea
As far as I can tell, the concept of “grouping” storage together or implementing a “page-based” storage scheme traces back to EIP-6800, which commits each run of 256 consecutive slots together. Monad is the only chain that I’m aware of that actually implements such a scheme in production: MIP-8 groups 128 slots into a 4 KB page and treats every slot of a loaded page as warm.
The principle is simple. When you read from a storage medium, typically an SSD, you don’t read byte by byte, but by pages. A page is typically 4 KB, depending on the device, the operating system and the database’s own page size. What that means for how storage slots are stored in the physical database is that, even though you read a 32-byte slot, it’s likely that neighbouring slots are fetched together in the same page and kept in memory.
So if a contract were to read consecutive slots, the first read hits the disk (assuming a cache miss), and the subsequent reads come from memory. But the current EVM pricing still charges each of those slots as a cold read.
The same reasoning can be applied inside the database. If related slots are consecutive, storing them in one record could save space, and a transaction that touches several fields of a struct could fetch them with one lookup.
Here’s how we tested the idea with two grouped layouts:
A group covers 16 or 32 consecutive slot positions. The slot key is split into a group number, which is the slot key shifted right by 4 or 5 bits, and an offset within the group.
The bitmap marks which positions are occupied. Empty positions cost nothing, so a group stores only the slots that exist. All three layouts keep DUPSORT and 4,096-byte pages and the same value encoding.
Grouping can only be done with plain keys. Hashing the slot key scatters the neighbours that a group is meant to collect. All three layouts in this experiment therefore use the plain keys (i.e. original addresses and slot keys).
How we measured
We used a BSC Reth snapshot synced to block 115,991,001. It stores only the hashed keys, so we recovered the original addresses and slot keys from historical changesets, which keep the original keys, and took the values from the snapshot’s current state. Before using the result, we recomputed the state root over all 764,607,222 accounts and their storage and matched it against the block header.
Workloads
| Workload | What it does |
|---|---|
| Random read, random update | Uniform draws from all slots |
| Missing-slot read | Reads an empty position next to occupied ones |
| Paired read | Reads the first and last occupied slot of one group |
| Batch update | Updates sorted batches of slots that share groups |
| Insert then delete | Adds new slots, then removes them |
| Singleton read and update | Only slots that are alone in their group of 32 |
| Dense read and update | Only slots in full groups of 32 |
Reads use 65,536 operations per trial. Writes use 8,192 operations and commit durably every 64.
Cache
A cold trial evicts the database files from the page cache before opening them. A warmed trial first replays a separate 65,536-read sample three times, outside the timer. Neither is a fully cached database.
Trials
Each workload and cache mode ran six times, once in each of the six possible layout orders. Each of the 360 trials is a fresh process pinned to one CPU, under a 32 GiB memory limit with no swap. Every layout receives the same operations within a repetition.
Results
Size
| Layout | Table size | Change | Records | Leaf pages | Branch pages |
|---|---|---|---|---|---|
| Per-slot | 401.7GB | 7.21 B | 95.7 M | 2.35 M | |
| Group16 | 312.9GB | -22.1% | 4.77 B | 74.0 M | 2.42 M |
| Group32 | 321.2GB | -20.0% | 4.68 B | 75.5 M | 2.87 M |
Why grouping saves space:
- Every record starts with a 32-byte key. In the per-slot layout each slot carries its own. In a group, all the slots share one group number.
- The values are small next to that key. A quarter of them fit in one byte and 61% fit in eight bytes, so for most slots the key is the larger part of the record.
- Group16 turns 7.21 billion records into 4.77 billion. Each of the 2.44 billion slots that joins an existing record drops its key and the database’s per-record overhead, about 41 bytes together.
- A group that holds a single slot saves nothing. It is 3 bytes larger than the per-slot record, for its bitmap and length. That is 88% of the groups.
- Put together, that arithmetic gives about 86 GB saved, close to the 88.8 GB we measured.
- Group32 is larger than group16. It merges only 87 million more records, and it pays two more bitmap bytes on every one of its 4.68 billion records.
Speed
Times are microseconds per slot operation. Each figure is the median of six trial averages.
| Workload | Cache | Per-slot | Group16 | Group32 |
|---|---|---|---|---|
| Random read | cold | 224.2 | 229.6 | 240.5 |
| Random read | warmed | 182.9 | 185.5 | 192.4 |
| Random update | cold | 420.7 | 433.4 | 456.9 |
| Random update | warmed | 302.6 | 311.7 | 324.0 |
| Paired read | cold | 109.2 | 110.4 | 115.7 |
| Paired read | warmed | 88.1 | 86.3 | 90.3 |
| Batch update | cold | 207.5 | 210.9 | 221.7 |
| Batch update | warmed | 148.4 | 148.3 | 153.8 |
| Dense read | cold | 175.3 | 190.4 | 226.6 |
| Dense read | warmed | 156.8 | 168.2 | 203.4 |
| Dense update | cold | 327.0 | 377.0 | 442.3 |
| Dense update | warmed | 254.4 | 287.3 | 344.1 |
Per-slot is ahead in 10 of the 12 rows, and group32 is behind per-slot in all of them. Group16 leads in two warmed rows, by 2% and by 0.1%.
Why per-slot is still faster on most workloads:
- Locality is the same in all three layouts. With plain keys, the per-slot table already keeps a contract’s neighbouring slots on the same leaf page, about 75 slots to a page. Reading the second field of a struct costs no extra disk fetch in the per-slot layout either. This is why the two workloads built to favour grouping, the paired read and the batch update show no gain.
- Grouped records are larger: up to 562 bytes in group16 and 1,092 in group32, against 64 for a per-slot record. Fewer of them fit on a page of the tree, so the tree has more branch pages to walk, as the size table shows.
- A write to one slot rewrites its whole group. The old record is deleted and the group is written back with the one value changed.
- The gap is widest on the dense workloads, where every group is full and the records are largest. A cold dense update takes 35% longer in group32 than per-slot.
Can Ethereum actually do page-based?
As of writing this article, Ethereum is set to schedule EIP-8297 (Partitioned Binary Trie) as the replacement for MPT in the I* fork. The state tree design itself already includes a page-based scheme.
EIP-8297 hashes the address and the slot’s group number, but keeps the slot’s position inside its group in plain form. A storage key is:
0xFF || H(address) || H(address || slot / 256) || slot % 256
where H is the tree’s key hash. Slots 0 to 63 are the exception, where they sit in the account’s header stem, 0x00 || H(address) || sub_index, next to its balance, nonce and code hash. Account keys are hashed the same way.
So each run of 256 aligned slots shares one stem and its slots are siblings, exactly the 256-slot group counted above. Which stem a group lands on is hash-derived: a contract’s groups all share the H(address) prefix, but within that bucket they are in no particular order.
On Ethereum’s current state, that means:
- Storage stems: 1,101,129,810 stems hold the 1,496,593,914 slots at keys 64 and above, 1.36 slots per stem. 90.9% of these stems hold a single slot, and 66.9% of these slots are the only leaf in their stem. Only 6.0% of the slots sit in a full stem.
- Header stems: 61.6 million slots (4.0% of all slots) have keys below 64 and live in the header stem, next to the account’s own leaves. They belong to 26.0 million contracts, 97.5% of all contracts with storage, at 2.37 slots per contract. So nearly every contract gets a few of its state variables next to its account data.
Conclusion
On BSC’s state, grouping slots saves about 20% of the storage table’s space and does not make it faster. Per-slot was ahead in almost every workload.
From the empirical analysis on both chains, most of the state has nothing to group. 58% of BSC’s slots and 66% of Ethereum’s sit alone in their 16-slot group, and wider groups barely change that. Furthermore, for the slots that do have neighbours, grouping adds no additional locality. With plain keys, a per-slot table already stores neighbouring slots on the same page, so both layouts fetch the same pages. Grouping only changes how large the records are, which is why it only saves space.
The same occupancy limits EIP-8297. Its tree already groups storage into 256-slot stems with plain offsets inside each stem, but on Ethereum’s current state 91% of those stems would hold a single slot.
Put differently, the locality we expect from grouping comes from keeping keys in their original order (i.e. plain keys) and not from packing them into one record.

