โœ๏ธGas Saving Technique 34: Optimizing Storage References in Smart Contracts

In Solidity, every read operation from storage costs gas. Repeatedly accessing the same storage location can quickly rack up costs, especially within loops or large functions. By reusing storage references, developers can reduce these costs and improve the efficiency of their smart contracts.


Concept:

  1. Storage Access Costs: Each time data is fetched from storage, it costs gas. This is because storage on the Ethereum blockchain is expensive due to its persistent nature.

  2. Reusing Storage References: By fetching data once and saving its reference, you can reduce subsequent gas costs. This is especially useful for arrays or mappings where specific elements are accessed multiple times.


Examples & Recommendations:

1. NFTVault.showPosition(): Problem: Repeatedly calling positions[_nftIndex] in a function. Solution: Save its reference and use it.

Before Optimization:

solidityCopy codeuint256 debtPrincipal = positions[_nftIndex].debtPrincipal;
uint256 liquidatedAt = positions[_nftIndex].liquidatedAt;
//... other references to positions[_nftIndex] ...

After Optimization:

solidityCopy codePosition storage _position = positions[_nftIndex];
uint256 debtPrincipal = _position.debtPrincipal;
uint256 liquidatedAt = _position.liquidatedAt;
//... other references using _position ...

2. StakedCitadelVester.claimableBalance(): Problem: Repeatedly calling vesting[recipient] in a function. Solution: Save its reference and use it.

Before Optimization:

solidityCopy codeuint256 locked = vesting[recipient].lockedAmounts;
uint256 claimed = vesting[recipient].claimedAmounts;
//... other references to vesting[recipient] ...

After Optimization:

solidityCopy codeVestingParams storage _vestingParams = vesting[recipient];
uint256 locked = _vestingParams.lockedAmounts;
uint256 claimed = _vestingParams.claimedAmounts;
//... other references using _vestingParams ...

Step-by-Step Guide for Implementing the Storage Reference Optimization:

  1. Identification: Review your contract functions, especially ones that have multiple references to a specific element in an array or mapping.

  2. Modification: At the start of the function, fetch the storage reference once and store it in a local storage-typed variable.

  3. Refactoring: Replace all subsequent accesses with the local storage-typed variable.

  4. Testing: Thoroughly test the contract after refactoring to ensure the logic remains intact and observe the gas savings.


Benefits:

  1. Gas Savings: By reducing repeated storage accesses, you minimize the associated gas costs.

  2. Readable Code: Code can be more readable with a well-named storage reference variable, making it clear what data is being used.


Conclusion: By understanding the underlying costs associated with storage access and leveraging storage references efficiently, developers can produce more optimized smart contracts. This approach both minimizes gas costs and can enhance code clarity.

Last updated