Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
0xMilenov opened this issue Apr 18, 2024 · 0 comments
Open

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

0xMilenov opened this issue Apr 18, 2024 · 0 comments
Labels
Report Status: Recommended Severity: Gas Optimization Suggestion made by the team to improve protocol

Comments

@0xMilenov
Copy link

0xMilenov commented Apr 18, 2024

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.

@0xMilenov 0xMilenov added Severity: Gas Optimization Suggestion made by the team to improve protocol Report Status: Recommended labels Apr 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Report Status: Recommended Severity: Gas Optimization Suggestion made by the team to improve protocol
Projects
None yet
Development

No branches or pull requests

1 participant