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

Open
code423n4 opened this issue Jun 16, 2022 · 2 comments
Open

Gas Optimizations #6

code423n4 opened this issue Jun 16, 2022 · 2 comments
Assignees
Labels

Comments

@code423n4
Copy link
Contributor

Use Custom Errors instead of Revert Strings to save Gas

Custom errors from Solidity 0.8.4 are cheaper than revert strings (cheaper deployment cost and runtime cost when the revert condition is met)

Source Custom Errors in Solidity:

Starting from Solidity v0.8.4, there is a convenient and gas-efficient way to explain to users why an operation failed through the use of custom errors. Until now, you could already use strings to give more information about failures (e.g., revert("Insufficient funds.");), but they are rather expensive, especially when it comes to deploy cost, and it is difficult to use dynamic information in them.

Custom errors are defined using the error statement, which can be used inside and outside of contracts (including interfaces and libraries).

If it's not possible to use error codes due to the pragma used, it is recommended to reduce the strings to less than 32 bytes.

All contracts in scope are affected.

++i costs less gas compared to i++ or i += 1

++i costs less gas compared to i++ or i += 1 for unsigned integer, as pre-increment is cheaper (about 5 gas per iteration). This statement is true even with the optimizer enabled.

i++ increments i and returns the initial value of i. Which means:

uint i = 1;
i++; // == 1 but i == 2

But ++i returns the actual incremented value:

uint i = 1;
++i; // == 2 and i == 2 too, so no need for a temporary variable

In the first case, the compiler has to create a temporary variable (when used) for returning 1 instead of 2
I suggest using ++i instead of i++ to increment the value of an uint variable. Same thing for --i and i--

It is also recommended to not initialize the counter variable and surround the increment with an unchecked region.

Affected source code:

@code423n4 code423n4 added bug Something isn't working G (Gas Optimization) labels Jun 16, 2022
code423n4 added a commit that referenced this issue Jun 16, 2022
@obatirou obatirou self-assigned this Jun 16, 2022
@maximebrugel
Copy link
Collaborator

Use Custom Errors instead of Revert Strings to save Gas (Disputed)

Already surfaced in previous audit and mentioned in README.md

@Yashiru
Copy link
Collaborator

Yashiru commented Jun 24, 2022

++i costs less gas compared to i++ or i += 1 (Duplicated)

Duplicated of #2 at For loop optimizaion

@Yashiru Yashiru added sponsor disputed Sponsor cannot duplicate the issue, or otherwise disagrees this is an issue duplicate This issue or pull request already exists and removed sponsor disputed Sponsor cannot duplicate the issue, or otherwise disagrees this is an issue labels Jun 24, 2022
@JeeberC4 JeeberC4 removed the duplicate This issue or pull request already exists label Jul 20, 2022
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

6 participants