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

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

Gas Optimizations #322

code423n4 opened this issue Aug 6, 2022 · 0 comments

Comments

@code423n4
Copy link
Contributor

code423n4 commented Aug 6, 2022

G-1 UNCHECKED{++I} INSTEAD OF I++
[HomeFiProxy.sol L#87]https://github.com/code-423n4/2022-08-rigor/blob/b17b2a11d04289f9e927c71703b42771dd7b86a4/contracts/HomeFiProxy.sol#:~:text=for%20all%20implementation-,for%20(uint256%20i%20%3D%200%3B%20i%20%3C%20_length%3B%20i%2B%2B)%20%7B,-_generateProxy(allContractNames%5Bi

[HomeFiProxy.sol L#136]https://github.com/code-423n4/2022-08-rigor/blob/b17b2a11d04289f9e927c71703b42771dd7b86a4/contracts/HomeFiProxy.sol#:~:text=//%20Replace%20implementations-,for%20(uint256%20i%20%3D%200%3B%20i%20%3C%20_length%3B%20i%2B%2B)%20%7B,-_replaceImplementation(_contractNames%5Bi

Project.sol L#248

[Project.sol L#311]https://github.com/code-423n4/2022-08-rigor/blob/b17b2a11d04289f9e927c71703b42771dd7b86a4/contracts/Project.sol#:~:text=for%20each%20task.-,for%20(uint256%20i%20%3D%200%3B%20i%20%3C%20_length%3B%20i%2B%2B)%20%7B,-_inviteSC(_taskList%5Bi

Project.sol L#322

Project.sol L#603

G-2 .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<N> (3 gas), and gets rid of the extra DUP<N> needed to store the stack offset
Project.sol L#603

G-3 ++I COSTS LESS GAS THAN I++, ESPECIALLY WHEN IT’S USED IN FOR-LOOPS (--I/I-- TOO)
Saves 6 gas per loop

[HomeFiProxy.sol L#87]https://github.com/code-423n4/2022-08-rigor/blob/b17b2a11d04289f9e927c71703b42771dd7b86a4/contracts/HomeFiProxy.sol#:~:text=for%20all%20implementation-,for%20(uint256%20i%20%3D%200%3B%20i%20%3C%20_length%3B%20i%2B%2B)%20%7B,-_generateProxy(allContractNames%5Bi

[HomeFiProxy.sol L#136]https://github.com/code-423n4/2022-08-rigor/blob/b17b2a11d04289f9e927c71703b42771dd7b86a4/contracts/HomeFiProxy.sol#:~:text=//%20Replace%20implementations-,for%20(uint256%20i%20%3D%200%3B%20i%20%3C%20_length%3B%20i%2B%2B)%20%7B,-_replaceImplementation(_contractNames%5Bi

Project.sol L#248

[Project.sol L#311]https://github.com/code-423n4/2022-08-rigor/blob/b17b2a11d04289f9e927c71703b42771dd7b86a4/contracts/Project.sol#:~:text=for%20each%20task.-,for%20(uint256%20i%20%3D%200%3B%20i%20%3C%20_length%3B%20i%2B%2B)%20%7B,-_inviteSC(_taskList%5Bi

Project.sol L#322

Project.sol L#603

G-4 MULTIPLE ADDRESS MAPPINGS CAN BE COMBINED INTO A SINGLE MAPPING OF AN ADDRESS TO A STRUCT, WHERE APPROPRIATE
Saves a storage slot for the mapping. Depending on the circumstances and sizes of types, can avoid a Gsset (20000 gas) per mapping combined. Reads and subsequent writes can also be cheaper when a function requires both values and they both fit in the same storage slot. Finally, if both fields are accessed in the same function, can save ~42 gas per access due to not having to recalculate the key’s keccak256 hash (Gkeccak256 - 30 gas) and that calculation’s associated stack operations.

[HomeFi.sol L#62-66]https://github.com/code-423n4/2022-08-rigor/blob/b17b2a11d04289f9e927c71703b42771dd7b86a4/contracts/HomeFi.sol#:~:text=mapping(uint256,public%20override%20wrappedToken%3B

G-5 USING > 0 COSTS MORE GAS THAN != 0 WHEN USED ON A UINT IN A REQUIRE() STATEMENT
Community.sol L#764

@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
Projects
None yet
Development

No branches or pull requests

2 participants