Skip to content

Commit

Permalink
Optimize
Browse files Browse the repository at this point in the history
  • Loading branch information
Vectorized committed Aug 27, 2023
1 parent d95cfa9 commit 1b56d76
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 46 deletions.
27 changes: 13 additions & 14 deletions .gas-snapshot
Original file line number Diff line number Diff line change
Expand Up @@ -372,27 +372,26 @@ FixedPointMathLibTest:testSqrtHashedSingle() (gas: 53086)
FixedPointMathLibTest:testZeroFloorSub(uint256,uint256) (runs: 256, μ: 548, ~: 518)
FixedPointMathLibTest:testZeroFloorSubCasted(uint32,uint32,uint256) (runs: 256, μ: 905, ~: 945)
FixedPointMathLibTest:test__codesize() (gas: 19246)
JSONParserLibTest:testCodePointAsUTF8Differential(uint256) (runs: 256, μ: 682, ~: 682)
JSONParserLibTest:testDecodeInvalidStringReverts() (gas: 56186)
JSONParserLibTest:testDecodeString() (gas: 50945)
JSONParserLibTest:testDecodeString() (gas: 50960)
JSONParserLibTest:testParseEmptyArrays() (gas: 629602)
JSONParserLibTest:testParseEmptyObjects() (gas: 630563)
JSONParserLibTest:testParseGas() (gas: 140722)
JSONParserLibTest:testParseInt() (gas: 61236)
JSONParserLibTest:testParseGas() (gas: 140700)
JSONParserLibTest:testParseInt() (gas: 61302)
JSONParserLibTest:testParseInvalidIntReverts() (gas: 81128)
JSONParserLibTest:testParseInvalidNumberReverts() (gas: 2831108)
JSONParserLibTest:testParseInvalidReverts() (gas: 6614453)
JSONParserLibTest:testParseInvalidStringReverts() (gas: 719102)
JSONParserLibTest:testParseInvalidNumberReverts() (gas: 2828358)
JSONParserLibTest:testParseInvalidReverts() (gas: 6610031)
JSONParserLibTest:testParseInvalidStringReverts() (gas: 717957)
JSONParserLibTest:testParseInvalidUintReverts() (gas: 159600)
JSONParserLibTest:testParseNumber() (gas: 859021)
JSONParserLibTest:testParseObject() (gas: 51276)
JSONParserLibTest:testParseRecursiveObject() (gas: 97121)
JSONParserLibTest:testParseSimpleArray() (gas: 24793)
JSONParserLibTest:testParseSimpleUintArray() (gas: 1300571)
JSONParserLibTest:testParseSpecials() (gas: 301577)
JSONParserLibTest:testParseString() (gas: 681336)
JSONParserLibTest:testParseObject() (gas: 51254)
JSONParserLibTest:testParseRecursiveObject() (gas: 97099)
JSONParserLibTest:testParseSimpleArray() (gas: 24771)
JSONParserLibTest:testParseSimpleUintArray() (gas: 1300549)
JSONParserLibTest:testParseSpecials() (gas: 301555)
JSONParserLibTest:testParseString() (gas: 680874)
JSONParserLibTest:testParseUint() (gas: 25985)
JSONParserLibTest:test__codesize() (gas: 30451)
JSONParserLibTest:test__codesize() (gas: 30207)
LibBitTest:testAnd() (gas: 185677)
LibBitTest:testAnd(bool,bool) (runs: 256, μ: 668, ~: 667)
LibBitTest:testAutoClean(uint256,uint256) (runs: 256, μ: 446, ~: 446)
Expand Down
14 changes: 6 additions & 8 deletions src/utils/JSONParserLib.sol
Original file line number Diff line number Diff line change
Expand Up @@ -374,23 +374,21 @@ library JSONParserLib {
_pOut := add(pIn_, 1)
leave
}
mstore8(0x1f, c_)
mstore8(0x1e, shr(6, c_))
if iszero(gt(c_, 0x7ff)) {
mstore(pIn_, shl(240, or(0xc080, or(and(0x1f00, shl(2, c_)), and(0x3f, c_)))))
mstore(pIn_, shl(240, or(0xc080, and(0x1f3f, mload(0x00)))))
_pOut := add(pIn_, 2)
leave
}
mstore8(0x1d, shr(12, c_))
if iszero(gt(c_, 0xffff)) {
// forgefmt: disable-next-item
mstore(pIn_, shl(232, or(0xe08080, or(shl(4, and(0xf000, c_)),
or(and(0x3f00, shl(2, c_)), and(0x3f, c_))))))
mstore(pIn_, shl(232, or(0xe08080, and(0x0f3f3f, mload(0x00)))))
_pOut := add(pIn_, 3)
leave
}
mstore8(0x1c, shr(18, c_))
if iszero(gt(c_, 0x10ffff)) {
mstore8(0x1c, shr(18, c_))
mstore8(0x1d, shr(12, c_))
mstore8(0x1e, shr(6, c_))
mstore8(0x1f, c_)
mstore(pIn_, shl(224, or(0xf0808080, and(0x073f3f3f, mload(0x00)))))
_pOut := add(pIn_, 4)
}
Expand Down
24 changes: 0 additions & 24 deletions test/JSONParserLib.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -603,28 +603,4 @@ contract JSONParserLibTest is SoladyTest {
function decodeString(string memory s) public pure returns (string memory) {
return JSONParserLib.decodeString(s);
}
function testCodePointAsUTF8Differential(uint256 c) public {
uint256 a;
uint256 b;
/// @solidity memory-safe-assembly
assembly {
a := shl(240, or(0xc080, or(and(0x1f00, shl(2, c)), and(0x3f, c))))
mstore8(0, or(0xc0, and(0x1f, shr(6, c))))
mstore8(1, or(0x80, and(0x3f, c)))
mstore(0x02, 0)
b := mload(0x00)
}
assertEq(a, b);
/// @solidity memory-safe-assembly
assembly {
// forgefmt: disable-next-item
a := shl(232, or(0xe08080, or(shl(4, and(0xf000, c)), or(and(0x3f00, shl(2, c)), and(0x3f, c)))))
mstore8(0, shr(12, c))
mstore8(1, shr(6, c))
mstore8(2, c)
b := shl(232, or(0xe08080, and(0x0f3f3f, shr(232, mload(0x00)))))
}
assertEq(a, b);
}
}

0 comments on commit 1b56d76

Please sign in to comment.