code-423n4 / 2022-06-badger-findings

0 stars 0 forks source link

Gas Optimizations #81

Closed code423n4 closed 2 years ago

code423n4 commented 2 years ago

An array’s length should be cached to save gas in for-loops

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:

MyStrategy.sol:300:        for (uint256 i = 0; i < _claims.length; i++) {
MyStrategy.sol:317:        for (uint256 i = 0; i < _claims.length; i++) {

Remediation:

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

JeeberC4 commented 2 years ago

Warden submitted multiple Gas Optimizations. Will not be judged.