sherlock-audit / 2024-05-pooltogether-judging

2 stars 0 forks source link

gas Optimization #167

Closed sherlock-admin2 closed 1 month ago

sherlock-admin2 commented 1 month ago

gas Optimization

Low/Info issue submitted by 0x1ns4n3

Summary Vulnerability Detail ++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

Code Snippet https://github.com/sherlock-audit/2024-05-pooltogether/blob/main/pt-v5-draw-manager/src/DrawManager.sol#L453

for (uint256 i = 0; i < length; i++) {
      (rewards[i], fractions[i]) = _computeStartDrawReward(previousStartTime, _startDrawAuctions[i].closedAt, _availableRewards);
      previousStartTime = _startDrawAuctions[i].closedAt;

Tool used Manual Review

Recommendation I suggest using ++i instead of i++ to increment the value of a uint variable.