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 30: Using 1 and 2 rather than 0 and 1 saves gas

PreviousGas Saving Technique 29: x = x + y is cheaper than x += yNextGas Saving Technique 31: Optimize Storage by Avoiding Booleans

Last updated 1 year ago

Introduction: Solidity is a unique language with many nuances that can affect gas consumption. One such nuance involves the representation and usage of integer constants. This tutorial will dive into why using 1 and 2 as constant values can be more gas-efficient than 0 and 1.


Concept: In Ethereum, the EVM (Ethereum Virtual Machine) treats the integer value 0 differently, as it's the default value of storage slots. When a storage slot is set to 0, the EVM refunds some gas because it assumes that you're freeing up storage. However, if you're using 0 and 1 for flagging or other logical purposes and not for freeing storage, this gas refund is not beneficial. Moreover, setting a value to 1 costs slightly more than setting it to any other non-zero value. Therefore, using 1 and 2 (or any other pair of non-zero values) can be more gas-efficient.


Underlying Problem:

  1. Unnecessary Gas Refund: Using 0 as a logical constant can trigger gas refunds, which are counterproductive if the intention isn't to free up storage.

  2. Higher Cost for 1: Setting a value to 1 in storage is slightly more expensive than setting it to other non-zero values.


Examples & Recommendations:

  1. DepositHandler Constants:

    • Before:

      solidityCopy codeuint256 internal constant IS_NOT_LOCKED = uint256(0);
      uint256 internal constant IS_LOCKED = uint256(1);
    • After:

      solidityCopy codeuint256 internal constant IS_NOT_LOCKED = uint256(1);
      uint256 internal constant IS_LOCKED = uint256(2);

Step-by-Step Guide to Implementing the Integer Gas Saving Technique:

  1. Review your contract for instances where 0 and 1 are used as logical constants or flags.

  2. Change those constants to 1 and 2 respectively.

  3. Ensure that any conditional checks or logic that relied on these constants are updated accordingly.

  4. Test the modified contract thoroughly to ensure consistent functionality.


Benefits:

  1. Gas Savings: By avoiding the default storage value (0) and the slightly higher cost of 1, contracts can conserve gas over multiple transactions.

  2. Clearer Intention: Using 1 and 2 makes it clear that these values are being used for logic, not for storage clearing.


Conclusion: Solidity, with its ties to the Ethereum ecosystem, has many peculiarities that can influence gas costs. Understanding these intricacies, like the special treatment of integer values 0 and 1, allows developers to write more optimized and efficient contracts.

๐Ÿ“š
๐Ÿ‘‚
Book an audit with Zokyo