diff --git a/.changeset/fluffy-countries-buy.md b/.changeset/fluffy-countries-buy.md new file mode 100644 index 00000000000..0cc7de37039 --- /dev/null +++ b/.changeset/fluffy-countries-buy.md @@ -0,0 +1,5 @@ +--- +'openzeppelin-solidity': minor +--- + +`Arrays`: Optimize `findUpperBound` by removing redundant SLOAD. diff --git a/contracts/utils/Arrays.sol b/contracts/utils/Arrays.sol index 7dbc86fdd52..0c16b07cf57 100644 --- a/contracts/utils/Arrays.sol +++ b/contracts/utils/Arrays.sol @@ -22,13 +22,13 @@ library Arrays { * repeated elements. */ function findUpperBound(uint256[] storage array, uint256 element) internal view returns (uint256) { - if (array.length == 0) { - return 0; - } - uint256 low = 0; uint256 high = array.length; + if (high == 0) { + return 0; + } + while (low < high) { uint256 mid = Math.average(low, high);