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

Open
code423n4 opened this issue Aug 6, 2022 · 0 comments
Open

Gas Optimizations #387

code423n4 opened this issue Aug 6, 2022 · 0 comments

Comments

@code423n4
Copy link
Contributor

Summary

Id Title
1 Caching returned values from function calls can save gas
2 Cache length in the for loop and uncheck index

1. Caching returned values from function calls can save gas

External call to other contract is gas consuming and should avoid as much as possible.

For example, in Community.lendToProject() function, lenderFee() of _projectInstance is called 2 times. We should call only 1 time and cache the returned values to save gas.

Affected Codes

2. Cache length in the for loop and uncheck index

At each iteration of the loop, length is read from storage. We can cache the length and save gas per iteration.

Solidity 0.8.0 check safe math in every operation. Use uncheck to increase index can save gas.

For example

uint256 length = arr.length;
for (uint i; i < length; ) {
    // do stuff
    unchecked { ++i; }
}

Occurences

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants