For mappings, the slot stays empty, but it is still needed to ensure that even if there are two mappings next to each other, their content ends up at different storage locations.
The value corresponding to a mapping key k
is located at keccak256(h(k) . p)
where .
is concatenation and h
is a function that is applied to the key depending on its type:
- For value types,
h
pads the value to 32 bytes in the same way as when storing the value in memory. - For strings and byte arrays,
h
computes thekeccak256
hash of the unpadded data.
If the mapping value is a non-value type, the computed slot marks the start of the data.
If the value is of struct type, for example, you have to add an offset corresponding to the struct member to reach the member.
- Mapping -> Slot p
- Stores -> Nothing
Mapping[Key k]
->keccak256(h(k).p)
.
-> Concatenation- h -> Type-specific Function
- Value -> Padded 32 Bytes
- string/byte -> keccak256
Mapping Types, bytes & string, Storage Layout for bytes & string, Storage, Storage Layout, Storage Packing, Storage Layout & Types, Storage Layout & Ordering, Storage Layout for Mappings, Storage Layout & Inheritance, Storage Layout for bytes & string, Storage Layout for Dynamic Arrays, Storage Layout for Mappings & Dynamic Arrays