Memory Vs Storage Vs Calldata

  • Memory – Used to store temporary function variables, that aren’t permentantly stored. They are defined within the scope of a function and only persist once a function is called.
  • Calldata – Immutable, temporary location where function parameters are stored. Similar behavior like memory. Then what is the difference? The difference itself is in the way the Solidity Compiler treats them, for calldata types, it avoids unnecessary copies and ensures that data is unaltered by requiring it to be immutable.
  • Storage – Persistant, mutable data type. Usually where all state variables are stored. Because state variables are required to be changed, they have to be mutable and persistant throughtout.

Solidity compiler allocates at max 32 bytes per data slot, in the case of dynamic arrays and objects, it automatically assumes that they will fill in the whole 32 bytes of data per slot.

If the data type is less than 32 bytes, it will share the data slot with other forms of data to improve efficiency – where is dynamic arrays are automatically assigned the whole slot.

Both memory and storage, have various considerations in terms of memory that enable them to copy themselves throughout the blockchain data slots – whereis calldata, does not copy itself. For that reason, it is best to understand these considerations when writing gas efficient code – to avoid unnecessary gas expense.

Scroll to Top
Skip to content