Skip to content

Commit

Permalink
Moved file out of src
Browse files Browse the repository at this point in the history
  • Loading branch information
aajj999 committed May 29, 2024
1 parent 61c0578 commit 835bd66
Showing 1 changed file with 7 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,24 @@ Contract storage is divided into 2<sup>256</sup> **slots** - 32 bytes each. Such

### Statically-sized values

Statically-sized values are stored one by one in the order of their initialization. If two or more consecutive variables can fit into one slot, they are packed there together. If a variable can't fit with the previous variable, it is stored in the next slot. Variables that appear after an array or structure variables, always start a new slot. Similarly arrays and structures also always start a new storage slot.
Statically-sized values are stored one by one, starting at slot 0, in the order of their initialization in the contract. If two or more consecutive variables can fit into one slot, they are packed there together. If a variable can't fit with the previous variable, it is stored in the next slot. Variables that appear after an array or structure variables, always start a new slot. Similarly arrays and structures also always start a new storage slot.

Variables of a structure are stored just as standalone variables.

### Dynamically-sized values

Dynamically-sized values can't be stored in the same as statically-sized values. Adding new elements to an existing dynamic array would demand values stored in succeeding slots to shift further away.
Dynamically-sized values can't be stored in the same way as statically-sized values. Adding new elements to an existing dynamic array would demand values stored in succeeding slots to shift further away.
Instead, dynamically-sized values are stored at slots chosen by keccak hashing. This is where such an enormous number of storage slots is used. It prevents data from overlapping.
**Slot position `p`** - the starting position of a structure that is calculated based on previously appearing in contract values - is used to calculate further the positions of data.

#### Dynamic arrays

In case of dynamic arrays, position `p` contains the length of the array. Position of the first element is calculated by `keccak256(p)`. The next elements are stored just as statically-sized array's elements - one
In case of dynamic arrays, position `p` contains the length of the array. Position of the first element is calculated by `keccak256(p)`. The next elements are stored just as statically-sized array's elements - one after another.

#### Mapping

Values of mapping are stored at slots: `keccak256(h(k).p)`, where `k` is a key in mapping and `h` is a function as follows: in case of value types `h` is a padding to 32 bytes, in case of strings and byte arrays `h(k) = k`.

## Employment

Understanding how contract storage works enables developer to make gas-efficient contracts.
Expand All @@ -41,7 +43,7 @@ contract Contract{
}
```

If they are initialized in this order, variable `b` wouldn't fit into the same slot as `a` (because `b` takes up all 32 bytes) and `c` also wouldn't fit with `b`. Because of that contract will use three slots.
If variables are initialized in this order, variable `b` wouldn't fit into the same slot as `a` (because `b` takes up all 32 bytes) and `c` also wouldn't fit with `b`. Because of that contract will use three slots.

If the order is changed:

Expand All @@ -53,7 +55,7 @@ contract Contract {
}
```

Now variables `a` and `c` can be packed into one slot and memory is saved since this contract uses only three slots/
Now variables `a` and `c` can be packed into one slot and memory is saved since this contract uses only three slots.

### Efficient variables reading

Expand Down

0 comments on commit 835bd66

Please sign in to comment.