Closed code423n4 closed 3 years ago
Duplicate of https://github.com/code-423n4/2021-10-pooltogether-findings/issues/7 Really not sure about these optimizations since these params are either calldata or memory, so it costs only 3 gas to access length. So storing in a variable will cost 3 more gas and it will still cost 3 gas per iteration.
As the sponsor showed, by using https://github.com/crytic/evm-opcodes, there is no difference in gas cost when reading from memory
or callData
from the input vs caching it inside the function
The act of storing it locally via MSTORE
will actually increase the gas cost by 3
Handle
WatchPug
Vulnerability details
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.
Instances include:
PrizeDistributor.sol#claim()
https://github.com/pooltogether/v4-core/blob/055335bf9b09e3f4bbe11a788710dd04d827bf37/contracts/PrizeDistributor.sol#L68-L68
DrawCalculator.sol#_getNormalizedBalancesAt()
https://github.com/pooltogether/v4-core/blob/055335bf9b09e3f4bbe11a788710dd04d827bf37/contracts/DrawCalculator.sol#L196-L196
PrizeDistributionBuffer.sol#getPrizeDistributions()
https://github.com/pooltogether/v4-core/blob/055335bf9b09e3f4bbe11a788710dd04d827bf37/contracts/PrizeDistributionBuffer.sol#L81-L81
Ticket.sol#_getAverageBalancesBetween()
https://github.com/pooltogether/v4-core/blob/055335bf9b09e3f4bbe11a788710dd04d827bf37/contracts/Ticket.sol#L260-L260