code-423n4 / 2022-05-backd-findings

0 stars 0 forks source link

Gas Optimizations #101

Closed code423n4 closed 2 years ago

code423n4 commented 2 years ago

For-Loops: Increments can be unchecked

In Solidity 0.8+, there’s a default overflow check on unsigned integers. It’s possible to uncheck this in for-loops and save some gas at each iteration, but at the cost of some code readability, as this uncheck cannot be made inline.

The code would go from:

for (uint256 i; i < numIterations; i++) {   // ...  }

to:

for (uint256 i; i < numIterations;) {   // ...   unchecked { ++i; }  }

Instances:

protocol/contracts/testing/MockFeeBurner.sol:27: for (uint256 i; i < tokens.length; i++) {

GalloDaSballo commented 2 years ago

20 gas