sherlock-audit / 2024-05-pooltogether-judging

2 stars 0 forks source link

Using Arrays #166

Closed sherlock-admin3 closed 1 month ago

sherlock-admin3 commented 1 month ago

Using Arrays

Low/Info issue submitted by 0x1ns4n3

Summary

AN ARRAY’S LENGTH SHOULD BE CACHED TO SAVE GAS IN FOR-LOOPS

Vulnerability Detail

Reading array length at each iteration of the loop takes 6 gas (3 for mload and 3 to place memory_offset) in the stack.

Caching the array length in the stack saves around 3 gas per iteration.

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

Here, I suggest storing the array’s length in a variable before the for-loop.