Storage gaps in libraries and structs

According to Solidity storage layout, "The elements of structs and arrays are stored after each other, just as if they were given as individual values."

So if the struct is used as a state variable, then you should use gaps.

If the struct is stored in a mapping, mapping values are stored in different slots so there is no need for gaps (because they are not contiguous with other values). But you do need to ensure that new struct elements (for an upgrade) come after previous struct elements (see Upgrades with Peace of Mind: "Structs" Edition for an example).

1 Like