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

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

Gas Optimizations #386

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

Comments

@code423n4
Copy link
Contributor

USE CUSTOM ERRORS RATHER THAN REVERT()/REQUIRE() STRINGS TO SAVE GAS

Rigor is using solidity version 0.8.6, while custom errors are available from solidity version 0.8.4, so it's suitable to use custom error. Custom errors save ~50 gas each time they’re hitby avoiding having to allocate and store the revert string. Not defining the strings also save deployment gas

.LENGTH SHOULD NOT BE LOOKED UP IN EVERY LOOP OF A FOR-LOOP

The overheads outlined below are PER LOOP, excluding the first loop

  • storage arrays incur a Gwarmaccess (100 gas)
  • memory arrays use MLOAD (3 gas)
  • calldata arrays use CALLDATALOAD (3 gas)
    Caching the length changes each of these to a DUP (3 gas), and gets rid of the extra DUP needed to store the stack offset

example loop _changeOrderedTask.length inside Project.sol

++I COSTS LESS GAS THAN I++, ESPECIALLY WHEN IT’S USED IN FOR-LOOPS (--I/I-- TOO)

Saves 6 gas per loop

example loop inside Community.sol, Project.sol, HomeFiProxy.sol, Tasks.sol

for (uint256 i = 0; i < _length; i++) 

SPLITTING REQUIRE() STATEMENTS THAT USE && SAVES GAS

See (this issue)[https://github.com/code-423n4/2022-01-xdefi-findings/issues/128] which describes the fact that there is a larger deployment gas cost, but with enough runtime calls, the change ends up being cheaper.

this issue exist inside Community.sol, Disputes.sol

require(
    _lendingNeeded >= _communityProject.totalLent &&
        _lendingNeeded <= IProject(_project).projectCost(),
    "Community::invalid lending"
);

USING PRIVATE RATHER THAN PUBLIC FOR CONSTANTS, SAVES GAS

If needed, the value can be read from the verified contract source code. Savings are due to the compiler not having to create non-payable getter functions for deployment calldata, and not adding another entry to the method ID table

example issue in Project.sol

uint256 public constant override VERSION = 25000;
@code423n4 code423n4 added bug Something isn't working G (Gas Optimization) labels Aug 6, 2022
code423n4 added a commit that referenced this issue Aug 6, 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

2 participants