From e8a64126cd2e42324703324b6458ac4273cc7826 Mon Sep 17 00:00:00 2001 From: ronhuafeng Date: Fri, 9 Sep 2022 17:09:17 +0100 Subject: [PATCH] Optimize Math.max and SignedMath.max (#3679) Co-authored-by: Daniel Liu --- CHANGELOG.md | 1 + contracts/utils/math/Math.sol | 2 +- contracts/utils/math/SignedMath.sol | 2 +- 3 files changed, 3 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 04c3d0ea555..85d0f74a167 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -29,6 +29,7 @@ * `Arrays`: Add `unsafeAccess` functions that allow reading and writing to an element in a storage array bypassing Solidity's "out-of-bounds" check. ([#3589](https://github.com/OpenZeppelin/openzeppelin-contracts/pull/3589)) * `Strings`: optimize `toString`. ([#3573](https://github.com/OpenZeppelin/openzeppelin-contracts/pull/3573)) * `Ownable2Step`: extension of `Ownable` that makes the ownership transfers a two step process. ([#3620](https://github.com/OpenZeppelin/openzeppelin-contracts/pull/3620)) + * `Math` and `SignedMath`: optimize function `max` by using `>` instead of `>=`. ([#3679](https://github.com/OpenZeppelin/openzeppelin-contracts/pull/3679)) ### Breaking changes diff --git a/contracts/utils/math/Math.sol b/contracts/utils/math/Math.sol index 379407c42e4..67b1857adcd 100644 --- a/contracts/utils/math/Math.sol +++ b/contracts/utils/math/Math.sol @@ -17,7 +17,7 @@ library Math { * @dev Returns the largest of two numbers. */ function max(uint256 a, uint256 b) internal pure returns (uint256) { - return a >= b ? a : b; + return a > b ? a : b; } /** diff --git a/contracts/utils/math/SignedMath.sol b/contracts/utils/math/SignedMath.sol index 5a9d6068d7b..2a269fbe51b 100644 --- a/contracts/utils/math/SignedMath.sol +++ b/contracts/utils/math/SignedMath.sol @@ -11,7 +11,7 @@ library SignedMath { * @dev Returns the largest of two signed numbers. */ function max(int256 a, int256 b) internal pure returns (int256) { - return a >= b ? a : b; + return a > b ? a : b; } /**