Open code423n4 opened 2 years ago
No harm in applying the suggestion
However, from reviewing other contests, reading from calldata
has the same cost of reading from memory
The information in calldata is read without paying extra gas to store it in memory
Another has disputed the finding and I agree with them
That said, caching the value brings more readability, also the extra cost is 3 gas
Notice that this applies only to calldata
arguments
When reading length from storage, you 100% wanna cache that
For all the cases above, the argument is calldata
I'd highly recommend the sponsor run their own tests, however I believe this finding to be superfluous / ineffective
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:
Zapper.sol#constructor()
https://github.com/code-423n4/2021-10-ambire/blob/bc01af4df3f70d1629c4e22a72c19e6a814db70d/contracts/wallet/Zapper.sol#L79-L79Zapper.sol#approveMaxMany()
https://github.com/code-423n4/2021-10-ambire/blob/bc01af4df3f70d1629c4e22a72c19e6a814db70d/contracts/wallet/Zapper.sol#L87-L87Zapper.sol#exchangeV2()
https://github.com/code-423n4/2021-10-ambire/blob/bc01af4df3f70d1629c4e22a72c19e6a814db70d/contracts/wallet/Zapper.sol#L104-L104Zapper.sol#wrapLending()
https://github.com/code-423n4/2021-10-ambire/blob/bc01af4df3f70d1629c4e22a72c19e6a814db70d/contracts/wallet/Zapper.sol#L126-L126Zapper.sol#unwrapLending()
https://github.com/code-423n4/2021-10-ambire/blob/bc01af4df3f70d1629c4e22a72c19e6a814db70d/contracts/wallet/Zapper.sol#L131QuickAccManager.sol#sendTxns()
https://github.com/code-423n4/2021-10-ambire/blob/bc01af4df3f70d1629c4e22a72c19e6a814db70d/contracts/wallet/QuickAccManager.sol#L162-L162