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 29: x = x + y is cheaper than x += y

PreviousGas Saving Technique 28: && operator uses more gasNextGas Saving Technique 30: Using 1 and 2 rather than 0 and 1 saves gas

Last updated 1 year ago

Introduction: At first glance, both x = x + y and x += y seem to perform the same arithmetic operation in Solidity. While their end result is indeed the same, the gas cost associated with these two statements can differ. In this tutorial, we delve into the nuances between the two, explaining why favoring the former can lead to enhanced gas efficiency.


Concept: In Solidity, the compound assignment operation x += y is syntactic sugar for x = x + y. However, the former tends to be slightly more expensive in gas terms. The additional cost stems from the overhead introduced by the compound assignment itself. Therefore, directly using x = x + y can be a more gas-efficient approach.


Underlying Problem:

  1. Compound Assignments: The added overhead of compound assignment operations (+=, -=, etc.) leads to marginally more gas consumption.

  2. Accumulation Over Transactions: In contracts with numerous transactions or in widely-used protocols, the extra gas from using compound assignments can accumulate significantly, affecting overall efficiency.


Example:

Using Compound Assignment:

solidityCopy codeself.balanceOf[_from] -= _amount;
self.balanceOf[_to] += _amount;

Optimized with Direct Assignment:

solidityCopy codeself.balanceOf[_from] = self.balanceOf[_from] - _amount;
self.balanceOf[_to] = self.balanceOf[_to] + _amount;

Recommendation:

  1. Review your contracts to identify occurrences of compound arithmetic assignments, like += or -=.

  2. Replace them with their direct assignment equivalents, adjusting any related logic if necessary.

  3. Thoroughly test your contracts post-modification to ensure no unintended behaviors have been introduced.

  4. Cultivate a practice of using direct arithmetic assignments when drafting new contracts, given that they can offer more predictable gas costs.


Conclusion: Gas efficiency is paramount in the realm of smart contracts. Embracing simple optimizations—like favoring direct assignments over compound ones—can lead to tangible gas savings, particularly in frequently executed contracts. By paying attention to these subtle differences, developers can produce not only functional but also economically efficient smart contracts, providing the best experience for users and the network.

📚
👓
Book an audit with Zokyo