Silo Code Style Findings

Silo Code Style Findings

SIL-01C: Ineffectual Loop Iteration

Type Severity Location
Gas Optimization Silo.sol:L80

Description:

The linked loop is meant to iterate through all active BIPs and each time one is removed from the array the loop needs to accommodate for the potential BIP swap and reduce the iterator by one. This is ineffectual due to simultaneously reducing the iterator redundantly and reading the length of the array dynamically on each iteration.

Example:

protocol/contracts/farm/facets/SeasonFacet/Silo.sol
79function stepGovernance() internal {
80 for (uint256 i; i < s.g.activeBips.length; i++) {
81 uint32 bip = s.g.activeBips[i];
82 if (season() >= s.g.bips[bip].start.add(s.g.bips[bip].period)) {
83 endBip(bip, i);
84 i--;
85 }
86 }
87}

Recommendation:

We advise the loop to instead iterate from the end to the start and the length to be cached to an in-memory variable thus greatly reducing the gas cost of this function.

Alleviation:

The number of active BIPs is now properly cached to a local variable that is consequently utilized for the loop limit evaluation.

View Fix on GitHub
Navigated to Silo Code Style Findings