From 2554d1e3864bdf02bb187a9edce20025c7ae8126 Mon Sep 17 00:00:00 2001 From: Vectorized Date: Thu, 24 Apr 2025 10:44:00 +0000 Subject: [PATCH 1/3] Opt --- src/utils/LibZip.sol | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/src/utils/LibZip.sol b/src/utils/LibZip.sol index 9fe31a8771..2aeb7ac067 100644 --- a/src/utils/LibZip.sol +++ b/src/utils/LibZip.sol @@ -228,13 +228,6 @@ library LibZip { function cdDecompress(bytes memory data) internal pure returns (bytes memory result) { /// @solidity memory-safe-assembly assembly { - function countLeadingZeroBytes(x_) -> _r { - _r := shl(7, lt(0xffffffffffffffffffffffffffffffff, x_)) - _r := or(_r, shl(6, lt(0xffffffffffffffff, shr(_r, x_)))) - _r := or(_r, shl(5, lt(0xffffffff, shr(_r, x_)))) - _r := or(_r, shl(4, lt(0xffff, shr(_r, x_)))) - _r := xor(31, or(shr(3, _r), lt(0xff, shr(_r, x_)))) - } function min(x_, y_) -> _z { _z := xor(x_, mul(xor(x_, y_), lt(y_, x_))) } @@ -265,7 +258,12 @@ library LibZip { } mstore(o, c) c := not(or(or(add(and(c, m), m), c), m)) - c := add(iszero(c), countLeadingZeroBytes(c)) + let r := shl(7, lt(0xffffffffffffffffffffffffffffffff, c)) + r := or(r, shl(6, lt(0xffffffffffffffff, shr(r, c)))) + // forgefmt: disable-next-item + c := add(iszero(c), xor(byte(and(0x1f, shr(byte(24, mul(0x02040810204081, shr(r, c))), + 0x8421084210842108cc6318c6db6d54be)), + 0x1819191a191d1a1b191d1c1d1a1b1c1e191a1d1a1c1c1b1e1a1a1c1b1f1f1f1f), shr(3, r))) o := add(min(sub(end, i), c), o) i := add(c, i) if iszero(lt(i, end)) { break } From 2ff3ecfaed47b2bd47b83bdaeef7bb74a20d3423 Mon Sep 17 00:00:00 2001 From: Vectorized Date: Thu, 24 Apr 2025 16:31:14 +0000 Subject: [PATCH 2/3] Opt --- src/utils/LibZip.sol | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/utils/LibZip.sol b/src/utils/LibZip.sol index 2aeb7ac067..cd7aa5a229 100644 --- a/src/utils/LibZip.sol +++ b/src/utils/LibZip.sol @@ -228,9 +228,6 @@ library LibZip { function cdDecompress(bytes memory data) internal pure returns (bytes memory result) { /// @solidity memory-safe-assembly assembly { - function min(x_, y_) -> _z { - _z := xor(x_, mul(xor(x_, y_), lt(y_, x_))) - } if mload(data) { result := mload(0x40) let s := add(data, 4) @@ -264,9 +261,11 @@ library LibZip { c := add(iszero(c), xor(byte(and(0x1f, shr(byte(24, mul(0x02040810204081, shr(r, c))), 0x8421084210842108cc6318c6db6d54be)), 0x1819191a191d1a1b191d1c1d1a1b1c1e191a1d1a1c1c1b1e1a1a1c1b1f1f1f1f), shr(3, r))) - o := add(min(sub(end, i), c), o) + o := add(c, o) i := add(c, i) - if iszero(lt(i, end)) { break } + if lt(i, end) { continue } + o := sub(o, mul(gt(i, end), sub(i, end))) + break } mstore(s, v) // Restore the first 4 bytes. mstore(result, sub(o, add(result, 0x20))) // Store the length. From 9a700a5e7c25bc04e1b38b4a2075107fc9507a51 Mon Sep 17 00:00:00 2001 From: Vectorized Date: Thu, 24 Apr 2025 16:38:27 +0000 Subject: [PATCH 3/3] Opt --- src/utils/LibZip.sol | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/utils/LibZip.sol b/src/utils/LibZip.sol index cd7aa5a229..a0e6bf6af0 100644 --- a/src/utils/LibZip.sol +++ b/src/utils/LibZip.sol @@ -261,10 +261,10 @@ library LibZip { c := add(iszero(c), xor(byte(and(0x1f, shr(byte(24, mul(0x02040810204081, shr(r, c))), 0x8421084210842108cc6318c6db6d54be)), 0x1819191a191d1a1b191d1c1d1a1b1c1e191a1d1a1c1c1b1e1a1a1c1b1f1f1f1f), shr(3, r))) - o := add(c, o) - i := add(c, i) + o := add(o, c) + i := add(i, c) if lt(i, end) { continue } - o := sub(o, mul(gt(i, end), sub(i, end))) + if gt(i, end) { o := sub(o, sub(i, end)) } break } mstore(s, v) // Restore the first 4 bytes.