As a blockchain developer, optimizing gas usage is critical to ensuring the efficiency and cost-effectiveness of your contracts on the Ethereum network. In this post, I'll be sharing five tips to help you improve gas utilization in your Solidity smart contracts. We'll cover key techniques for reducing gas costs and improving performance. Whether you're a seasoned developer or just getting started with Solidity, these tips will help you write more efficient and sustainable smart contracts. Let's get started!
Using 'calldata' Instead of 'memory' for Function Parameters
calldata for function parameters is generally more efficient than using
memory. This is because
calldata is a read-only data location that is optimized for gas usage, whereas
memory is a read-write data location that is more expensive in terms of gas.
When you use
calldata for function parameters, you are essentially telling Solidity that the data passed to the function is read-only and does not need to be modified. This allows Solidity to optimize gas usage by avoiding unnecessary copying of the data. In contrast, when you use
memory for function parameters, Solidity must allocate additional gas to copy the data from
memory, and then deallocate it once the function call is complete.
Load State Variables to Memory
Loading state variables to memory in a Solidity smart contract can improve performance by reducing gas usage. This technique involves copying state variables from storage to memory before performing operations on them, and then copying them back to storage once the operations are complete. By using memory instead of storage directly, you can avoid expensive storage reads and writes and reduce the overall gas cost of the contract. However, this technique should be used with caution, as it can increase the complexity of the contract and introduce potential security vulnerabilities if not implemented correctly.
++i instead of
When you use
++i, the value of
i is incremented before it is used in an operation. This means that there is no need to create a temporary copy of the value in memory, which can result in gas savings. In contrast, when you use
i++, the value of
i is incremented after it is used in an operation, which requires creating a temporary copy of the value in memory.
While the gas savings from using
i++ may be small, they can add up over time and contribute to the overall efficiency of the contract.
Cache array/struct lookups
When you perform an array or struct lookup in Solidity, the data is typically read from storage each time it is accessed. This can be expensive in terms of gas usage, especially when the data is accessed multiple times within the same function or across multiple function calls.
unchecked When No Overflow Safety is Required
By default, Solidity checks for integer overflow and underflow, which can result in higher gas costs for certain operations. When you use the
unchecked keyword, Solidity skips these checks, which can result in gas savings for operations that don't require overflow safety.
Result When All Optimisations are Used
By using all the optimization tricks above, we saved 4151 units of gas. The example we used is a very simple example and when used in actual applications can save a significant amount in gas costs.
Thank you for reading my blog. If you wish to connect with me, you can find my socials below