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

Open
daopunk opened this issue Jan 19, 2025 · 0 comments
Open

Gas Optimizations #9

daopunk opened this issue Jan 19, 2025 · 0 comments
Assignees
Labels

Comments

@daopunk
Copy link
Collaborator

daopunk commented Jan 19, 2025

  • using multiple if-revert statements is cheaper than using one long if-revert statement that uses && / ||.
  • cache mapping lookups as function scoped variables rather than looking up the same mapping value multiple times in the same scope
  • for function params, use calldata instead of memory when the function is not modifying the param

gas ops for for-loops:

  • ++i is cheaper than i++
  • after sol 0.8.0, can use unchecked to increment vars that have no risk of overflow
  • cache the length in a var

example of most efficient solidity for-loop:

uint256 len = arr.length;
for (uint256 i; i < len) {
   unchecked { ++i }
   // logic
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants