You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
if it is a storage array, this is an extra sload operation (100 additional extra gas (EIP-2929 2) for each iteration except for the first),
if it is a memory array, this is an extra mload operation (3 additional gas for each iteration except for the first),
if it is a calldata array, this is an extra calldataload operation (3 additional gas for each iteration except for the first)
for loop postcondition can be made unchecked Gas savings: roughly speaking this can save 30-40 gas per loop iteration. For lengthy loops, this can be significant!
Solidity 0.8.4 introduced custom errors. They are more gas efficient than revert strings, when it comes to deployment cost as well as runtime cost when the revert condition is met. Use custom errors instead of revert strings for gas savings.
Any require statement in your code can be replaced with custom error for example,
// If there is enough funds to allocate this task
if (_costToAllocate >= _taskCost) {
// Reduce task cost from _costToAllocate
_costToAllocate -= _taskCost;
// Mark the task as allocated
tasks[_changeOrderedTask[i]].fundTask();
// Add task to _tasksAllocated array
_tasksAllocated[_loopCount] = _changeOrderedTask[i];
// Increment loop counter
_loopCount++;
}
// If there are not enough funds to allocate this task then stop looping
else {
break;
}
to
unchecked {
// If there is enough funds to allocate this task
if (_costToAllocate >= _taskCost) {
// Reduce task cost from _costToAllocate
_costToAllocate -= _taskCost;
// Mark the task as allocated
tasks[_changeOrderedTask[i]].fundTask();
// Add task to _tasksAllocated array
_tasksAllocated[_loopCount] = _changeOrderedTask[i];
// Increment loop counter
++_loopCount;
}
// If there are not enough funds to allocate this task then stop looping
else {
break;
}
}
The text was updated successfully, but these errors were encountered:
Caching the length in for loops and increment in for loop postcondition can be made unchecked
This reduce gas cost as show here https://forum.openzeppelin.com/t/a-collection-of-gas-optimisation-tricks/19966/5
Caching the length in for loops:
for loop postcondition can be made unchecked
Gas savings: roughly speaking this can save 30-40 gas per loop iteration. For lengthy loops, this can be significant!
https://github.com/code-423n4/2022-08-rigor/blob/b17b2a11d04289f9e927c71703b42771dd7b86a4/contracts/Project.sol#L603
Can be optimized to
Consider using ++i instead of i++
Currently every loops such as
https://github.com/code-423n4/2022-08-rigor/blob/b17b2a11d04289f9e927c71703b42771dd7b86a4/contracts/Project.sol#L603
are using i++ which is more expensive than ++i
is better than
Consider using custom errors instead of revert strings
This reduce gas cost as show here https://forum.openzeppelin.com/t/a-collection-of-gas-optimisation-tricks/19966/5
Solidity 0.8.4 introduced custom errors. They are more gas efficient than revert strings, when it comes to deployment cost as well as runtime cost when the revert condition is met. Use custom errors instead of revert strings for gas savings.
Any require statement in your code can be replaced with custom error for example,
Can be replaced with
Consider adding unchecked
https://github.com/code-423n4/2022-08-rigor/blob/b17b2a11d04289f9e927c71703b42771dd7b86a4/contracts/Project.sol#L672
to
https://github.com/code-423n4/2022-08-rigor/blob/b17b2a11d04289f9e927c71703b42771dd7b86a4/contracts/Project.sol#L613-L630
to
The text was updated successfully, but these errors were encountered: