Skip to content

Commit

Permalink
Optimize
Browse files Browse the repository at this point in the history
  • Loading branch information
Vectorized committed Aug 25, 2023
1 parent 2f4281b commit 6c3d345
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 8 deletions.
10 changes: 5 additions & 5 deletions .gas-snapshot
Original file line number Diff line number Diff line change
Expand Up @@ -375,17 +375,17 @@ FixedPointMathLibTest:test__codesize() (gas: 19246)
JSONParserLibTest:testParseEmptyArrays() (gas: 629291)
JSONParserLibTest:testParseEmptyObjects() (gas: 630253)
JSONParserLibTest:testParseGas() (gas: 140678)
JSONParserLibTest:testParseInvalidNumberReverts() (gas: 2628518)
JSONParserLibTest:testParseInvalidReverts() (gas: 5474144)
JSONParserLibTest:testParseInvalidNumberReverts() (gas: 2822224)
JSONParserLibTest:testParseInvalidReverts() (gas: 6600265)
JSONParserLibTest:testParseInvalidStringReverts() (gas: 715205)
JSONParserLibTest:testParseNumber() (gas: 777406)
JSONParserLibTest:testParseObject() (gas: 38717)
JSONParserLibTest:testParseNumber() (gas: 858633)
JSONParserLibTest:testParseObject() (gas: 51254)
JSONParserLibTest:testParseRecursiveObject() (gas: 97142)
JSONParserLibTest:testParseSimpleArray() (gas: 24771)
JSONParserLibTest:testParseSimpleUintArray() (gas: 1300495)
JSONParserLibTest:testParseSpecials() (gas: 301393)
JSONParserLibTest:testParseString() (gas: 679398)
JSONParserLibTest:test__codesize() (gas: 21283)
JSONParserLibTest:test__codesize() (gas: 21857)
LibBitTest:testAnd() (gas: 185677)
LibBitTest:testAnd(bool,bool) (runs: 256, μ: 668, ~: 667)
LibBitTest:testAutoClean(uint256,uint256) (runs: 256, μ: 446, ~: 446)
Expand Down
3 changes: 2 additions & 1 deletion src/utils/JSONParserLib.sol
Original file line number Diff line number Diff line change
Expand Up @@ -196,8 +196,9 @@ library JSONParserLib {
}
unchecked {
if (isObject(item)) {
Item[] memory r = children(item);
bytes32 kHash = keccak256(bytes(k));
Item[] memory r = children(item);
// We'll just do a linear search. The alternatives are very bloated.
for (uint256 i = r.length << 5; i != 0; i -= 0x20) {
/// @solidity memory-safe-assembly
assembly {
Expand Down
14 changes: 12 additions & 2 deletions test/JSONParserLib.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ contract JSONParserLibTest is SoladyTest {
_checkParseReverts("[][]");
_checkParseReverts("[],[]");
_checkParseReverts("[1,2");
_checkParseReverts("1,2]");
_checkParseReverts("[1");
_checkParseReverts("1]");
_checkParseReverts("[1,");
_checkParseReverts("{}{");
_checkParseReverts("{}{}");
Expand All @@ -45,6 +47,8 @@ contract JSONParserLibTest is SoladyTest {
_checkParseReverts('{,"a":"A","b":"B"}');
_checkParseReverts('{"a"::"A"}');
_checkParseReverts('{"a","A"}');
_checkParseReverts("{1}");
_checkParseReverts("{:}");
}

function testParseInvalidNumberReverts() public {
Expand All @@ -70,6 +74,7 @@ contract JSONParserLibTest is SoladyTest {
_checkParseReverts("+123");
_checkParseReverts(".123");
_checkParseReverts("e123");
_checkParseReverts("1 e 1");
_checkParseReverts("-1.e+0");
_checkParseReverts("0x");
}
Expand Down Expand Up @@ -100,6 +105,7 @@ contract JSONParserLibTest is SoladyTest {
_checkParseNumber("0.1");
_checkParseNumber("0.123");
_checkParseNumber("12345678901234567890123456789012345678901234567890");
_checkParseNumber("12345e12345678901234567890123456789012345678901234567890");
_checkParseNumber("1234567890");
_checkParseNumber("123");
_checkParseNumber("1");
Expand Down Expand Up @@ -316,15 +322,19 @@ contract JSONParserLibTest is SoladyTest {
string memory s;
JSONParserLib.Item memory item;

s = '{"b": "B", "_": "x", "hehe": "HEHE", "_": "y", "_": "z"}';
s = '{"b": "B", "_": "x", "hehe": "HEHE", "_": "y", "v": 12345, "_": "z"}';
item = s.parse();

assertEq(item.size(), 5);
assertEq(item.isObject(), true);
assertEq(item.size(), 6);

for (uint256 i; i < item.size(); ++i) {
assertEq(item.atIndex(i).isUndefined(), true);
assertEq(item.children()[i].parent()._data, item._data);
}
assertEq(item.atKey('"_"').value(), '"z"');
assertEq(item.atKey('"b"').value(), '"B"');
assertEq(item.atKey('"v"').value(), "12345");
assertEq(item.atKey('"hehe"').value(), '"HEHE"');
assertEq(item.atKey('"m"').value(), "");
assertEq(item.atKey('"m"').isUndefined(), true);
Expand Down

0 comments on commit 6c3d345

Please sign in to comment.