From 76e80f3f25649d6e6ef82f86964da86503a848db Mon Sep 17 00:00:00 2001 From: Dan Liu Date: Fri, 8 Jul 2022 09:21:55 +0800 Subject: [PATCH] perf(BytesArrayUtils): reduce gas of function toBool --- contracts/lib/BytesArrayUtils.sol | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/contracts/lib/BytesArrayUtils.sol b/contracts/lib/BytesArrayUtils.sol index 913446042..34457a5f5 100644 --- a/contracts/lib/BytesArrayUtils.sol +++ b/contracts/lib/BytesArrayUtils.sol @@ -34,16 +34,16 @@ library BytesArrayUtils { * @return bool Boolean value */ function toBool(bytes memory _bytes, uint256 _start) internal pure returns (bool) { - require(_start + 1 >= _start, "toBool_overflow"); - require(_bytes.length >= _start + 1, "toBool_outOfBounds"); + require(_start != type(uint256).max, "toBool_overflow"); + require(_bytes.length > _start, "toBool_outOfBounds"); uint8 tempUint; assembly { tempUint := mload(add(add(_bytes, 0x1), _start)) } - require(tempUint <= 1, "Invalid bool data"); // Should be either 0 or 1 + require(tempUint < 2, "Invalid bool data"); // Should be either 0 or 1 - return (tempUint == 0) ? false : true; + return tempUint != 0; } } \ No newline at end of file