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

Gas Optimizations #61

Open
code423n4 opened this issue Apr 21, 2022 · 0 comments
Open

Gas Optimizations #61

code423n4 opened this issue Apr 21, 2022 · 0 comments
Labels
bug Something isn't working G (Gas Optimization)

Comments

@code423n4
Copy link
Contributor

Save gas in for loops by unchecked arithmetic

The for loop has no overflow risk of i. Use an unchecked block to save gas.

Proof of Concept

IndexLogic.sol
39:        for (uint i; i < assets.length(); ++i) {
60:        for (uint i; i < inactiveAssets.length(); ++i) {
102:        for (uint i; i < length; ++i) {
125:        for (uint i; i < length + inactiveAssets.length(); ++i) {

TrackedIndexReweightingLogic.sol
37:        for (uint i; i < assets.length(); ++i) {
66:        for (uint i; i < assets.length(); ++i) {

TopNMarketCapReweightingLogic.sol
37:        for (uint i; i < assets.length(); ++i) {
51:        for (uint _i; _i < diff.assetCount; ++_i) {
104:        for (uint i; i < _inactiveAssets.length; ++i) {

ManagedIndex.sol
30:        for (uint i; i < _assets.length; ++i) {

TopNMarketCapIndex.sol
48:        for (uint i; i < _assets.length; ++i) {

BaseIndex.sol
78:        for (uint i; i < _assets.length; ++i) {

UniswapV2PathPriceOracle.sol
34:        for (uint i = 0; i < path.length - 1; i++) {
49:        for (uint i = 0; i < path.length - 1; i++) {

TrackedIndex.sol
35:        for (uint i; i < _assets.length; ++i) {

ManagedIndexReweightingLogic.sol
38:        for (uint i; i < assets.length(); ++i) {
50:        for (uint i; i < _updatedAssets.length; ++i) {
96:        for (uint i; i < _inactiveAssets.length; ++i) {

Recommendation

Use unchecked blocks to avoid overflow checks, or use ++i rather than i++ if you don't use unchecked blocks.

for (uint256 i = 0; i < length; ) {
    ...
    unchecked {
        ++i;
    }
}
@code423n4 code423n4 added bug Something isn't working G (Gas Optimization) labels Apr 21, 2022
code423n4 added a commit that referenced this issue Apr 21, 2022
@jn-lp jn-lp added the duplicate This issue or pull request already exists label May 3, 2022
@jn-lp jn-lp closed this as completed May 3, 2022
@itsmetechjay itsmetechjay reopened this May 13, 2022
@liveactionllama liveactionllama removed the duplicate This issue or pull request already exists label May 17, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working G (Gas Optimization)
Projects
None yet
Development

No branches or pull requests

4 participants