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 #255

Open
code423n4 opened this issue May 24, 2022 · 0 comments
Open

Gas Optimizations #255

code423n4 opened this issue May 24, 2022 · 0 comments
Labels
bug Something isn't working duplicate This issue or pull request already exists G (Gas Optimization)

Comments

@code423n4
Copy link
Contributor

Unnecessary checked arithmetic in for loops

There is no risk of overflow caused by increments to the iteration index in for loops (the i++ in for (uint256 i = 0; i < numIterations; i++)). Increments perform overflow checks that are not necessary in this case.

Recommendation

Surround the increment expressions with an unchecked { ... } block to avoid the default overflow checks. For example, change the loop

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

to

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

It is a little less readable but it saves a significant amount of gas.

Unnecessary MLOADs in for-each loops

There are many for loops that follows this for-each pattern:

for (uint256 i = 0; i < array.length; i++) {
        // do something with `array[i]`
}

In such for loops, the array.length is read on every iteration, instead of caching it once in a local variable and read it from there. Memory reads are a bit more expensive than reading local variables.

Recommendation

Read these values from memory once, cache them in local variables and then read them again from the local variables. For example:

uint256 length = array.length;
for (uint256 i = 0; i < length; i++) {
        // do something with `array[i]`
}

Prefix increments / decrements are cheaper than postfix increments / decrements

Use prefix increments / decrements (++x / --x) instead of postfix increments / decrements (x++ / x--).

Recommendation

Change all postfix increments / decrements to prefix increments / decrements.

!= 0 is cheaper than > 0

It is more gas efficient to compare variables with != 0 than it is with > 0. These two comparisons are equivalent when the compared variable is an unsigned integer.

Recommendation

Change all > 0 comparisons of unsigned integers to != 0 comparisons.

@code423n4 code423n4 added bug Something isn't working G (Gas Optimization) labels May 24, 2022
code423n4 added a commit that referenced this issue May 24, 2022
@0xMaharishi 0xMaharishi added the duplicate This issue or pull request already exists label May 28, 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 duplicate This issue or pull request already exists G (Gas Optimization)
Projects
None yet
Development

No branches or pull requests

2 participants