Skip to content

Commit

Permalink
Slight for loop gas optimization
Browse files Browse the repository at this point in the history
Use unchecked and pre-increment for for-loop iterators
  • Loading branch information
redsh4de committed Jan 31, 2023
1 parent dd6eca7 commit 3eef2fe
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
3 changes: 2 additions & 1 deletion contracts/facets/DiamondLoupeFacet.sol
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,11 @@ contract DiamondLoupeFacet is IDiamondLoupe, IERC165 {
LibDiamond.DiamondStorage storage ds = LibDiamond.diamondStorage();
uint256 numFacets = ds.facetAddresses.length;
facets_ = new Facet[](numFacets);
for (uint256 i; i < numFacets; i++) {
for (uint256 i; i < numFacets;) {
address facetAddress_ = ds.facetAddresses[i];
facets_[i].facetAddress = facetAddress_;
facets_[i].functionSelectors = ds.facetFunctionSelectors[facetAddress_].functionSelectors;
unchecked { ++i; }
}
}

Expand Down
14 changes: 9 additions & 5 deletions contracts/libraries/LibDiamond.sol
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ library LibDiamond {
address _init,
bytes memory _calldata
) internal {
for (uint256 facetIndex; facetIndex < _diamondCut.length; facetIndex++) {
for (uint256 facetIndex; facetIndex < _diamondCut.length;) {
IDiamondCut.FacetCutAction action = _diamondCut[facetIndex].action;
if (action == IDiamondCut.FacetCutAction.Add) {
addFunctions(_diamondCut[facetIndex].facetAddress, _diamondCut[facetIndex].functionSelectors);
Expand All @@ -83,6 +83,7 @@ library LibDiamond {
} else {
revert("LibDiamondCut: Incorrect FacetCutAction");
}
unchecked { ++facetIndex; }
}
emit DiamondCut(_diamondCut, _init, _calldata);
initializeDiamondCut(_init, _calldata);
Expand All @@ -102,7 +103,8 @@ library LibDiamond {
address oldFacetAddress = ds.selectorToFacetAndPosition[selector].facetAddress;
require(oldFacetAddress == address(0), "LibDiamondCut: Can't add function that already exists");
addFunction(ds, selector, selectorPosition, _facetAddress);
selectorPosition++;
++selectorPosition;
unchecked { ++selectorIndex; }
}
}

Expand All @@ -115,13 +117,14 @@ library LibDiamond {
if (selectorPosition == 0) {
addFacet(ds, _facetAddress);
}
for (uint256 selectorIndex; selectorIndex < _functionSelectors.length; selectorIndex++) {
for (uint256 selectorIndex; selectorIndex < _functionSelectors.length;) {
bytes4 selector = _functionSelectors[selectorIndex];
address oldFacetAddress = ds.selectorToFacetAndPosition[selector].facetAddress;
require(oldFacetAddress != _facetAddress, "LibDiamondCut: Can't replace function with same function");
removeFunction(ds, oldFacetAddress, selector);
addFunction(ds, selector, selectorPosition, _facetAddress);
selectorPosition++;
++selectorPosition;
unchecked { ++selectorIndex; }
}
}

Expand All @@ -130,10 +133,11 @@ library LibDiamond {
DiamondStorage storage ds = diamondStorage();
// if function does not exist then do nothing and return
require(_facetAddress == address(0), "LibDiamondCut: Remove facet address must be address(0)");
for (uint256 selectorIndex; selectorIndex < _functionSelectors.length; selectorIndex++) {
for (uint256 selectorIndex; selectorIndex < _functionSelectors.length;) {
bytes4 selector = _functionSelectors[selectorIndex];
address oldFacetAddress = ds.selectorToFacetAndPosition[selector].facetAddress;
removeFunction(ds, oldFacetAddress, selector);
unchecked { ++selectorIndex; }
}
}

Expand Down

0 comments on commit 3eef2fe

Please sign in to comment.