VIS-2 / taobank-04-24

0 stars 0 forks source link

Increments/decrements can be unchecked in for-loops #27

Open 0xMilenov opened 4 months ago

0xMilenov commented 4 months ago

Context

VaultBorrowRate::getBorrowRate() LiquidationRouter::collaterals()

Description

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.

ethereum/solidity#10695

The change would be:

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

These save around 25 gas saved per instance. The same can be applied with decrements (which should use break when i == 0). The risk of overflow is non-existent for uint256.