noxx Profile picture
Jul 20 12 tweets 3 min read
A quick mental model for understanding storage slot packing in the EVM

🧵
1/ Conceptually storage can be viewed as an astronomically large array

Within each index of the array we can store some data

Indexes start at 0 & go all the way to (2^256)-1

(2^256 is close to the number of atoms in the observable universe)
2/ Let's take this idea of an array and represent it with a set of boxes side by side stretching into the horizon

Each box has a number on it (array index) and holds some items (data) within it
3/ The items within these boxes are lego bricks

The smallest unit of lego is the 1x1 brick (which for us represents 1 byte of data)

Each box can hold up to 32 of these 1x1 bricks
4/ If we want to store bigger bricks we can

The block below is a 2x2 meaning it takes up 4 times as much space as a 1x1

This means we can only fit 8 of these in our box

We can use many different sizes of bricks within a single box to maximise how efficiently we use the space
5/ As we are adding our lego bricks we'll eventually get to a point where the next brick doesn't fit

Maybe the box is full or maybe it's a larger brick and there's only space for part of it

In either case we move on to the next box (array index) and begin filling that
6/ If we have filled the box entirely great we've made efficient use of the space

If we've left space in the previous box you may be thinking we can come back and fill it later

We can't once we move onto the next box we can only place our lego in that box or subsequent boxes
7/ What does not filling a box mean?

It means you might need more boxes (storage) for a set amount lego (data) than necessary

More boxes mean more $$$

If you'd had organised the lego bricks and treated it like a game of Tetris maybe you would have needed fewer boxes
8/ So how do I organise my lego you ask?

You do it via the ordering of your variable declarations in your code

In the image below we declare a uint8 then a uint32

This is the same as saying first I want to put the 1x1 brick in the box next I want the 2x2 brick
9/Let's now go back to the EVM and see what parallels we can draw

- EVM storage has the concept of a slot (lego box / array index)
- A slot can hold 32 bytes of data (32 1x1 bricks)
- Slots can be packed ie can hold multiple variables (lego bricks) if their sizes allow
10/

- We can organise which variables (bricks) will be stored where (boxes) by the order in which they are declared within our code
- This organisation means we may be able to fit more data into a smaller number of slots saving us $$$
11/ There is loads more to learn about storage in the EVM

How dynamic arrays are stored, how mappings are stored etc.

There's a lot I've simplified / glossed over for this example so use it as a way to remember a concept rather than as reference material

• • •

Missing some Tweet in this thread? You can try to force a refresh
 

Keep Current with noxx

noxx Profile picture

Stay in touch and get notified when new unrolls are available from this author!

Read all threads

This Thread may be Removed Anytime!

PDF

Twitter may remove this content at anytime! Save it as PDF for later use!

Try unrolling a thread yourself!

how to unroll video
  1. Follow @ThreadReaderApp to mention us!

  2. From a Twitter thread mention us with a keyword "unroll"
@threadreaderapp unroll

Practice here first or read more on our help page!

More from @noxx3xxon

Mar 6
Web2 dev thinking about making the jump to web3?

You may not know it yet but this decision could be the most important of your working life.

The clock is ticking ⏰.

Here are 8 reasons why 🧵
1. Time

Time, the most precious commodity

Opportunity cost is real & varies with the time period

The opportunity cost of not getting into the internet space in 1998 is not the same as in 2022

This is a moment in time for crypto, a chance to be early

It will not last forever
2. Access

In what industries can you engage with the leading minds via Twitter / Discord / Telegram

You have the opportunity to learn from the best

As the space grows you can expect these figures to become harder and harder to reach

One of the many advantages of being early
Read 9 tweets

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just two indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member ($3/month or $30/year) and get exclusive features!

Become Premium

Don't want to be a Premium member but still want to support us?

Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal

Or Donate anonymously using crypto!

Ethereum

0xfe58350B80634f60Fa6Dc149a72b4DFbc17D341E copy

Bitcoin

3ATGMxNzCUFzxpMCHL5sWSt4DVtS8UqXpi copy

Thank you for your support!

Follow Us on Twitter!

:(