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 37: Switching from Public to External Functions for Savings

PreviousGas Saving Technique 36: Inlining Single Use Internal Functions for SavingsNextGas Saving Technique 38: Upgrading Solidity Compiler to Improve Gas Efficiency and Security

Last updated 1 year ago

Introduction: Solidity offers various function visibility specifiers, including public and external. While both can be accessed from outside the contract, switching from public to external in certain scenarios can save gas and lead to more efficient smart contracts.


Concept:

  1. Function Visibility:

    • public: These functions can be called both externally and internally (from within the contract or derived contracts). When used externally, arguments are passed in calldata, and when used internally, arguments are passed in memory.

    • external: These functions can only be called from other contracts and transactions. Arguments are always in calldata, which is a read-only, low-level data area.

  2. Gas Efficiency: Calling an external function is typically more gas-efficient than a public function when called externally because external functions can skip one step of copying argument data (from calldata to memory).


Examples & Recommendations:

Problem: Having a public function that's never called from within the contract. Solution: Change its visibility to external.

Non-Optimized Approach:

solidityCopy codecontract Example {
    function setData(uint256 data) public {
        // function body
    }
}

Optimized Approach:

solidityCopy codecontract Example {
    function setData(uint256 data) external {
        // function body
    }
}

Step-by-Step Guide for Switching to External Functions:

  1. Review: Identify public functions in your contract that are never called internally.

  2. Change Visibility: For functions identified in step 1, change their visibility from public to external.

  3. Test: After making the change, ensure that the contract's functionality remains consistent. The function should still be callable from external contracts and transactions.


Benefits:

  1. Gas Savings: external functions use less gas than public functions when called externally because of reduced data copying.

  2. Code Clarity: Using external visibility clearly indicates that a function is not meant to be called internally, leading to improved code semantics and readability.


Considerations:

  1. Internal Calls: If a function is or will be used both externally and internally, it should remain public.

  2. Proxy Contracts: If using proxy patterns, ensure the visibility of functions aligns with intended usage across different contract versions.


Statement Verification: Is the statement "External call cost is less expensive than of public functions" true?

Yes, the statement is generally true when referring to calls made externally to the contract. As mentioned, external functions can leverage calldata directly, avoiding the need for data to be copied to memory, which incurs additional gas.


Conclusion: Switching appropriate public functions to external can lead to gas savings and clearer code. However, always consider the function's intended use case before making such changes to ensure functionality remains consistent.

๐Ÿ“š
โ˜„๏ธ
Book an audit with Zokyo