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

Open
code423n4 opened this issue Jul 15, 2022 · 0 comments
Open

Gas Optimizations #164

code423n4 opened this issue Jul 15, 2022 · 0 comments
Labels
bug Something isn't working duplicate This issue or pull request already exists G (Gas Optimization) wontfix out of scope, a non-issue, or something already addressed

Comments

@code423n4
Copy link
Contributor

GAS

1. Improve logic

Move the following line from VaultTracker.sol#L155-L158:

    Vault memory from = vaults[f];
-   Vault memory to = vaults[t];

    if (a > from.notional) { revert Exception(31, a, from.notional, address(0), address(0)); }
+   Vault memory to = vaults[t];

2. Avoid unused returns

Having a method that always returns the same value is not correct in terms of consumption, if you want to modify a value, and the method will perform a revert in case of failure, it is not necessary to return a true, since it will never be false. It is less expensive not to return anything, and it also eliminates the need to double-check the returned value by the caller.

Affected source code:

3. ++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--

Affected source code:

4. delete optimization

Use delete instead of set to default value (false or 0).

5 gas could be saved per entry in the following affected lines:

5. Reduce math operations

Use scientific notation (e.g. 10e17) rather than exponentiation (e.g. 10**18)

Change 2**256 - 1 to type(uint256).max:

@code423n4 code423n4 added bug Something isn't working G (Gas Optimization) labels Jul 15, 2022
code423n4 added a commit that referenced this issue Jul 15, 2022
@robrobbins robrobbins added duplicate This issue or pull request already exists wontfix out of scope, a non-issue, or something already addressed labels Aug 31, 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) wontfix out of scope, a non-issue, or something already addressed
Projects
None yet
Development

No branches or pull requests

2 participants