EVM has three types of data storage locations:
- Storage
- Memory
- Calldata
Each data location has a different purpose and using them appropriately will save us gas and money. Keep in mind that, in some situations, Solidity strictly enforces a default location.
Storage
When placed in storage, a variable is written on the blockchain. Everything that is on the chain, stays there. Every contract has its own storage, so these variables are persistent. To explain simply, the variables stored in storage will have a lifetime scope of a contract. Its value is the same for all calls to the contract.
Memory
Variables stored in memory are declared inside a function. They are temporary and their ‘lifetime’ is dependent on the runtime of the function they correspond to. They are only accessible inside that method. Their purpose is to assist local logic inside function blocks.
Calldata
Calldata is also a temporary data location in Solidity. It acts like memory, in terms of its block scope. The variables stored in calldata are only available inside the function they were declared in.
Now, it’s obvious from the above definitions that changing storage data costs a lot more gas than changing memory or calldata variables. So we should minimize the number of times we are modifying the storage data. Especially in the case of loops.
The following solidity code will help you understand the difference between a poor code and better – optimized code.
Want some proof? Here we go:
Call for cheaperFunction():
Call for expensiveFunction():
Unlike first tips and tricks article, this solidity tip is fairly simple however, can make your contracts pretty expensive in terms of interactions. Do follow on twitter or linkedIn for more such informative and hopefully not so boring content.
Leave a Reply