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 Tutorial 41: In-lining Single Use Modifiers For Gas Saving

PreviousGas Saving Technique 40: Removal of Unused Internal Functions for Gas SavingsNextGas Saving Technique 42: `require` vs`assert`

Last updated 1 year ago

Introduction: Modifiers in Solidity are powerful tools that provide a way to add pre-conditions to function executions. However, while they can make code more readable and modular, their use isn't always the most gas-efficient approach, especially when the modifier is used only once. In such scenarios, directly incorporating the modifier's logic into the function can save on gas costs. In this tutorial, we will discuss the concept of inlining modifiers and how it can help optimize gas consumption.


Concept: When a modifier is invoked, there's an associated overhead due to the added layer of execution. In cases where a modifier is used multiple times throughout a contract, this overhead can be justified by the enhanced readability and code reusability. However, when a modifier is used only once, the gas overhead becomes unnecessary, and inlining the modifier's logic directly into the function can result in gas savings.


Underlying Problem:

  1. Extra Overhead: Using a modifier introduces additional gas costs related to its execution.

  2. Redundancy: A modifier used only once doesn't capitalize on its primary advantage of code reusability.


Example:

With a Single-use Modifier:

solidityCopy codemodifier onlyWhitelistedProfileCreator() {
    require(isWhitelistedProfileCreator(msg.sender), "Not a whitelisted profile creator");
    _;
}

function createProfile(DataTypes.CreateProfileData calldata vars) external onlyWhitelistedProfileCreator {
    // Function logic
}

Inlining the Modifier:

solidityCopy codefunction createProfile(DataTypes.CreateProfileData calldata vars) external {
    require(isWhitelistedProfileCreator(msg.sender), "Not a whitelisted profile creator");
    // Function logic
}

Recommendation:

  1. Review your contracts and identify any modifiers that are used only once.

  2. For such single-use modifiers, directly incorporate the modifier's logic into the respective function.

  3. Ensure that the logic's placement within the function doesn't affect the function's behavior or security.

  4. Test the refactored code to guarantee consistent behavior.

  5. Always prioritize gas optimization by assessing the true need for a modifier based on its frequency of use.


Conclusion: While modifiers are undeniably useful for enhancing code readability and modularization, they're not always the most gas-efficient option. Especially in cases of single-use modifiers, inlining them can save on gas, ensuring a more economical execution. By regularly evaluating the need for modifiers and balancing between readability and gas costs, developers can create efficient and cost-effective smart contracts.

๐Ÿ“š
๐Ÿ–‹๏ธ
Book an audit with Zokyo