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

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

Gas Optimizations #382

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

Comments

@code423n4
Copy link
Contributor

[G-01] ++variable costs less gas than variable++, especially in for loops

Saves about 5 gas per loop.

File: contracts/Project.sol
248: for (uint256 i = 0; i < _length; i++) {

603: for (; i < _changeOrderedTask.length; i++) {

625: _loopCount++;

710: for (uint256 _taskID = 1; _taskID <= _length; _taskID++) {

https://github.com/code-423n4/2022-08-rigor/blob/main/contracts/Project.sol

[G-02] The increment in for loop post condition can be made unchecked

The solidity compiler will apply arithmetic checks for the increment step during for loops. This can be disabled since the value being incremented won't surpass the upper bound that's checked on the break condition.

Adding uncheck can save 30-40 gas per loop.

File: contracts/Project.sol
248: for (uint256 i = 0; i < _length; i++) {

603: for (; i < _changeOrderedTask.length; i++) {

710: for (uint256 _taskID = 1; _taskID <= _length; _taskID++) {

https://github.com/code-423n4/2022-08-rigor/blob/main/contracts/Project.sol

[G-03] Array.length should not be computed on every interation during a loop

Instead of computing array.length for every iteration, the value for array.length should be cached before the loop to save gas.

File: contracts/Project.sol
603: for (; i < _changeOrderedTask.length; i++) {

https://github.com/code-423n4/2022-08-rigor/blob/main/contracts/Project.sol

[G-04] It costs more gas to initialize variables to zero than to let the default of zero be applied

File: contracts/Project.sol
248: for (uint256 i = 0; i < _length; i++) {

https://github.com/code-423n4/2022-08-rigor/blob/main/contracts/Project.sol

[G-05] != costs less gas than > in conditional contexts for uints

Saves about 6 gas.

File: contracts/Project.sol
195: require(_cost > 0, "Project::!value>0");

691: if (_loopCount > 0) emit TaskAllocated(_tasksAllocated);

https://github.com/code-423n4/2022-08-rigor/blob/main/contracts/Project.sol

[G-06] Use custom errors rather than revert()/require() to save gas

File: contracts/Project.sol
123: require(!contractorConfirmed, "Project::GC accepted");

132: require(_projectAddress == address(this), "Project::!projectAddress");

https://github.com/code-423n4/2022-08-rigor/blob/main/contracts/Project.sol

File: contracts/Community.sol
69: require(_address != address(0), "Community::0 address");

75: require(_msgSender() == homeFi.admin(), "Community::!admin");

81: require(

https://github.com/code-423n4/2022-08-rigor/blob/main/contracts/Community.sol

File: contracts/HomeFi.sol
142: require(!addrSet, "HomeFi::Set");

191: require(lenderFee != _newLenderFee, "HomeFi::!Change");

https://github.com/code-423n4/2022-08-rigor/blob/main/contracts/HomeFi.sol

@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