The referenced code block is meant to validate that no duplicates are present in the _assets array by iterating it in a nested loop.
Example:
unchecked {
for (uint256 i = 0; i < assetsLen; i++) {
for (uint256 j = i + 1; j < assetsLen; j++) {
if (_assets[i] == _assets[j]) {
revert StabilityPool__DuplicateElementOnArray();
}
}
}
}
Recommendation:
We advise the code to instead follow a different approach, mandating that the address values within _assets are ordered in a strictly ascending / descending order.
Given that all address values are unique and they represent a "value", it is possible to ascertain that the _assets array contains no duplicates in one loop by ensuring that the caller of the function has supplied the assets in a strictly ascending / descending order.
SPL-01C: Optimization of Duplicate Entry Sanitization
Description:
The referenced code block is meant to validate that no duplicates are present in the
_assets
array by iterating it in a nested loop.Example:
Recommendation:
We advise the code to instead follow a different approach, mandating that the
address
values within_assets
are ordered in a strictly ascending / descending order.Given that all
address
values are unique and they represent a "value", it is possible to ascertain that the_assets
array contains no duplicates in one loop by ensuring that the caller of the function has supplied the assets in a strictly ascending / descending order.