Zokyo Gas Savings
  • ⛽Zokyo Gas Savings
  • 📚Tutorials
    • ✔️Gas Saving Technique 1: Unchecked Arithmetic
    • ⛓️Gas Saving Technique 2: Immutable Variable
    • ✨Gas Saving Technique 3: Double star ** inefficiency
    • 💰Gas Saving Technique 4: Cache Array Length
    • ⬅️Gas Saving Technique 5: ++i costs less gas compared to i++
    • ⚖️Gas Saving Technique 6: NOT operator ! cheaper than boolean FALSE
    • 🪡Gas Saving Technique 7: Using Short Reason Strings
    • 🪵Gas Saving Technique 8: Use Custom Errors instead of Revert Strings to save Gas
    • ✒️Gas Saving Technique 9: Use Custom Errors instead of Revert Strings to save Gas
    • 👾Gas Saving Technique 10: Calldata cheaper than memory
    • ⛔Gas Saving Technique 11: > 0 is less efficient than != 0 for unsigned integers
    • ➗Gas Saving Technique 12: SafeMath no longer needed
    • 😮Gas Saving Technique 13: variables default to 0
    • 🧱Gas Saving Technique 14: struct layout/ variable packing
    • 📞Gas Saving Technique 15: Cache External Call
    • ✍️Gas Saving Technique 16: Early Validation before external call
    • 😎Gas Saving Technique 17: Don’t cache value that is used once
    • 😧Gas Saving Technique 18: Redundant code
    • ✅Gas Saving Technique 19: Early Validation before external call
    • ⛏️Gas Saving Technique 20: Storage vs Memory read optimizations
    • ✒️Gas Saving Technique 21: Unneeded If statements
    • 🌗Gas Saving Technique 22: >= is cheaper than >
    • 🎒Gas Saving Technique 23: Public to private constants
    • ⏹️Gas Saving Technique 24: Make unchanged variables constant/immutable
    • ⏱️Gas Saving Techniques 25: Redundant Access Control Checks
    • ➡️Gas Saving Technique 26: Shift Right instead of Dividing by 2
    • 🪃Gas Saving Tutorial 27: Efficient Boolean Comparison
    • 🤝Gas Saving Technique 28: && operator uses more gas
    • 👓Gas Saving Technique 29: x = x + y is cheaper than x += y
    • 👂Gas Saving Technique 30: Using 1 and 2 rather than 0 and 1 saves gas
    • ⚽Gas Saving Technique 31: Optimize Storage by Avoiding Booleans
    • 🔙Gas Saving Technique 32: Optimal Use of Named Return Variables in Solidity
    • 🛢️Gas Saving Technique 33: Making Functions Payable for Optimized Gas Costs
    • ✍️Gas Saving Technique 34: Optimizing Storage References in Smart Contracts
    • ⛰️Gas Saving Technique 35: Usage of uints/ints smaller than 32 bytes (256 bits) incurs overhead
    • 🌪️Gas Saving Technique 36: Inlining Single Use Internal Functions for Savings
    • ☄️Gas Saving Technique 37: Switching from Public to External Functions for Savings
    • 🎆Gas Saving Technique 38: Upgrading Solidity Compiler to Improve Gas Efficiency and Security
    • 🕶️Gas Saving Technique 39: Avoiding Duplicated Code for Gas Savings
    • 😄Gas Saving Technique 40: Removal of Unused Internal Functions for Gas Savings
    • 🖋️Gas Saving Tutorial 41: In-lining Single Use Modifiers For Gas Saving
    • ⛏️Gas Saving Technique 42: `require` vs`assert`
Powered by GitBook
On this page
  1. Tutorials

Gas Saving Technique 17: Don’t cache value that is used once

PreviousGas Saving Technique 16: Early Validation before external callNextGas Saving Technique 18: Redundant code

Last updated 1 year ago

Introduction

Efficient gas usage is a pivotal consideration for smart contract developers on Ethereum. Sometimes developers cache values into variables for later use, but this might not always be necessary, and it might even result in increased gas consumption. This tutorial will highlight the importance of avoiding unnecessary variable caching, particularly when a value is used only once.

Impact & Details

Unnecessary Variable Caching

  • Increased Gas Costs: Creating a variable to store data that is used only once might lead to increased gas costs without any improvement in readability or functionality.

  • Avoid Redundant Caching: Be mindful of unnecessary variable assignments, especially when dealing with struct attributes or mappings. Directly accessing values without caching is more gas-efficient when the value is used only once.

Example: Avoiding Unnecessary Variable Caching

Here’s an example demonstrating unnecessary variable caching and its optimized version:

Before Optimization:

solidityCopy code// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

contract MyContract {
    struct Claim {
        uint256 updated;
        // ... other attributes ...
    }

    mapping(bytes32 => Claim) public claims;

    function someFunction(bytes32 claimIdentifier) public {
        Claim storage claim = claims[claimIdentifier];
        uint256 timestamp = claim.updated;  // Unnecessary variable caching
        // timestamp is used only once in the function
        // ... rest of the function ...
    }
}

After Optimization:

solidityCopy code// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

contract MyContract {
    struct Claim {
        uint256 updated;
        // ... other attributes ...
    }

    mapping(bytes32 => Claim) public claims;

    function someFunction(bytes32 claimIdentifier) public {
        uint256 timestamp = claims[claimIdentifier].updated;  // Directly accessing value
        // ... rest of the function ...
    }
}

Recommended Mitigation Steps

  1. Review Your Smart Contract: Inspect your contract for instances where values are unnecessarily cached into variables.

  2. Directly Access Values: If a cached value is used only once, consider accessing it directly instead of storing it in a variable.

  3. Test: After making changes, test the contract rigorously to ensure it maintains functionality while utilizing less gas.

Conclusion

Avoiding unnecessary variable caching is a straightforward yet effective technique for gas optimization in smart contracts. For values used only once, direct access is preferable over variable caching. After implementing these changes, always conduct thorough testing to confirm the contract’s functionality and efficiency in gas usage.

📚
😎
Book an audit with Zokyo