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 3: Double star ** inefficiency

PreviousGas Saving Technique 2: Immutable VariableNextGas Saving Technique 4: Cache Array Length

Last updated 1 year ago

Introduction

In Ethereum smart contracts, every gas unit saved matters. Even minor optimizations cumulatively lead to significant gas savings. One such subtle yet impactful optimization involves efficiently representing constant values, primarily focusing on avoiding the double star ** exponentiation operation where feasible and using alternative representations that are more gas-efficient.

Double Star ** Inefficiency Details & Impact

Why Optimize?

  • Gas Savings: The ** operation consumes more gas compared to alternative constant value representations. By using optimized representations, you save gas on each transaction invoking these constants.

Practical Examples

Using type(uint256).max Instead of 2**256 - 1

When you need to represent the maximum value for a uint256, using type(uint256).max is more efficient than calculating 2**256 - 1.

Example:

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

contract EfficientConstants {
    // Inefficient Representation
    // uint256 public constant MAX_UINT = 2**256 - 1;

    // Efficient Representation
    uint256 public constant MAX_UINT = type(uint256).max;
}

Using 1e18 Instead of 10**18

For values derived from exponentiation with base 10, itโ€™s more efficient to use the e notation. For example, 1e18 should be used instead of 10**18.

Example:

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

contract EfficientExponents {
    // Before Optimization
    // uint256 internal constant SHER_DECIMALS = 10**18;

    // After Optimization
    uint256 internal constant SHER_DECIMALS = 1e18;
    uint256 internal constant SHER_STEPS = 1e16;
    uint256 internal constant RATE_STEPS = 1e4;
}

Recommendations for Efficient Representation

  1. Avoid Double Star ** for Common Constants: For commonly used constants, especially those involved with decimal representation, use the e notation or the type().max where appropriate.

  2. Review and Refactor: Scrutinize your smart contract for uses of the ** operator and evaluate whether an alternative representation would be more gas-efficient without sacrificing readability or accuracy.

Conclusion

Minor optimizations, like efficiently representing constant values, collectively contribute to gas-efficient smart contracts. While the individual savings per transaction might be minor, considering the frequency and volume of transactions on the Ethereum network, these optimizations are worth implementing. Always ensure to test your contract thoroughly after making these changes to validate that the contract behavior remains as expected while enjoying reduced gas costs.

References

๐Ÿ“š
โœจ
Book an audit with Zokyo
[G-10] 10 ** 18 can be changed to 1e18 and save some gas