Cache array length outside of loop
If not cached, the solidity compiler will always read the length of the array during each iteration (except the first iteration):
Storage arrays incur sload operation - 100 additional extra gas for each iteration
Memory arrays incur mload operation - 3 additional extra gas for each iteration
Calldata arrays incur CALLDATALOAD operation - 3 additional extra gas for each iteration
Caching the length changes each of these to a DUP<N>
(3 gas), and gets rid of the extra DUP<N>
needed to store the stack offset.
Link to code - https://github.com/Cyfrin/2023-07-beedle/tree/main/src/Lender.sol
Code Review
Cache array length outside of loop
NeoCrao
The contest is live. Earn rewards by submitting a finding.
This is your time to appeal against judgements on your submissions.
Appeals are being carefully reviewed by our judges.