1. For Loop Improvements through Local Variable Caching and Prefix Increment
The for loop in createProject(string memory _projectId, Collection[] memory _collections) within CoreFactory.sol performs a length check and postfix increment at each iteration. The code can be seen below:
require(
_collections.length > 0,
'CoreFactory: should have more at least one collection'
);
for (uint256 i; i < _collections.length; i++) {
...
}
This can be improved by storing the collections length in a local variable in memory and using a prefix increment instead.
Recommended logic:
uint collLength = _collections.length;
require(
collLength > 0,
'CoreFactory: should have more at least one collection'
);
for (uint256 i; i < collLength; ++i) {
...
}
Output of provided tests can be seen below. The gas savings would be greater for projects with larger collections.
JOYN Contest
April 1, 2022
@securerodd #
Gas Optimizations
1. For Loop Improvements through Local Variable Caching and Prefix Increment
The for loop in
createProject(string memory _projectId, Collection[] memory _collections)
withinCoreFactory.sol
performs a length check and postfix increment at each iteration. The code can be seen below:This can be improved by storing the collections length in a local variable in memory and using a prefix increment instead.
Recommended logic:
Output of provided tests can be seen below. The gas savings would be greater for projects with larger collections.
Existing logic:
Recommened logic: