++i costs less gas compared to i++ or i += 1++i costs less gas compared to i++ or i += 1 for unsigned integer, as pre-increment is
cheaper (about 5 gas iteration). This statement is true even with the optimizer enabled.
i++ increments i and retuns the initial value of i. Which means:
uint i = 1;
i++; // == 1 but i == 2
But ++i returns the actual incremented value:
uint i = 1;
++i; // == 2 and i == 2 too, so no need for a temporary variable
In the first case, the compiler has to create a temporary variable (when used) for returning 1
instead of 2
Impact
More Gase is used than necessary. On ethereum chains, it is around $1 worth of gas per instance per transaction
Arithmetics
Low/Info issue submitted by 0x1ns4n3
Summary
Vulnerability Detail
++i
costs less gas compared toi++
ori += 1
++i
costs less gas compared toi++
ori += 1
for unsigned integer, as pre-increment is cheaper (about 5 gas iteration). This statement is true even with the optimizer enabled.i++
incrementsi
and retuns the initial value ofi
. Which means:But
++i
returns the actual incremented value:In the first case, the compiler has to create a temporary variable (when used) for returning
1
instead of2
Impact
More Gase is used than necessary. On ethereum chains, it is around $1 worth of gas per instance per transaction
Code Snippet
https://github.com/sherlock-audit/2024-05-pooltogether/blob/main/pt-v5-claimer/src/Claimer.sol#L132 https://github.com/sherlock-audit/2024-05-pooltogether/blob/main/pt-v5-claimer/src/Claimer.sol#L158 https://github.com/sherlock-audit/2024-05-pooltogether/blob/main/pt-v5-claimer/src/Claimer.sol#L160 https://github.com/sherlock-audit/2024-05-pooltogether/blob/main/pt-v5-claimer/src/Claimer.sol#L164 https://github.com/sherlock-audit/2024-05-pooltogether/blob/main/pt-v5-claimer/src/Claimer.sol#L230
Tool used
Manual Review
Recommendation
I suggest using
++i
instead ofi++
to increment the value of a uint variable.