Skip to content

Commit cea4957

Browse files
committed
refactor: replace unchecked increments with just preincrements due to compiler optimizations
1 parent 1bc4ed0 commit cea4957

5 files changed

Lines changed: 26 additions & 47 deletions

File tree

src/facets/DiamondLoupeFacet.sol

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,10 @@ contract DiamondLoupeFacet is IDiamondLoupe {
1818
DiamondStorage storage ds = LibDiamond._diamondStorage();
1919
uint256 facetCount = ds.facetAddresses.length;
2020
facets_ = new Facet[](facetCount);
21-
for (uint256 i; i < facetCount;) {
21+
for (uint256 i; i < facetCount; ++i) {
2222
address facetAddr = ds.facetAddresses[i];
2323
facets_[i].facetAddress = facetAddr;
2424
facets_[i].functionSelectors = ds.facetToSelectorsAndPosition[facetAddr].functionSelectors;
25-
unchecked {
26-
++i;
27-
}
2825
}
2926
}
3027

src/initializers/MultiInit.sol

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,8 @@ contract MultiInit {
1919
if (addressesLength != _calldata.length) {
2020
revert AddressAndCalldataLengthDoNotMatch();
2121
}
22-
for (uint256 i; i < addressesLength;) {
22+
for (uint256 i; i < addressesLength; ++i) {
2323
LibDiamond._initializeDiamondCut(_addresses[i], _calldata[i]);
24-
unchecked {
25-
++i;
26-
}
2724
}
2825
}
2926
}

src/libraries/LibDiamond.sol

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ library LibDiamond {
5454
function _diamondCut(FacetCut[] memory _facetCuts, address _init, bytes memory _calldata) internal {
5555
uint256 facetCutsLength = _facetCuts.length;
5656
if (facetCutsLength == 0) revert NoFacetsInDiamondCut();
57-
for (uint256 facetIndex; facetIndex < facetCutsLength;) {
57+
for (uint256 facetIndex; facetIndex < facetCutsLength; ++facetIndex) {
5858
FacetCutAction action = _facetCuts[facetIndex].action;
5959
if (action == FacetCutAction.Add) {
6060
_addFunctions(_facetCuts[facetIndex].facetAddress, _facetCuts[facetIndex].functionSelectors);
@@ -65,9 +65,6 @@ library LibDiamond {
6565
} else {
6666
revert IncorrectFacetCutAction(uint8(action));
6767
}
68-
unchecked {
69-
++facetIndex;
70-
}
7168
}
7269
emit DiamondCut(_facetCuts, _init, _calldata);
7370
_initializeDiamondCut(_init, _calldata);
@@ -86,15 +83,12 @@ library LibDiamond {
8683
if (selectorPosition == 0) {
8784
_addFacet(ds, _facetAddress);
8885
}
89-
for (uint256 selectorIndex; selectorIndex < functionSelectorsLength;) {
86+
for (uint256 selectorIndex; selectorIndex < functionSelectorsLength; ++selectorIndex) {
9087
bytes4 selector = _functionSelectors[selectorIndex];
9188
address oldFacetAddress = ds.selectorToFacetAndPosition[selector].facetAddress;
9289
if (oldFacetAddress != address(0)) revert CannotAddFunctionToDiamondThatAlreadyExists(selector);
9390
_addFunction(ds, selector, selectorPosition, _facetAddress);
94-
selectorPosition++;
95-
unchecked {
96-
++selectorIndex;
97-
}
91+
++selectorPosition;
9892
}
9993
}
10094

@@ -111,18 +105,15 @@ library LibDiamond {
111105
if (selectorPosition == 0) {
112106
_addFacet(ds, _facetAddress);
113107
}
114-
for (uint256 selectorIndex; selectorIndex < functionSelectorsLength;) {
108+
for (uint256 selectorIndex; selectorIndex < functionSelectorsLength; ++selectorIndex) {
115109
bytes4 selector = _functionSelectors[selectorIndex];
116110
address oldFacetAddress = ds.selectorToFacetAndPosition[selector].facetAddress;
117111
if (oldFacetAddress == _facetAddress) {
118112
revert CannotReplaceFunctionWithTheSameFunctionFromTheSameFacet(selector);
119113
}
120114
_removeFunction(ds, oldFacetAddress, selector);
121115
_addFunction(ds, selector, selectorPosition, _facetAddress);
122-
selectorPosition++;
123-
unchecked {
124-
++selectorIndex;
125-
}
116+
++selectorPosition;
126117
}
127118
}
128119

@@ -134,13 +125,10 @@ library LibDiamond {
134125
if (_facetAddress != address(0)) revert RemoveFacetAddressMustBeZeroAddress(_facetAddress);
135126
if (functionSelectorsLength == 0) revert NoSelectorsProvidedForFacetForCut(_facetAddress);
136127
DiamondStorage storage ds = _diamondStorage();
137-
for (uint256 selectorIndex; selectorIndex < functionSelectorsLength;) {
128+
for (uint256 selectorIndex; selectorIndex < functionSelectorsLength; ++selectorIndex) {
138129
bytes4 selector = _functionSelectors[selectorIndex];
139130
address oldFacetAddress = ds.selectorToFacetAndPosition[selector].facetAddress;
140131
_removeFunction(ds, oldFacetAddress, selector);
141-
unchecked {
142-
++selectorIndex;
143-
}
144132
}
145133
}
146134

test/DiamondTester.t.sol

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,17 @@ contract DiamondTester is DeployedDiamondState {
2323
/// @dev Expects exactly 3 standard facets: DiamondCut, DiamondLoupe, and OwnableRoles.
2424
function testStandardFacetsDeployed() public view {
2525
assertEq(facetAddresses.length, 3);
26-
for (uint256 i; i < facetAddresses.length; i++) {
26+
for (uint256 i; i < facetAddresses.length; ++i) {
2727
assertNotEq(address(facetAddresses[i]), address(0));
2828
}
2929
}
3030

3131
/// @notice Ensures all function selectors are registered correctly for each facet.
3232
/// @dev Compares generated selectors with those registered in the diamond via facetAddress().
3333
function testSelectorsAreComplete() public {
34-
for (uint256 i; i < facetAddresses.length; i++) {
34+
for (uint256 i; i < facetAddresses.length; ++i) {
3535
bytes4[] memory fromGenSelectors = _generateSelectors(facetNames[i]);
36-
for (uint256 j; j < fromGenSelectors.length; j++) {
36+
for (uint256 j; j < fromGenSelectors.length; ++j) {
3737
assertEq(facetAddresses[i], diamondLoupe.facetAddress(fromGenSelectors[j]));
3838
}
3939
}
@@ -42,8 +42,8 @@ contract DiamondTester is DeployedDiamondState {
4242
/// @notice Asserts that all function selectors across all facets are unique.
4343
function testSelectorsAreUnique() public view {
4444
bytes4[] memory allSelectors = getAllSelectors(address(diamond));
45-
for (uint256 i; i < allSelectors.length; i++) {
46-
for (uint256 j = i + 1; j < allSelectors.length; j++) {
45+
for (uint256 i; i < allSelectors.length; ++i) {
46+
for (uint256 j = i + 1; j < allSelectors.length; ++j) {
4747
assertNotEq(allSelectors[i], allSelectors[j]);
4848
}
4949
}
@@ -52,8 +52,8 @@ contract DiamondTester is DeployedDiamondState {
5252
/// @notice Ensures each selector maps back to the correct facet.
5353
function testSelectorToFacetMappingIsCorrect() public view {
5454
Facet[] memory facetsList = diamondLoupe.facets();
55-
for (uint256 i; i < facetsList.length; i++) {
56-
for (uint256 j; j < facetsList[i].functionSelectors.length; j++) {
55+
for (uint256 i; i < facetsList.length; ++i) {
56+
for (uint256 j; j < facetsList[i].functionSelectors.length; ++j) {
5757
bytes4 selector = facetsList[i].functionSelectors[j];
5858
address expected = facetsList[i].facetAddress;
5959
assertEq(diamondLoupe.facetAddress(selector), expected);
@@ -63,9 +63,9 @@ contract DiamondTester is DeployedDiamondState {
6363

6464
/// @notice Ensures facet addresses return the correct function selectors.
6565
function testFacetAddressToSelectorsMappingIsCorrect() public view {
66-
for (uint256 i; i < facetAddresses.length; i++) {
66+
for (uint256 i; i < facetAddresses.length; ++i) {
6767
bytes4[] memory selectors = diamondLoupe.facetFunctionSelectors(facetAddresses[i]);
68-
for (uint256 j; j < selectors.length; j++) {
68+
for (uint256 j; j < selectors.length; ++j) {
6969
assertEq(diamondLoupe.facetAddress(selectors[j]), facetAddresses[i]);
7070
}
7171
}

test/helpers/HelperContract.sol

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,8 @@ abstract contract HelperContract is Test {
3232
// Initialize the selectors array with the selectorCount
3333
selectors_ = new bytes4[](keysLength);
3434

35-
for (uint256 i; i < keysLength;) {
35+
for (uint256 i; i < keysLength; ++i) {
3636
selectors_[i] = bytes4(bytes32(keccak256(bytes(keys[i]))));
37-
unchecked {
38-
++i;
39-
}
4037
}
4138
}
4239

@@ -48,7 +45,7 @@ abstract contract HelperContract is Test {
4845
uint256 arrayLength = _array.length;
4946
array_ = new bytes4[](arrayLength - 1);
5047
uint256 j = 0;
51-
for (uint256 i; i < arrayLength; i++) {
48+
for (uint256 i; i < arrayLength; ++i) {
5249
if (i != _index) {
5350
array_[j] = _array[i];
5451
j += 1;
@@ -62,7 +59,7 @@ abstract contract HelperContract is Test {
6259
/// @return array_ A new array without the specified selector.
6360
function removeElement(bytes4 el, bytes4[] memory _array) public pure returns (bytes4[] memory array_) {
6461
uint256 arrayLength = _array.length;
65-
for (uint256 i; i < arrayLength; i++) {
62+
for (uint256 i; i < arrayLength; ++i) {
6663
if (_array[i] == el) {
6764
array_ = removeElement(i, _array);
6865
}
@@ -75,7 +72,7 @@ abstract contract HelperContract is Test {
7572
/// @return `true` if the selector is found, `false` otherwise.
7673
function containsElement(bytes4[] memory _array, bytes4 _el) public pure returns (bool) {
7774
uint256 arrayLength = _array.length;
78-
for (uint256 i; i < arrayLength; i++) {
75+
for (uint256 i; i < arrayLength; ++i) {
7976
if (_array[i] == _el) {
8077
return true;
8178
}
@@ -89,7 +86,7 @@ abstract contract HelperContract is Test {
8986
/// @return `true` if the address is found, `false` otherwise.
9087
function containsElement(address[] memory _array, address _el) public pure returns (bool) {
9188
uint256 arrayLength = _array.length;
92-
for (uint256 i; i < arrayLength; i++) {
89+
for (uint256 i; i < arrayLength; ++i) {
9390
if (_array[i] == _el) {
9491
return true;
9592
}
@@ -106,7 +103,7 @@ abstract contract HelperContract is Test {
106103
if (array1Length != _array2.length) {
107104
return false;
108105
}
109-
for (uint256 i; i < array1Length; i++) {
106+
for (uint256 i; i < array1Length; ++i) {
110107
if (containsElement(_array1, _array2[i])) {
111108
return true;
112109
}
@@ -122,14 +119,14 @@ abstract contract HelperContract is Test {
122119

123120
uint256 facetListLength = facetList.length;
124121
uint256 len = 0;
125-
for (uint256 i; i < facetListLength; i++) {
122+
for (uint256 i; i < facetListLength; ++i) {
126123
len += facetList[i].functionSelectors.length;
127124
}
128125

129126
uint256 pos = 0;
130127
selectors_ = new bytes4[](len);
131-
for (uint256 i; i < facetListLength; i++) {
132-
for (uint256 j; j < facetList[i].functionSelectors.length; j++) {
128+
for (uint256 i; i < facetListLength; ++i) {
129+
for (uint256 j; j < facetList[i].functionSelectors.length; ++j) {
133130
selectors_[pos] = facetList[i].functionSelectors[j];
134131
pos += 1;
135132
}

0 commit comments

Comments
 (0)