AuditBase
Sign InGet Started
infoG019

array[index] += amount is cheaper than array[index] = array[index] + amount (or related variants)

Learn how using array[index] += amount over array[index] = array[index] + amount can optimize gas usage in Solidity. Save 28 gas for storage arrays and 38 for memory arrays. Discover the efficiency of this optimization technique for arithmetic operations in loops.

Category

gas

Languages

solidity

Analysis Layer

static

Severity

info

When updating a value in an array with arithmetic, using array[index] += amount is cheaper than array[index] = array[index] + amount. This optimization can be particularly significant if the pattern occurs during a loop.

Background

In Solidity, when updating array values with arithmetic operations, such as addition (+=), subtraction (-=), division (/=), multiplication (*=), exponentiation (^=), bitwise AND (&=), modulo (%=), bitwise left shift (<<=), bitwise right shift (>>=), or bitwise unsigned right shift (>>>=), it is more gas-efficient to use the shorthand assignment operators (+=, -=, /=, *=, ^=, &=, %=, <<=, >>=, and >>>=) rather than the longer form assignment (array[index] = array[index] + amount).

Explanation

This optimization applies to situations where you need to update an array element by performing arithmetic operations on the existing value. By using the shorthand assignment operator (+=) instead of the longer form assignment (array[index] = array[index] + amount), you can save gas costs.

Storage Arrays

For storage arrays, the longer form assignment requires an extra sload operation to load the existing value from storage before performing the addition operation. On the other hand, the shorthand assignment operator directly performs the addition on the existing value and updates it in a single step. This eliminates the additional sload, resulting in gas savings.

Memory Arrays

For memory arrays, the longer form assignment requires an mload operation to load the existing value from memory before performing the addition operation. However, the shorthand assignment operator does not require this additional step. By using the shorthand operator, you avoid the extra mload operation, resulting in gas savings.

Example

Consider the following code snippet:

contract ArrayExample {
    uint256[] public storageArray;
    uint256[] public memoryArray;
    
    function updateStorageArray(uint256 index, uint256 amount) public {
        storageArray[index] += amount; // Shorthand assignment
    }
    
    function updateMemoryArray(uint256 index, uint256 amount) public {
        memoryArray[index] += amount; // Shorthand assignment
    }
}

In the updateStorageArray function, using storageArray[index] += amount is more gas-efficient than storageArray[index] = storageArray[index] + amount.

Similarly, in the updateMemoryArray function, using memoryArray[index] += amount is more gas-efficient than memoryArray[index] = memoryArray[index] + amount.

Gas Savings

By using the shorthand assignment operator, you can save gas costs. The specific amount of gas saved depends on whether the array is stored in storage or memory.

  • For a storage array, using the shorthand assignment operator saves 28 gas.
  • For a memory array, using the shorthand assignment operator saves 38 gas.

Keep in mind that these gas savings can be particularly significant if the pattern occurs repeatedly in a loop.

In conclusion, when updating array values with arithmetic operations, it is recommended to use the shorthand assignment operators (+=, -=, /=, *=, ^=, &=, %=, <<=, >>=, and >>>=) to optimize gas consumption.

Sources