Skip to content

Commit 393d2ce

Browse files
committed
perf: cleaner getTransportDestinations
1 parent e8b7ad1 commit 393d2ce

File tree

1 file changed

+13
-13
lines changed

1 file changed

+13
-13
lines changed

src/multichain/CrossChainRegistry.sol

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -392,27 +392,27 @@ contract CrossChainRegistry is
392392
function getTransportDestinations(
393393
OperatorSet memory operatorSet
394394
) public view returns (uint32[] memory) {
395-
// Only return chains that are whitelisted
396395
EnumerableSet.UintSet storage chainIDs = _transportDestinations[operatorSet.key()];
396+
uint256 length = chainIDs.length();
397397

398-
// First count how many chain IDs are whitelisted
398+
// Create result array with maximum possible size
399+
uint32[] memory result = new uint32[](length);
399400
uint256 count = 0;
400-
for (uint256 i = 0; i < chainIDs.length(); i++) {
401-
if (_whitelistedChainIDs.contains(uint32(chainIDs.at(i)))) {
402-
count++;
403-
}
404-
}
405401

406-
// Create result array with correct size
407-
uint32[] memory result = new uint32[](count);
408-
uint256 j = 0;
409-
for (uint256 i = 0; i < chainIDs.length(); i++) {
402+
// Single loop to filter whitelisted chains
403+
for (uint256 i = 0; i < length; i++) {
410404
uint32 chainID = uint32(chainIDs.at(i));
411405
if (_whitelistedChainIDs.contains(chainID)) {
412-
result[j] = chainID;
413-
j++;
406+
result[count] = chainID;
407+
count++;
414408
}
415409
}
410+
411+
// Resize the array to the actual count using assembly
412+
assembly {
413+
mstore(result, count)
414+
}
415+
// Only return chains that are whitelisted
416416
return result;
417417
}
418418

0 commit comments

Comments
 (0)