Remove duplicated SLOAD in Arrays.findUpperBound (#4442)

Co-authored-by: Hadrien Croubois <hadrien.croubois@gmail.com>
Co-authored-by: Francisco Giordano <fg@frang.io>
pull/4462/head
Molly 2 years ago committed by GitHub
parent 921ac49ccb
commit 0abf18f305
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 5
      .changeset/fluffy-countries-buy.md
  2. 8
      contracts/utils/Arrays.sol

@ -0,0 +1,5 @@
---
'openzeppelin-solidity': minor
---
`Arrays`: Optimize `findUpperBound` by removing redundant SLOAD.

@ -22,13 +22,13 @@ library Arrays {
* repeated elements. * repeated elements.
*/ */
function findUpperBound(uint256[] storage array, uint256 element) internal view returns (uint256) { function findUpperBound(uint256[] storage array, uint256 element) internal view returns (uint256) {
if (array.length == 0) {
return 0;
}
uint256 low = 0; uint256 low = 0;
uint256 high = array.length; uint256 high = array.length;
if (high == 0) {
return 0;
}
while (low < high) { while (low < high) {
uint256 mid = Math.average(low, high); uint256 mid = Math.average(low, high);

Loading…
Cancel
Save