From ac5db6ea016e96a99bc2211950c4648b06dff0b6 Mon Sep 17 00:00:00 2001 From: Fraser Scott Date: Tue, 9 May 2023 23:34:32 +0200 Subject: [PATCH 01/15] feat: add gas reports for setting 10 and 100 keys --- packages/world/gas-report.json | 50 ++++++++++++++- packages/world/test/KeysWithValueModule.t.sol | 64 +++++++++++++++++++ 2 files changed, 113 insertions(+), 1 deletion(-) diff --git a/packages/world/gas-report.json b/packages/world/gas-report.json index f1858c0e44..3fff481dbd 100644 --- a/packages/world/gas-report.json +++ b/packages/world/gas-report.json @@ -39,7 +39,55 @@ "source": "test/KeysWithValueModule.t.sol", "name": "Get list of keys with a given value", "functionCall": "bytes32[] memory keysWithValue = getKeysWithValue(world, sourceTableId, abi.encode(value1))", - "gasUsed": 7658 + "gasUsed": 7688 + }, + { + "source": "test/KeysWithValueModule.t.sol", + "name": "install keys with value module", + "functionCall": "world.installRootModule(keysWithValueModule, abi.encode(sourceTableId))", + "gasUsed": 609250 + }, + { + "source": "test/KeysWithValueModule.t.sol", + "name": "Setting the first of 10 keys", + "functionCall": "world.setRecord(namespace, sourceName, firstKey, abi.encodePacked(value))", + "gasUsed": 141550 + }, + { + "source": "test/KeysWithValueModule.t.sol", + "name": "Setting the last of 10 keys", + "functionCall": "world.setRecord(namespace, sourceName, lastKey, abi.encodePacked(value))", + "gasUsed": 134842 + }, + { + "source": "test/KeysWithValueModule.t.sol", + "name": "Get list of 10 keys with a given value", + "functionCall": "bytes32[] memory keysWithValue = getKeysWithValue(world, sourceTableId, abi.encode(value))", + "gasUsed": 12162 + }, + { + "source": "test/KeysWithValueModule.t.sol", + "name": "install keys with value module", + "functionCall": "world.installRootModule(keysWithValueModule, abi.encode(sourceTableId))", + "gasUsed": 609250 + }, + { + "source": "test/KeysWithValueModule.t.sol", + "name": "Setting the first of 100 keys", + "functionCall": "world.setRecord(namespace, sourceName, firstKey, abi.encodePacked(value))", + "gasUsed": 141550 + }, + { + "source": "test/KeysWithValueModule.t.sol", + "name": "Setting the last of 100 keys", + "functionCall": "world.setRecord(namespace, sourceName, lastKey, abi.encodePacked(value))", + "gasUsed": 192968 + }, + { + "source": "test/KeysWithValueModule.t.sol", + "name": "Get list of 100 keys with a given value", + "functionCall": "bytes32[] memory keysWithValue = getKeysWithValue(world, sourceTableId, abi.encode(value))", + "gasUsed": 57163 }, { "source": "test/KeysWithValueModule.t.sol", diff --git a/packages/world/test/KeysWithValueModule.t.sol b/packages/world/test/KeysWithValueModule.t.sol index 907fba2b76..c59f846240 100644 --- a/packages/world/test/KeysWithValueModule.t.sol +++ b/packages/world/test/KeysWithValueModule.t.sol @@ -208,4 +208,68 @@ contract KeysWithValueModuleTest is Test { assertEq(keysWithValue[0], key1); assertEq(keysWithValue[1], key2); } + + function testGetKeysWithValueMany10() public { + _installKeysWithValueModule(); + + uint256 AMOUNT = 10; + uint256 value = 1; + + bytes32[] memory firstKey = new bytes32[](1); + firstKey[0] = bytes32(0); + // !gasreport Setting the first of 10 keys + world.setRecord(namespace, sourceName, firstKey, abi.encodePacked(value)); + + for (uint256 i = 1; i < AMOUNT - 1; i++) { + bytes32[] memory key = new bytes32[](1); + key[0] = bytes32(i); + world.setRecord(namespace, sourceName, key, abi.encodePacked(value)); + } + + bytes32[] memory lastKey = new bytes32[](1); + lastKey[0] = bytes32(uint256(AMOUNT - 1)); + // !gasreport Setting the last of 10 keys + world.setRecord(namespace, sourceName, lastKey, abi.encodePacked(value)); + + // !gasreport Get list of 10 keys with a given value + bytes32[] memory keysWithValue = getKeysWithValue(world, sourceTableId, abi.encode(value)); + + // Get the list of keys with value1 from the target table + keysWithValue = getKeysWithValue(world, sourceTableId, abi.encode(value)); + + // Assert that the list is correct + assertEq(keysWithValue.length, AMOUNT); + } + + function testGetKeysWithValueMany100() public { + _installKeysWithValueModule(); + + uint256 AMOUNT = 100; + uint256 value = 1; + + bytes32[] memory firstKey = new bytes32[](1); + firstKey[0] = bytes32(0); + // !gasreport Setting the first of 100 keys + world.setRecord(namespace, sourceName, firstKey, abi.encodePacked(value)); + + for (uint256 i = 1; i < AMOUNT - 1; i++) { + bytes32[] memory key = new bytes32[](1); + key[0] = bytes32(i); + world.setRecord(namespace, sourceName, key, abi.encodePacked(value)); + } + + bytes32[] memory lastKey = new bytes32[](1); + lastKey[0] = bytes32(uint256(AMOUNT - 1)); + // !gasreport Setting the last of 100 keys + world.setRecord(namespace, sourceName, lastKey, abi.encodePacked(value)); + + // !gasreport Get list of 100 keys with a given value + bytes32[] memory keysWithValue = getKeysWithValue(world, sourceTableId, abi.encode(value)); + + // Get the list of keys with value1 from the target table + keysWithValue = getKeysWithValue(world, sourceTableId, abi.encode(value)); + + // Assert that the list is correct + assertEq(keysWithValue.length, AMOUNT); + } } From 85b5dc7e60ab7a3f9f120d15b153539de2b671b2 Mon Sep 17 00:00:00 2001 From: Fraser Scott Date: Tue, 9 May 2023 23:37:25 +0200 Subject: [PATCH 02/15] test: add gas report for 1000 keys --- packages/world/gas-report.json | 26 ++++++++++++++- packages/world/test/KeysWithValueModule.t.sol | 32 +++++++++++++++++++ 2 files changed, 57 insertions(+), 1 deletion(-) diff --git a/packages/world/gas-report.json b/packages/world/gas-report.json index 3fff481dbd..0f75861c17 100644 --- a/packages/world/gas-report.json +++ b/packages/world/gas-report.json @@ -39,7 +39,7 @@ "source": "test/KeysWithValueModule.t.sol", "name": "Get list of keys with a given value", "functionCall": "bytes32[] memory keysWithValue = getKeysWithValue(world, sourceTableId, abi.encode(value1))", - "gasUsed": 7688 + "gasUsed": 7699 }, { "source": "test/KeysWithValueModule.t.sol", @@ -89,6 +89,30 @@ "functionCall": "bytes32[] memory keysWithValue = getKeysWithValue(world, sourceTableId, abi.encode(value))", "gasUsed": 57163 }, + { + "source": "test/KeysWithValueModule.t.sol", + "name": "install keys with value module", + "functionCall": "world.installRootModule(keysWithValueModule, abi.encode(sourceTableId))", + "gasUsed": 609250 + }, + { + "source": "test/KeysWithValueModule.t.sol", + "name": "Setting the first of 1000 keys", + "functionCall": "world.setRecord(namespace, sourceName, firstKey, abi.encodePacked(value))", + "gasUsed": 141550 + }, + { + "source": "test/KeysWithValueModule.t.sol", + "name": "Setting the last of 1000 keys", + "functionCall": "world.setRecord(namespace, sourceName, lastKey, abi.encodePacked(value))", + "gasUsed": 790975 + }, + { + "source": "test/KeysWithValueModule.t.sol", + "name": "Get list of 1000 keys with a given value", + "functionCall": "bytes32[] memory keysWithValue = getKeysWithValue(world, sourceTableId, abi.encode(value))", + "gasUsed": 571583 + }, { "source": "test/KeysWithValueModule.t.sol", "name": "compute the target table selector", diff --git a/packages/world/test/KeysWithValueModule.t.sol b/packages/world/test/KeysWithValueModule.t.sol index c59f846240..278cc5836d 100644 --- a/packages/world/test/KeysWithValueModule.t.sol +++ b/packages/world/test/KeysWithValueModule.t.sol @@ -272,4 +272,36 @@ contract KeysWithValueModuleTest is Test { // Assert that the list is correct assertEq(keysWithValue.length, AMOUNT); } + + function testGetKeysWithValueMany1000() public { + _installKeysWithValueModule(); + + uint256 AMOUNT = 1000; + uint256 value = 1; + + bytes32[] memory firstKey = new bytes32[](1); + firstKey[0] = bytes32(0); + // !gasreport Setting the first of 1000 keys + world.setRecord(namespace, sourceName, firstKey, abi.encodePacked(value)); + + for (uint256 i = 1; i < AMOUNT - 1; i++) { + bytes32[] memory key = new bytes32[](1); + key[0] = bytes32(i); + world.setRecord(namespace, sourceName, key, abi.encodePacked(value)); + } + + bytes32[] memory lastKey = new bytes32[](1); + lastKey[0] = bytes32(uint256(AMOUNT - 1)); + // !gasreport Setting the last of 1000 keys + world.setRecord(namespace, sourceName, lastKey, abi.encodePacked(value)); + + // !gasreport Get list of 1000 keys with a given value + bytes32[] memory keysWithValue = getKeysWithValue(world, sourceTableId, abi.encode(value)); + + // Get the list of keys with value1 from the target table + keysWithValue = getKeysWithValue(world, sourceTableId, abi.encode(value)); + + // Assert that the list is correct + assertEq(keysWithValue.length, AMOUNT); + } } From f8f16d29b69546937f74f1404bb9c4a7f64957f4 Mon Sep 17 00:00:00 2001 From: Fraser Scott Date: Wed, 10 May 2023 13:24:13 +0200 Subject: [PATCH 03/15] test: add 10,100,1000 gas reports for keysInTable --- packages/world/gas-report.json | 78 +++++++++++++++- packages/world/test/KeysInTableModule.t.sol | 90 +++++++++++++++++++ packages/world/test/KeysWithValueModule.t.sol | 42 ++++----- 3 files changed, 183 insertions(+), 27 deletions(-) diff --git a/packages/world/gas-report.json b/packages/world/gas-report.json index 0f75861c17..42baced15f 100644 --- a/packages/world/gas-report.json +++ b/packages/world/gas-report.json @@ -1,4 +1,76 @@ [ + { + "source": "test/KeysInTableModule.t.sol", + "name": "install keys in table module", + "functionCall": "world.installRootModule(keysInTableModule, abi.encode(sourceTableId))", + "gasUsed": 1032425 + }, + { + "source": "test/KeysInTableModule.t.sol", + "name": "Setting the first of 10 keys", + "functionCall": "world.setRecord(namespace, sourceFile, firstKey, abi.encodePacked(value))", + "gasUsed": 213599 + }, + { + "source": "test/KeysInTableModule.t.sol", + "name": "Setting the last of 10 keys", + "functionCall": "world.setRecord(namespace, sourceFile, lastKey, abi.encodePacked(value))", + "gasUsed": 184985 + }, + { + "source": "test/KeysInTableModule.t.sol", + "name": "Get list of 10 keys with a given value", + "functionCall": "bytes32[][] memory keyTuples = getKeysInTable(world, sourceTableId)", + "gasUsed": 17036 + }, + { + "source": "test/KeysInTableModule.t.sol", + "name": "install keys in table module", + "functionCall": "world.installRootModule(keysInTableModule, abi.encode(sourceTableId))", + "gasUsed": 1032425 + }, + { + "source": "test/KeysInTableModule.t.sol", + "name": "Setting the first of 100 keys", + "functionCall": "world.setRecord(namespace, sourceFile, firstKey, abi.encodePacked(value))", + "gasUsed": 213599 + }, + { + "source": "test/KeysInTableModule.t.sol", + "name": "Setting the last of 100 keys", + "functionCall": "world.setRecord(namespace, sourceFile, lastKey, abi.encodePacked(value))", + "gasUsed": 243107 + }, + { + "source": "test/KeysInTableModule.t.sol", + "name": "Get list of 100 keys with a given value", + "functionCall": "bytes32[][] memory keyTuples = getKeysInTable(world, sourceTableId)", + "gasUsed": 106205 + }, + { + "source": "test/KeysInTableModule.t.sol", + "name": "install keys in table module", + "functionCall": "world.installRootModule(keysInTableModule, abi.encode(sourceTableId))", + "gasUsed": 1032425 + }, + { + "source": "test/KeysInTableModule.t.sol", + "name": "Setting the first of 1000 keys", + "functionCall": "world.setRecord(namespace, sourceFile, firstKey, abi.encodePacked(value))", + "gasUsed": 213599 + }, + { + "source": "test/KeysInTableModule.t.sol", + "name": "Setting the last of 1000 keys", + "functionCall": "world.setRecord(namespace, sourceFile, lastKey, abi.encodePacked(value))", + "gasUsed": 841086 + }, + { + "source": "test/KeysInTableModule.t.sol", + "name": "Get list of 1000 keys with a given value", + "functionCall": "bytes32[][] memory keyTuples = getKeysInTable(world, sourceTableId)", + "gasUsed": 1151042 + }, { "source": "test/KeysInTableModule.t.sol", "name": "install keys in table module", @@ -57,7 +129,7 @@ "source": "test/KeysWithValueModule.t.sol", "name": "Setting the last of 10 keys", "functionCall": "world.setRecord(namespace, sourceName, lastKey, abi.encodePacked(value))", - "gasUsed": 134842 + "gasUsed": 134836 }, { "source": "test/KeysWithValueModule.t.sol", @@ -81,7 +153,7 @@ "source": "test/KeysWithValueModule.t.sol", "name": "Setting the last of 100 keys", "functionCall": "world.setRecord(namespace, sourceName, lastKey, abi.encodePacked(value))", - "gasUsed": 192968 + "gasUsed": 192958 }, { "source": "test/KeysWithValueModule.t.sol", @@ -105,7 +177,7 @@ "source": "test/KeysWithValueModule.t.sol", "name": "Setting the last of 1000 keys", "functionCall": "world.setRecord(namespace, sourceName, lastKey, abi.encodePacked(value))", - "gasUsed": 790975 + "gasUsed": 790937 }, { "source": "test/KeysWithValueModule.t.sol", diff --git a/packages/world/test/KeysInTableModule.t.sol b/packages/world/test/KeysInTableModule.t.sol index 90dbe6c95f..ee5f82f510 100644 --- a/packages/world/test/KeysInTableModule.t.sol +++ b/packages/world/test/KeysInTableModule.t.sol @@ -293,4 +293,94 @@ contract KeysInTableModuleTest is Test { // Assert that the key tuple is in the source table assertTrue(hasKey(world, sourceTableId, keyTuple)); } + + function testGetKeysWithValueMany10() public { + _installKeysInTableModule(); + + uint256 AMOUNT = 10; + uint256 value = 1; + + bytes32[] memory firstKey = new bytes32[](1); + firstKey[0] = bytes32(0); + bytes32[] memory lastKey = new bytes32[](1); + lastKey[0] = bytes32(uint256(AMOUNT - 1)); + + // !gasreport Setting the first of 10 keys + world.setRecord(namespace, sourceFile, firstKey, abi.encodePacked(value)); + + for (uint256 i = 1; i < AMOUNT - 1; i++) { + bytes32[] memory key = new bytes32[](1); + key[0] = bytes32(i); + world.setRecord(namespace, sourceFile, key, abi.encodePacked(value)); + } + + // !gasreport Setting the last of 10 keys + world.setRecord(namespace, sourceFile, lastKey, abi.encodePacked(value)); + + // !gasreport Get list of 10 keys with a given value + bytes32[][] memory keyTuples = getKeysInTable(world, sourceTableId); + + // Assert that the list is correct + assertEq(keyTuples.length, AMOUNT); + } + + function testGetKeysWithValueMany100() public { + _installKeysInTableModule(); + + uint256 AMOUNT = 100; + uint256 value = 1; + + bytes32[] memory firstKey = new bytes32[](1); + firstKey[0] = bytes32(0); + bytes32[] memory lastKey = new bytes32[](1); + lastKey[0] = bytes32(uint256(AMOUNT - 1)); + + // !gasreport Setting the first of 100 keys + world.setRecord(namespace, sourceFile, firstKey, abi.encodePacked(value)); + + for (uint256 i = 1; i < AMOUNT - 1; i++) { + bytes32[] memory key = new bytes32[](1); + key[0] = bytes32(i); + world.setRecord(namespace, sourceFile, key, abi.encodePacked(value)); + } + + // !gasreport Setting the last of 100 keys + world.setRecord(namespace, sourceFile, lastKey, abi.encodePacked(value)); + + // !gasreport Get list of 100 keys with a given value + bytes32[][] memory keyTuples = getKeysInTable(world, sourceTableId); + + // Assert that the list is correct + assertEq(keyTuples.length, AMOUNT); + } + + function testGetKeysWithValueMany1000() public { + _installKeysInTableModule(); + + uint256 AMOUNT = 1000; + uint256 value = 1; + + bytes32[] memory firstKey = new bytes32[](1); + firstKey[0] = bytes32(0); + bytes32[] memory lastKey = new bytes32[](1); + lastKey[0] = bytes32(uint256(AMOUNT - 1)); + + // !gasreport Setting the first of 1000 keys + world.setRecord(namespace, sourceFile, firstKey, abi.encodePacked(value)); + + for (uint256 i = 1; i < AMOUNT - 1; i++) { + bytes32[] memory key = new bytes32[](1); + key[0] = bytes32(i); + world.setRecord(namespace, sourceFile, key, abi.encodePacked(value)); + } + + // !gasreport Setting the last of 1000 keys + world.setRecord(namespace, sourceFile, lastKey, abi.encodePacked(value)); + + // !gasreport Get list of 1000 keys with a given value + bytes32[][] memory keyTuples = getKeysInTable(world, sourceTableId); + + // Assert that the list is correct + assertEq(keyTuples.length, AMOUNT); + } } diff --git a/packages/world/test/KeysWithValueModule.t.sol b/packages/world/test/KeysWithValueModule.t.sol index 278cc5836d..a47e90ec1e 100644 --- a/packages/world/test/KeysWithValueModule.t.sol +++ b/packages/world/test/KeysWithValueModule.t.sol @@ -212,96 +212,90 @@ contract KeysWithValueModuleTest is Test { function testGetKeysWithValueMany10() public { _installKeysWithValueModule(); - uint256 AMOUNT = 10; + uint256 amount = 10; uint256 value = 1; bytes32[] memory firstKey = new bytes32[](1); firstKey[0] = bytes32(0); + bytes32[] memory lastKey = new bytes32[](1); + lastKey[0] = bytes32(uint256(amount - 1)); + // !gasreport Setting the first of 10 keys world.setRecord(namespace, sourceName, firstKey, abi.encodePacked(value)); - for (uint256 i = 1; i < AMOUNT - 1; i++) { + for (uint256 i = 1; i < amount - 1; i++) { bytes32[] memory key = new bytes32[](1); key[0] = bytes32(i); world.setRecord(namespace, sourceName, key, abi.encodePacked(value)); } - bytes32[] memory lastKey = new bytes32[](1); - lastKey[0] = bytes32(uint256(AMOUNT - 1)); // !gasreport Setting the last of 10 keys world.setRecord(namespace, sourceName, lastKey, abi.encodePacked(value)); // !gasreport Get list of 10 keys with a given value bytes32[] memory keysWithValue = getKeysWithValue(world, sourceTableId, abi.encode(value)); - // Get the list of keys with value1 from the target table - keysWithValue = getKeysWithValue(world, sourceTableId, abi.encode(value)); - // Assert that the list is correct - assertEq(keysWithValue.length, AMOUNT); + assertEq(keysWithValue.length, amount); } function testGetKeysWithValueMany100() public { _installKeysWithValueModule(); - uint256 AMOUNT = 100; + uint256 amount = 100; uint256 value = 1; bytes32[] memory firstKey = new bytes32[](1); firstKey[0] = bytes32(0); + bytes32[] memory lastKey = new bytes32[](1); + lastKey[0] = bytes32(uint256(amount - 1)); + // !gasreport Setting the first of 100 keys world.setRecord(namespace, sourceName, firstKey, abi.encodePacked(value)); - for (uint256 i = 1; i < AMOUNT - 1; i++) { + for (uint256 i = 1; i < amount - 1; i++) { bytes32[] memory key = new bytes32[](1); key[0] = bytes32(i); world.setRecord(namespace, sourceName, key, abi.encodePacked(value)); } - bytes32[] memory lastKey = new bytes32[](1); - lastKey[0] = bytes32(uint256(AMOUNT - 1)); // !gasreport Setting the last of 100 keys world.setRecord(namespace, sourceName, lastKey, abi.encodePacked(value)); // !gasreport Get list of 100 keys with a given value bytes32[] memory keysWithValue = getKeysWithValue(world, sourceTableId, abi.encode(value)); - // Get the list of keys with value1 from the target table - keysWithValue = getKeysWithValue(world, sourceTableId, abi.encode(value)); - // Assert that the list is correct - assertEq(keysWithValue.length, AMOUNT); + assertEq(keysWithValue.length, amount); } function testGetKeysWithValueMany1000() public { _installKeysWithValueModule(); - uint256 AMOUNT = 1000; + uint256 amount = 1000; uint256 value = 1; bytes32[] memory firstKey = new bytes32[](1); firstKey[0] = bytes32(0); + bytes32[] memory lastKey = new bytes32[](1); + lastKey[0] = bytes32(uint256(amount - 1)); + // !gasreport Setting the first of 1000 keys world.setRecord(namespace, sourceName, firstKey, abi.encodePacked(value)); - for (uint256 i = 1; i < AMOUNT - 1; i++) { + for (uint256 i = 1; i < amount - 1; i++) { bytes32[] memory key = new bytes32[](1); key[0] = bytes32(i); world.setRecord(namespace, sourceName, key, abi.encodePacked(value)); } - bytes32[] memory lastKey = new bytes32[](1); - lastKey[0] = bytes32(uint256(AMOUNT - 1)); // !gasreport Setting the last of 1000 keys world.setRecord(namespace, sourceName, lastKey, abi.encodePacked(value)); // !gasreport Get list of 1000 keys with a given value bytes32[] memory keysWithValue = getKeysWithValue(world, sourceTableId, abi.encode(value)); - // Get the list of keys with value1 from the target table - keysWithValue = getKeysWithValue(world, sourceTableId, abi.encode(value)); - // Assert that the list is correct - assertEq(keysWithValue.length, AMOUNT); + assertEq(keysWithValue.length, amount); } } From 6577377347b7c0d3b0f481d8ea69e2c72a442905 Mon Sep 17 00:00:00 2001 From: Fraser Scott Date: Wed, 10 May 2023 13:28:34 +0200 Subject: [PATCH 04/15] test: remove first key gas report --- packages/world/gas-report.json | 56 ++++--------------- packages/world/test/KeysInTableModule.t.sol | 21 +------ packages/world/test/KeysWithValueModule.t.sol | 21 +------ 3 files changed, 16 insertions(+), 82 deletions(-) diff --git a/packages/world/gas-report.json b/packages/world/gas-report.json index 42baced15f..8fff5185ac 100644 --- a/packages/world/gas-report.json +++ b/packages/world/gas-report.json @@ -5,23 +5,17 @@ "functionCall": "world.installRootModule(keysInTableModule, abi.encode(sourceTableId))", "gasUsed": 1032425 }, - { - "source": "test/KeysInTableModule.t.sol", - "name": "Setting the first of 10 keys", - "functionCall": "world.setRecord(namespace, sourceFile, firstKey, abi.encodePacked(value))", - "gasUsed": 213599 - }, { "source": "test/KeysInTableModule.t.sol", "name": "Setting the last of 10 keys", "functionCall": "world.setRecord(namespace, sourceFile, lastKey, abi.encodePacked(value))", - "gasUsed": 184985 + "gasUsed": 184984 }, { "source": "test/KeysInTableModule.t.sol", "name": "Get list of 10 keys with a given value", "functionCall": "bytes32[][] memory keyTuples = getKeysInTable(world, sourceTableId)", - "gasUsed": 17036 + "gasUsed": 17032 }, { "source": "test/KeysInTableModule.t.sol", @@ -29,12 +23,6 @@ "functionCall": "world.installRootModule(keysInTableModule, abi.encode(sourceTableId))", "gasUsed": 1032425 }, - { - "source": "test/KeysInTableModule.t.sol", - "name": "Setting the first of 100 keys", - "functionCall": "world.setRecord(namespace, sourceFile, firstKey, abi.encodePacked(value))", - "gasUsed": 213599 - }, { "source": "test/KeysInTableModule.t.sol", "name": "Setting the last of 100 keys", @@ -45,7 +33,7 @@ "source": "test/KeysInTableModule.t.sol", "name": "Get list of 100 keys with a given value", "functionCall": "bytes32[][] memory keyTuples = getKeysInTable(world, sourceTableId)", - "gasUsed": 106205 + "gasUsed": 106170 }, { "source": "test/KeysInTableModule.t.sol", @@ -53,23 +41,17 @@ "functionCall": "world.installRootModule(keysInTableModule, abi.encode(sourceTableId))", "gasUsed": 1032425 }, - { - "source": "test/KeysInTableModule.t.sol", - "name": "Setting the first of 1000 keys", - "functionCall": "world.setRecord(namespace, sourceFile, firstKey, abi.encodePacked(value))", - "gasUsed": 213599 - }, { "source": "test/KeysInTableModule.t.sol", "name": "Setting the last of 1000 keys", "functionCall": "world.setRecord(namespace, sourceFile, lastKey, abi.encodePacked(value))", - "gasUsed": 841086 + "gasUsed": 841087 }, { "source": "test/KeysInTableModule.t.sol", "name": "Get list of 1000 keys with a given value", "functionCall": "bytes32[][] memory keyTuples = getKeysInTable(world, sourceTableId)", - "gasUsed": 1151042 + "gasUsed": 1150690 }, { "source": "test/KeysInTableModule.t.sol", @@ -119,23 +101,17 @@ "functionCall": "world.installRootModule(keysWithValueModule, abi.encode(sourceTableId))", "gasUsed": 609250 }, - { - "source": "test/KeysWithValueModule.t.sol", - "name": "Setting the first of 10 keys", - "functionCall": "world.setRecord(namespace, sourceName, firstKey, abi.encodePacked(value))", - "gasUsed": 141550 - }, { "source": "test/KeysWithValueModule.t.sol", "name": "Setting the last of 10 keys", "functionCall": "world.setRecord(namespace, sourceName, lastKey, abi.encodePacked(value))", - "gasUsed": 134836 + "gasUsed": 134835 }, { "source": "test/KeysWithValueModule.t.sol", "name": "Get list of 10 keys with a given value", "functionCall": "bytes32[] memory keysWithValue = getKeysWithValue(world, sourceTableId, abi.encode(value))", - "gasUsed": 12162 + "gasUsed": 12160 }, { "source": "test/KeysWithValueModule.t.sol", @@ -143,12 +119,6 @@ "functionCall": "world.installRootModule(keysWithValueModule, abi.encode(sourceTableId))", "gasUsed": 609250 }, - { - "source": "test/KeysWithValueModule.t.sol", - "name": "Setting the first of 100 keys", - "functionCall": "world.setRecord(namespace, sourceName, firstKey, abi.encodePacked(value))", - "gasUsed": 141550 - }, { "source": "test/KeysWithValueModule.t.sol", "name": "Setting the last of 100 keys", @@ -159,7 +129,7 @@ "source": "test/KeysWithValueModule.t.sol", "name": "Get list of 100 keys with a given value", "functionCall": "bytes32[] memory keysWithValue = getKeysWithValue(world, sourceTableId, abi.encode(value))", - "gasUsed": 57163 + "gasUsed": 57149 }, { "source": "test/KeysWithValueModule.t.sol", @@ -167,23 +137,17 @@ "functionCall": "world.installRootModule(keysWithValueModule, abi.encode(sourceTableId))", "gasUsed": 609250 }, - { - "source": "test/KeysWithValueModule.t.sol", - "name": "Setting the first of 1000 keys", - "functionCall": "world.setRecord(namespace, sourceName, firstKey, abi.encodePacked(value))", - "gasUsed": 141550 - }, { "source": "test/KeysWithValueModule.t.sol", "name": "Setting the last of 1000 keys", "functionCall": "world.setRecord(namespace, sourceName, lastKey, abi.encodePacked(value))", - "gasUsed": 790937 + "gasUsed": 790938 }, { "source": "test/KeysWithValueModule.t.sol", "name": "Get list of 1000 keys with a given value", "functionCall": "bytes32[] memory keysWithValue = getKeysWithValue(world, sourceTableId, abi.encode(value))", - "gasUsed": 571583 + "gasUsed": 571426 }, { "source": "test/KeysWithValueModule.t.sol", diff --git a/packages/world/test/KeysInTableModule.t.sol b/packages/world/test/KeysInTableModule.t.sol index ee5f82f510..c7c0e1f216 100644 --- a/packages/world/test/KeysInTableModule.t.sol +++ b/packages/world/test/KeysInTableModule.t.sol @@ -300,15 +300,10 @@ contract KeysInTableModuleTest is Test { uint256 AMOUNT = 10; uint256 value = 1; - bytes32[] memory firstKey = new bytes32[](1); - firstKey[0] = bytes32(0); bytes32[] memory lastKey = new bytes32[](1); lastKey[0] = bytes32(uint256(AMOUNT - 1)); - // !gasreport Setting the first of 10 keys - world.setRecord(namespace, sourceFile, firstKey, abi.encodePacked(value)); - - for (uint256 i = 1; i < AMOUNT - 1; i++) { + for (uint256 i; i < AMOUNT - 1; i++) { bytes32[] memory key = new bytes32[](1); key[0] = bytes32(i); world.setRecord(namespace, sourceFile, key, abi.encodePacked(value)); @@ -330,15 +325,10 @@ contract KeysInTableModuleTest is Test { uint256 AMOUNT = 100; uint256 value = 1; - bytes32[] memory firstKey = new bytes32[](1); - firstKey[0] = bytes32(0); bytes32[] memory lastKey = new bytes32[](1); lastKey[0] = bytes32(uint256(AMOUNT - 1)); - // !gasreport Setting the first of 100 keys - world.setRecord(namespace, sourceFile, firstKey, abi.encodePacked(value)); - - for (uint256 i = 1; i < AMOUNT - 1; i++) { + for (uint256 i; i < AMOUNT - 1; i++) { bytes32[] memory key = new bytes32[](1); key[0] = bytes32(i); world.setRecord(namespace, sourceFile, key, abi.encodePacked(value)); @@ -360,15 +350,10 @@ contract KeysInTableModuleTest is Test { uint256 AMOUNT = 1000; uint256 value = 1; - bytes32[] memory firstKey = new bytes32[](1); - firstKey[0] = bytes32(0); bytes32[] memory lastKey = new bytes32[](1); lastKey[0] = bytes32(uint256(AMOUNT - 1)); - // !gasreport Setting the first of 1000 keys - world.setRecord(namespace, sourceFile, firstKey, abi.encodePacked(value)); - - for (uint256 i = 1; i < AMOUNT - 1; i++) { + for (uint256 i; i < AMOUNT - 1; i++) { bytes32[] memory key = new bytes32[](1); key[0] = bytes32(i); world.setRecord(namespace, sourceFile, key, abi.encodePacked(value)); diff --git a/packages/world/test/KeysWithValueModule.t.sol b/packages/world/test/KeysWithValueModule.t.sol index a47e90ec1e..d2256a3e55 100644 --- a/packages/world/test/KeysWithValueModule.t.sol +++ b/packages/world/test/KeysWithValueModule.t.sol @@ -215,15 +215,10 @@ contract KeysWithValueModuleTest is Test { uint256 amount = 10; uint256 value = 1; - bytes32[] memory firstKey = new bytes32[](1); - firstKey[0] = bytes32(0); bytes32[] memory lastKey = new bytes32[](1); lastKey[0] = bytes32(uint256(amount - 1)); - // !gasreport Setting the first of 10 keys - world.setRecord(namespace, sourceName, firstKey, abi.encodePacked(value)); - - for (uint256 i = 1; i < amount - 1; i++) { + for (uint256 i; i < amount - 1; i++) { bytes32[] memory key = new bytes32[](1); key[0] = bytes32(i); world.setRecord(namespace, sourceName, key, abi.encodePacked(value)); @@ -245,15 +240,10 @@ contract KeysWithValueModuleTest is Test { uint256 amount = 100; uint256 value = 1; - bytes32[] memory firstKey = new bytes32[](1); - firstKey[0] = bytes32(0); bytes32[] memory lastKey = new bytes32[](1); lastKey[0] = bytes32(uint256(amount - 1)); - // !gasreport Setting the first of 100 keys - world.setRecord(namespace, sourceName, firstKey, abi.encodePacked(value)); - - for (uint256 i = 1; i < amount - 1; i++) { + for (uint256 i; i < amount - 1; i++) { bytes32[] memory key = new bytes32[](1); key[0] = bytes32(i); world.setRecord(namespace, sourceName, key, abi.encodePacked(value)); @@ -275,15 +265,10 @@ contract KeysWithValueModuleTest is Test { uint256 amount = 1000; uint256 value = 1; - bytes32[] memory firstKey = new bytes32[](1); - firstKey[0] = bytes32(0); bytes32[] memory lastKey = new bytes32[](1); lastKey[0] = bytes32(uint256(amount - 1)); - // !gasreport Setting the first of 1000 keys - world.setRecord(namespace, sourceName, firstKey, abi.encodePacked(value)); - - for (uint256 i = 1; i < amount - 1; i++) { + for (uint256 i; i < amount - 1; i++) { bytes32[] memory key = new bytes32[](1); key[0] = bytes32(i); world.setRecord(namespace, sourceName, key, abi.encodePacked(value)); From 949fbf66ecf2257e39aca6c3aca93ed8548fa74c Mon Sep 17 00:00:00 2001 From: Fraser Scott Date: Thu, 11 May 2023 10:11:33 +0200 Subject: [PATCH 05/15] refactor: add setKeysHelper --- packages/world/gas-report.json | 6 +-- packages/world/test/KeysInTableModule.t.sol | 54 ++++++++----------- packages/world/test/KeysWithValueModule.t.sol | 44 ++++++--------- 3 files changed, 40 insertions(+), 64 deletions(-) diff --git a/packages/world/gas-report.json b/packages/world/gas-report.json index 8fff5185ac..d596cf576d 100644 --- a/packages/world/gas-report.json +++ b/packages/world/gas-report.json @@ -105,7 +105,7 @@ "source": "test/KeysWithValueModule.t.sol", "name": "Setting the last of 10 keys", "functionCall": "world.setRecord(namespace, sourceName, lastKey, abi.encodePacked(value))", - "gasUsed": 134835 + "gasUsed": 134842 }, { "source": "test/KeysWithValueModule.t.sol", @@ -123,7 +123,7 @@ "source": "test/KeysWithValueModule.t.sol", "name": "Setting the last of 100 keys", "functionCall": "world.setRecord(namespace, sourceName, lastKey, abi.encodePacked(value))", - "gasUsed": 192958 + "gasUsed": 192967 }, { "source": "test/KeysWithValueModule.t.sol", @@ -141,7 +141,7 @@ "source": "test/KeysWithValueModule.t.sol", "name": "Setting the last of 1000 keys", "functionCall": "world.setRecord(namespace, sourceName, lastKey, abi.encodePacked(value))", - "gasUsed": 790938 + "gasUsed": 790975 }, { "source": "test/KeysWithValueModule.t.sol", diff --git a/packages/world/test/KeysInTableModule.t.sol b/packages/world/test/KeysInTableModule.t.sol index c7c0e1f216..b27545e158 100644 --- a/packages/world/test/KeysInTableModule.t.sol +++ b/packages/world/test/KeysInTableModule.t.sol @@ -271,7 +271,7 @@ contract KeysInTableModuleTest is Test { } // The KeysInTable module does not support composite keys yet, - // so this checks that the tuple returned only contains the first key + // so this documents that the tuple returned only contains the first key function testGetKeysInTableDoesNotSupportCompositeKeys(bytes32 keyA, bytes32 keyB, uint256 value1) public { _installKeysInTableModule(); @@ -294,21 +294,27 @@ contract KeysInTableModuleTest is Test { assertTrue(hasKey(world, sourceTableId, keyTuple)); } - function testGetKeysWithValueMany10() public { + function setKeysHelper(uint256 amount, uint256 value) internal returns (bytes32[] memory lastKey) { _installKeysInTableModule(); - uint256 AMOUNT = 10; - uint256 value = 1; - - bytes32[] memory lastKey = new bytes32[](1); - lastKey[0] = bytes32(uint256(AMOUNT - 1)); + lastKey = new bytes32[](1); + lastKey[0] = bytes32(uint256(amount - 1)); - for (uint256 i; i < AMOUNT - 1; i++) { + for (uint256 i; i < amount - 1; i++) { bytes32[] memory key = new bytes32[](1); key[0] = bytes32(i); world.setRecord(namespace, sourceFile, key, abi.encodePacked(value)); } + return lastKey; + } + + function testGetKeysWithValueMany10() public { + uint256 amount = 10; + uint256 value = 1; + + bytes32[] memory lastKey = setKeysHelper(amount, value); + // !gasreport Setting the last of 10 keys world.setRecord(namespace, sourceFile, lastKey, abi.encodePacked(value)); @@ -316,23 +322,14 @@ contract KeysInTableModuleTest is Test { bytes32[][] memory keyTuples = getKeysInTable(world, sourceTableId); // Assert that the list is correct - assertEq(keyTuples.length, AMOUNT); + assertEq(keyTuples.length, amount); } function testGetKeysWithValueMany100() public { - _installKeysInTableModule(); - - uint256 AMOUNT = 100; + uint256 amount = 100; uint256 value = 1; - bytes32[] memory lastKey = new bytes32[](1); - lastKey[0] = bytes32(uint256(AMOUNT - 1)); - - for (uint256 i; i < AMOUNT - 1; i++) { - bytes32[] memory key = new bytes32[](1); - key[0] = bytes32(i); - world.setRecord(namespace, sourceFile, key, abi.encodePacked(value)); - } + bytes32[] memory lastKey = setKeysHelper(amount, value); // !gasreport Setting the last of 100 keys world.setRecord(namespace, sourceFile, lastKey, abi.encodePacked(value)); @@ -341,23 +338,14 @@ contract KeysInTableModuleTest is Test { bytes32[][] memory keyTuples = getKeysInTable(world, sourceTableId); // Assert that the list is correct - assertEq(keyTuples.length, AMOUNT); + assertEq(keyTuples.length, amount); } function testGetKeysWithValueMany1000() public { - _installKeysInTableModule(); - - uint256 AMOUNT = 1000; + uint256 amount = 1000; uint256 value = 1; - bytes32[] memory lastKey = new bytes32[](1); - lastKey[0] = bytes32(uint256(AMOUNT - 1)); - - for (uint256 i; i < AMOUNT - 1; i++) { - bytes32[] memory key = new bytes32[](1); - key[0] = bytes32(i); - world.setRecord(namespace, sourceFile, key, abi.encodePacked(value)); - } + bytes32[] memory lastKey = setKeysHelper(amount, value); // !gasreport Setting the last of 1000 keys world.setRecord(namespace, sourceFile, lastKey, abi.encodePacked(value)); @@ -366,6 +354,6 @@ contract KeysInTableModuleTest is Test { bytes32[][] memory keyTuples = getKeysInTable(world, sourceTableId); // Assert that the list is correct - assertEq(keyTuples.length, AMOUNT); + assertEq(keyTuples.length, amount); } } diff --git a/packages/world/test/KeysWithValueModule.t.sol b/packages/world/test/KeysWithValueModule.t.sol index d2256a3e55..20b1e6baba 100644 --- a/packages/world/test/KeysWithValueModule.t.sol +++ b/packages/world/test/KeysWithValueModule.t.sol @@ -197,7 +197,7 @@ contract KeysWithValueModuleTest is Test { assertEq(keysWithValue.length, 1); assertEq(keysWithValue[0], key1); - // Set a another key with the same value + // Set another key with the same value world.setRecord(namespace, sourceName, keyTuple2, abi.encodePacked(value1)); // Get the list of keys with value2 from the target table @@ -209,21 +209,27 @@ contract KeysWithValueModuleTest is Test { assertEq(keysWithValue[1], key2); } - function testGetKeysWithValueMany10() public { + function setKeysHelper(uint256 amount, uint256 value) internal returns (bytes32[] memory lastKey) { _installKeysWithValueModule(); - uint256 amount = 10; - uint256 value = 1; - - bytes32[] memory lastKey = new bytes32[](1); - lastKey[0] = bytes32(uint256(amount - 1)); - for (uint256 i; i < amount - 1; i++) { bytes32[] memory key = new bytes32[](1); key[0] = bytes32(i); world.setRecord(namespace, sourceName, key, abi.encodePacked(value)); } + lastKey = new bytes32[](1); + lastKey[0] = bytes32(uint256(amount - 1)); + + return lastKey; + } + + function testGetKeysWithValueMany10() public { + uint256 amount = 10; + uint256 value = 1; + + bytes32[] memory lastKey = setKeysHelper(amount, value); + // !gasreport Setting the last of 10 keys world.setRecord(namespace, sourceName, lastKey, abi.encodePacked(value)); @@ -235,19 +241,10 @@ contract KeysWithValueModuleTest is Test { } function testGetKeysWithValueMany100() public { - _installKeysWithValueModule(); - uint256 amount = 100; uint256 value = 1; - bytes32[] memory lastKey = new bytes32[](1); - lastKey[0] = bytes32(uint256(amount - 1)); - - for (uint256 i; i < amount - 1; i++) { - bytes32[] memory key = new bytes32[](1); - key[0] = bytes32(i); - world.setRecord(namespace, sourceName, key, abi.encodePacked(value)); - } + bytes32[] memory lastKey = setKeysHelper(amount, value); // !gasreport Setting the last of 100 keys world.setRecord(namespace, sourceName, lastKey, abi.encodePacked(value)); @@ -260,19 +257,10 @@ contract KeysWithValueModuleTest is Test { } function testGetKeysWithValueMany1000() public { - _installKeysWithValueModule(); - uint256 amount = 1000; uint256 value = 1; - bytes32[] memory lastKey = new bytes32[](1); - lastKey[0] = bytes32(uint256(amount - 1)); - - for (uint256 i; i < amount - 1; i++) { - bytes32[] memory key = new bytes32[](1); - key[0] = bytes32(i); - world.setRecord(namespace, sourceName, key, abi.encodePacked(value)); - } + bytes32[] memory lastKey = setKeysHelper(amount, value); // !gasreport Setting the last of 1000 keys world.setRecord(namespace, sourceName, lastKey, abi.encodePacked(value)); From e95cd10734881d6352038b3d5a5a7e4111e44690 Mon Sep 17 00:00:00 2001 From: Fraser Scott Date: Thu, 11 May 2023 10:15:08 +0200 Subject: [PATCH 06/15] chore: run gas report --- packages/world/gas-report.json | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/packages/world/gas-report.json b/packages/world/gas-report.json index 8305cfe48e..fde60c3ca6 100644 --- a/packages/world/gas-report.json +++ b/packages/world/gas-report.json @@ -9,7 +9,7 @@ "source": "test/KeysInTableModule.t.sol", "name": "Setting the last of 10 keys", "functionCall": "world.setRecord(namespace, sourceFile, lastKey, abi.encodePacked(value))", - "gasUsed": 184984 + "gasUsed": 185027 }, { "source": "test/KeysInTableModule.t.sol", @@ -21,13 +21,13 @@ "source": "test/KeysInTableModule.t.sol", "name": "install keys in table module", "functionCall": "world.installRootModule(keysInTableModule, abi.encode(sourceTableId))", - "gasUsed": 1032425 + "gasUsed": 1032601 }, { "source": "test/KeysInTableModule.t.sol", "name": "Setting the last of 100 keys", "functionCall": "world.setRecord(namespace, sourceFile, lastKey, abi.encodePacked(value))", - "gasUsed": 243107 + "gasUsed": 243150 }, { "source": "test/KeysInTableModule.t.sol", @@ -39,13 +39,13 @@ "source": "test/KeysInTableModule.t.sol", "name": "install keys in table module", "functionCall": "world.installRootModule(keysInTableModule, abi.encode(sourceTableId))", - "gasUsed": 1032425 + "gasUsed": 1032601 }, { "source": "test/KeysInTableModule.t.sol", "name": "Setting the last of 1000 keys", "functionCall": "world.setRecord(namespace, sourceFile, lastKey, abi.encodePacked(value))", - "gasUsed": 841087 + "gasUsed": 841130 }, { "source": "test/KeysInTableModule.t.sol", @@ -57,7 +57,7 @@ "source": "test/KeysInTableModule.t.sol", "name": "install keys in table module", "functionCall": "world.installRootModule(keysInTableModule, abi.encode(sourceTableId))", - "gasUsed": 1032425 + "gasUsed": 1032601 }, { "source": "test/KeysInTableModule.t.sol", @@ -99,7 +99,7 @@ "source": "test/KeysWithValueModule.t.sol", "name": "install keys with value module", "functionCall": "world.installRootModule(keysWithValueModule, abi.encode(sourceTableId))", - "gasUsed": 609250 + "gasUsed": 609316 }, { "source": "test/KeysWithValueModule.t.sol", @@ -117,7 +117,7 @@ "source": "test/KeysWithValueModule.t.sol", "name": "install keys with value module", "functionCall": "world.installRootModule(keysWithValueModule, abi.encode(sourceTableId))", - "gasUsed": 609250 + "gasUsed": 609316 }, { "source": "test/KeysWithValueModule.t.sol", @@ -135,7 +135,7 @@ "source": "test/KeysWithValueModule.t.sol", "name": "install keys with value module", "functionCall": "world.installRootModule(keysWithValueModule, abi.encode(sourceTableId))", - "gasUsed": 609250 + "gasUsed": 609316 }, { "source": "test/KeysWithValueModule.t.sol", From bbebb859f857b2cd5e7491527da76b974de42bb6 Mon Sep 17 00:00:00 2001 From: Fraser Scott Date: Thu, 11 May 2023 13:29:13 +0200 Subject: [PATCH 07/15] test: setting 10000 keys --- packages/world/gas-report.json | 38 ++++++++++++++++++- packages/world/test/KeysInTableModule.t.sol | 17 +++++++++ packages/world/test/KeysWithValueModule.t.sol | 17 +++++++++ 3 files changed, 71 insertions(+), 1 deletion(-) diff --git a/packages/world/gas-report.json b/packages/world/gas-report.json index fde60c3ca6..5a80037003 100644 --- a/packages/world/gas-report.json +++ b/packages/world/gas-report.json @@ -59,6 +59,24 @@ "functionCall": "world.installRootModule(keysInTableModule, abi.encode(sourceTableId))", "gasUsed": 1032601 }, + { + "source": "test/KeysInTableModule.t.sol", + "name": "Setting the last of 10000 keys", + "functionCall": "world.setRecord(namespace, sourceFile, lastKey, abi.encodePacked(value))", + "gasUsed": 1380498 + }, + { + "source": "test/KeysInTableModule.t.sol", + "name": "Get list of 10000 keys with a given value", + "functionCall": "bytes32[][] memory keyTuples = getKeysInTable(world, sourceTableId)", + "gasUsed": 3715331 + }, + { + "source": "test/KeysInTableModule.t.sol", + "name": "install keys in table module", + "functionCall": "world.installRootModule(keysInTableModule, abi.encode(sourceTableId))", + "gasUsed": 1032601 + }, { "source": "test/KeysInTableModule.t.sol", "name": "set a record on a table with keysInTableModule installed", @@ -147,7 +165,25 @@ "source": "test/KeysWithValueModule.t.sol", "name": "Get list of 1000 keys with a given value", "functionCall": "bytes32[] memory keysWithValue = getKeysWithValue(world, sourceTableId, abi.encode(value))", - "gasUsed": 571426 + "gasUsed": 571437 + }, + { + "source": "test/KeysWithValueModule.t.sol", + "name": "install keys with value module", + "functionCall": "world.installRootModule(keysWithValueModule, abi.encode(sourceTableId))", + "gasUsed": 609316 + }, + { + "source": "test/KeysWithValueModule.t.sol", + "name": "Setting the last of 10000 keys", + "functionCall": "world.setRecord(namespace, sourceName, lastKey, abi.encodePacked(value))", + "gasUsed": 1330624 + }, + { + "source": "test/KeysWithValueModule.t.sol", + "name": "Get list of 10000 keys with a given value", + "functionCall": "bytes32[] memory keysWithValue = getKeysWithValue(world, sourceTableId, abi.encode(value))", + "gasUsed": 1828167 }, { "source": "test/KeysWithValueModule.t.sol", diff --git a/packages/world/test/KeysInTableModule.t.sol b/packages/world/test/KeysInTableModule.t.sol index b27545e158..2523facdbd 100644 --- a/packages/world/test/KeysInTableModule.t.sol +++ b/packages/world/test/KeysInTableModule.t.sol @@ -356,4 +356,21 @@ contract KeysInTableModuleTest is Test { // Assert that the list is correct assertEq(keyTuples.length, amount); } + + // NOTE: this test is expected to fail by setting too many keys + function testGetKeysWithValueMany10000() public { + uint256 amount = 10000; + uint256 value = 1; + + bytes32[] memory lastKey = setKeysHelper(amount, value); + + // !gasreport Setting the last of 10000 keys + world.setRecord(namespace, sourceFile, lastKey, abi.encodePacked(value)); + + // !gasreport Get list of 10000 keys with a given value + bytes32[][] memory keyTuples = getKeysInTable(world, sourceTableId); + + // Assert that the list is NOT what we expect + assertFalse(keyTuples.length == amount); + } } diff --git a/packages/world/test/KeysWithValueModule.t.sol b/packages/world/test/KeysWithValueModule.t.sol index 20b1e6baba..223ce6f2ae 100644 --- a/packages/world/test/KeysWithValueModule.t.sol +++ b/packages/world/test/KeysWithValueModule.t.sol @@ -271,4 +271,21 @@ contract KeysWithValueModuleTest is Test { // Assert that the list is correct assertEq(keysWithValue.length, amount); } + + // NOTE: this test is expected to fail by setting too many keys + function testGetKeysWithValueMany10000() public { + uint256 amount = 10000; + uint256 value = 1; + + bytes32[] memory lastKey = setKeysHelper(amount, value); + + // !gasreport Setting the last of 10000 keys + world.setRecord(namespace, sourceName, lastKey, abi.encodePacked(value)); + + // !gasreport Get list of 10000 keys with a given value + bytes32[] memory keysWithValue = getKeysWithValue(world, sourceTableId, abi.encode(value)); + + // Assert that the list is NOT what we expect + assertFalse(keysWithValue.length == amount); + } } From c4e59b3e2b7ccea143d35fc168207030f0ac643f Mon Sep 17 00:00:00 2001 From: Fraser Scott Date: Sun, 14 May 2023 18:38:18 +0100 Subject: [PATCH 08/15] refactor: rename array to keyTuples --- packages/world/gas-report.json | 8 ++++---- packages/world/test/KeysWithValueModule.t.sol | 16 ++++++++-------- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/packages/world/gas-report.json b/packages/world/gas-report.json index 5a80037003..8df4567a57 100644 --- a/packages/world/gas-report.json +++ b/packages/world/gas-report.json @@ -128,7 +128,7 @@ { "source": "test/KeysWithValueModule.t.sol", "name": "Get list of 10 keys with a given value", - "functionCall": "bytes32[] memory keysWithValue = getKeysWithValue(world, sourceTableId, abi.encode(value))", + "functionCall": "bytes32[] memory keyTuples = getKeysWithValue(world, sourceTableId, abi.encode(value))", "gasUsed": 12160 }, { @@ -146,7 +146,7 @@ { "source": "test/KeysWithValueModule.t.sol", "name": "Get list of 100 keys with a given value", - "functionCall": "bytes32[] memory keysWithValue = getKeysWithValue(world, sourceTableId, abi.encode(value))", + "functionCall": "bytes32[] memory keyTuples = getKeysWithValue(world, sourceTableId, abi.encode(value))", "gasUsed": 57149 }, { @@ -164,7 +164,7 @@ { "source": "test/KeysWithValueModule.t.sol", "name": "Get list of 1000 keys with a given value", - "functionCall": "bytes32[] memory keysWithValue = getKeysWithValue(world, sourceTableId, abi.encode(value))", + "functionCall": "bytes32[] memory keyTuples = getKeysWithValue(world, sourceTableId, abi.encode(value))", "gasUsed": 571437 }, { @@ -182,7 +182,7 @@ { "source": "test/KeysWithValueModule.t.sol", "name": "Get list of 10000 keys with a given value", - "functionCall": "bytes32[] memory keysWithValue = getKeysWithValue(world, sourceTableId, abi.encode(value))", + "functionCall": "bytes32[] memory keyTuples = getKeysWithValue(world, sourceTableId, abi.encode(value))", "gasUsed": 1828167 }, { diff --git a/packages/world/test/KeysWithValueModule.t.sol b/packages/world/test/KeysWithValueModule.t.sol index 223ce6f2ae..e758478f53 100644 --- a/packages/world/test/KeysWithValueModule.t.sol +++ b/packages/world/test/KeysWithValueModule.t.sol @@ -234,10 +234,10 @@ contract KeysWithValueModuleTest is Test { world.setRecord(namespace, sourceName, lastKey, abi.encodePacked(value)); // !gasreport Get list of 10 keys with a given value - bytes32[] memory keysWithValue = getKeysWithValue(world, sourceTableId, abi.encode(value)); + bytes32[] memory keyTuples = getKeysWithValue(world, sourceTableId, abi.encode(value)); // Assert that the list is correct - assertEq(keysWithValue.length, amount); + assertEq(keyTuples.length, amount); } function testGetKeysWithValueMany100() public { @@ -250,10 +250,10 @@ contract KeysWithValueModuleTest is Test { world.setRecord(namespace, sourceName, lastKey, abi.encodePacked(value)); // !gasreport Get list of 100 keys with a given value - bytes32[] memory keysWithValue = getKeysWithValue(world, sourceTableId, abi.encode(value)); + bytes32[] memory keyTuples = getKeysWithValue(world, sourceTableId, abi.encode(value)); // Assert that the list is correct - assertEq(keysWithValue.length, amount); + assertEq(keyTuples.length, amount); } function testGetKeysWithValueMany1000() public { @@ -266,10 +266,10 @@ contract KeysWithValueModuleTest is Test { world.setRecord(namespace, sourceName, lastKey, abi.encodePacked(value)); // !gasreport Get list of 1000 keys with a given value - bytes32[] memory keysWithValue = getKeysWithValue(world, sourceTableId, abi.encode(value)); + bytes32[] memory keyTuples = getKeysWithValue(world, sourceTableId, abi.encode(value)); // Assert that the list is correct - assertEq(keysWithValue.length, amount); + assertEq(keyTuples.length, amount); } // NOTE: this test is expected to fail by setting too many keys @@ -283,9 +283,9 @@ contract KeysWithValueModuleTest is Test { world.setRecord(namespace, sourceName, lastKey, abi.encodePacked(value)); // !gasreport Get list of 10000 keys with a given value - bytes32[] memory keysWithValue = getKeysWithValue(world, sourceTableId, abi.encode(value)); + bytes32[] memory keyTuples = getKeysWithValue(world, sourceTableId, abi.encode(value)); // Assert that the list is NOT what we expect - assertFalse(keysWithValue.length == amount); + assertFalse(keyTuples.length == amount); } } From fcf55124a969596c353f110569ef68953663a3cd Mon Sep 17 00:00:00 2001 From: alvrs Date: Fri, 16 Jun 2023 13:24:21 +0200 Subject: [PATCH 09/15] restore gas report --- packages/world/gas-report.json | 452 +++++++++++++++++++++++++++++++++ 1 file changed, 452 insertions(+) create mode 100644 packages/world/gas-report.json diff --git a/packages/world/gas-report.json b/packages/world/gas-report.json new file mode 100644 index 0000000000..1225a26ca1 --- /dev/null +++ b/packages/world/gas-report.json @@ -0,0 +1,452 @@ +[ + { + "source": "test/KeysInTableModule.t.sol", + "name": "install keys in table module", + "functionCall": "world.installRootModule(keysInTableModule, abi.encode(tableId))", + "gasUsed": 1286932 + }, + { + "source": "test/KeysInTableModule.t.sol", + "name": "Setting the 10th key", + "functionCall": "world.setRecord(namespace, name, lastKey, abi.encodePacked(value))", + "gasUsed": 163063 + }, + { + "source": "test/KeysInTableModule.t.sol", + "name": "Get list of 10 keys with a given value", + "functionCall": "bytes32[][] memory keyTuples = getKeysInTable(world, tableId)", + "gasUsed": 115647 + }, + { + "source": "test/KeysInTableModule.t.sol", + "name": "install keys in table module", + "functionCall": "world.installRootModule(keysInTableModule, abi.encode(tableId))", + "gasUsed": 1286932 + }, + { + "source": "test/KeysInTableModule.t.sol", + "name": "Setting the 100th key", + "functionCall": "world.setRecord(namespace, name, lastKey, abi.encodePacked(value))", + "gasUsed": 221185 + }, + { + "source": "test/KeysInTableModule.t.sol", + "name": "Get list of 100 keys with a given value", + "functionCall": "bytes32[][] memory keyTuples = getKeysInTable(world, tableId)", + "gasUsed": 1054977 + }, + { + "source": "test/KeysInTableModule.t.sol", + "name": "install keys in table module", + "functionCall": "world.installRootModule(keysInTableModule, abi.encode(tableId))", + "gasUsed": 1286932 + }, + { + "source": "test/KeysInTableModule.t.sol", + "name": "Setting the 1000th key", + "functionCall": "world.setRecord(namespace, name, lastKey, abi.encodePacked(value))", + "gasUsed": 819164 + }, + { + "source": "test/KeysInTableModule.t.sol", + "name": "Get list of 1000 keys with a given value", + "functionCall": "bytes32[][] memory keyTuples = getKeysInTable(world, tableId)", + "gasUsed": 11262721 + }, + { + "source": "test/KeysInTableModule.t.sol", + "name": "install keys in table module", + "functionCall": "world.installRootModule(keysInTableModule, abi.encode(tableId))", + "gasUsed": 1286932 + }, + { + "source": "test/KeysInTableModule.t.sol", + "name": "Setting the 10000th key", + "functionCall": "world.setRecord(namespace, name, lastKey, abi.encodePacked(value))", + "gasUsed": 8365172 + }, + { + "source": "test/KeysInTableModule.t.sol", + "name": "Get list of 10000 keys with a given value", + "functionCall": "bytes32[][] memory keyTuples = getKeysInTable(world, tableId)", + "gasUsed": 194783128 + }, + { + "source": "test/KeysInTableModule.t.sol", + "name": "install keys in table module", + "functionCall": "world.installRootModule(keysInTableModule, abi.encode(tableId))", + "gasUsed": 1286932 + }, + { + "source": "test/KeysInTableModule.t.sol", + "name": "install keys in table module", + "functionCall": "world.installRootModule(keysInTableModule, abi.encode(tableId))", + "gasUsed": 1286932 + }, + { + "source": "test/KeysInTableModule.t.sol", + "name": "set a record on a table with keysInTableModule installed", + "functionCall": "world.setRecord(namespace, name, keyTuple1, abi.encodePacked(value))", + "gasUsed": 193894 + }, + { + "source": "test/KeysInTableModule.t.sol", + "name": "install keys in table module", + "functionCall": "world.installRootModule(keysInTableModule, abi.encode(tableId))", + "gasUsed": 1286932 + }, + { + "source": "test/KeysInTableModule.t.sol", + "name": "install keys in table module", + "functionCall": "world.installRootModule(keysInTableModule, abi.encode(tableId))", + "gasUsed": 1286932 + }, + { + "source": "test/KeysInTableModule.t.sol", + "name": "change a composite record on a table with keysInTableModule installed", + "functionCall": "world.setRecord(namespace, compositeName, keyTupleA, abi.encodePacked(value2))", + "gasUsed": 30066 + }, + { + "source": "test/KeysInTableModule.t.sol", + "name": "delete a composite record on a table with keysInTableModule installed", + "functionCall": "world.deleteRecord(namespace, compositeName, keyTupleA)", + "gasUsed": 292110 + }, + { + "source": "test/KeysInTableModule.t.sol", + "name": "install keys in table module", + "functionCall": "world.installRootModule(keysInTableModule, abi.encode(tableId))", + "gasUsed": 1286932 + }, + { + "source": "test/KeysInTableModule.t.sol", + "name": "change a record on a table with keysInTableModule installed", + "functionCall": "world.setRecord(namespace, name, keyTuple1, abi.encodePacked(value2))", + "gasUsed": 28689 + }, + { + "source": "test/KeysInTableModule.t.sol", + "name": "delete a record on a table with keysInTableModule installed", + "functionCall": "world.deleteRecord(namespace, name, keyTuple1)", + "gasUsed": 153364 + }, + { + "source": "test/KeysWithValueModule.t.sol", + "name": "install keys with value module", + "functionCall": "world.installRootModule(keysWithValueModule, abi.encode(sourceTableId))", + "gasUsed": 626500 + }, + { + "source": "test/KeysWithValueModule.t.sol", + "name": "Get list of keys with a given value", + "functionCall": "bytes32[] memory keysWithValue = getKeysWithValue(world, sourceTableId, abi.encode(value1))", + "gasUsed": 7695 + }, + { + "source": "test/KeysWithValueModule.t.sol", + "name": "install keys with value module", + "functionCall": "world.installRootModule(keysWithValueModule, abi.encode(sourceTableId))", + "gasUsed": 626500 + }, + { + "source": "test/KeysWithValueModule.t.sol", + "name": "Setting the 10th key", + "functionCall": "world.setRecord(namespace, sourceName, lastKey, abi.encodePacked(value))", + "gasUsed": 134868 + }, + { + "source": "test/KeysWithValueModule.t.sol", + "name": "Get list of 10 keys with a given value", + "functionCall": "bytes32[] memory keyTuples = getKeysWithValue(world, sourceTableId, abi.encode(value))", + "gasUsed": 12156 + }, + { + "source": "test/KeysWithValueModule.t.sol", + "name": "install keys with value module", + "functionCall": "world.installRootModule(keysWithValueModule, abi.encode(sourceTableId))", + "gasUsed": 626500 + }, + { + "source": "test/KeysWithValueModule.t.sol", + "name": "Setting the 100th key", + "functionCall": "world.setRecord(namespace, sourceName, lastKey, abi.encodePacked(value))", + "gasUsed": 192993 + }, + { + "source": "test/KeysWithValueModule.t.sol", + "name": "Get list of 100 keys with a given value", + "functionCall": "bytes32[] memory keyTuples = getKeysWithValue(world, sourceTableId, abi.encode(value))", + "gasUsed": 57145 + }, + { + "source": "test/KeysWithValueModule.t.sol", + "name": "install keys with value module", + "functionCall": "world.installRootModule(keysWithValueModule, abi.encode(sourceTableId))", + "gasUsed": 626500 + }, + { + "source": "test/KeysWithValueModule.t.sol", + "name": "Setting the 1000th key", + "functionCall": "world.setRecord(namespace, sourceName, lastKey, abi.encodePacked(value))", + "gasUsed": 791001 + }, + { + "source": "test/KeysWithValueModule.t.sol", + "name": "Get list of 1000 keys with a given value", + "functionCall": "bytes32[] memory keyTuples = getKeysWithValue(world, sourceTableId, abi.encode(value))", + "gasUsed": 571433 + }, + { + "source": "test/KeysWithValueModule.t.sol", + "name": "install keys with value module", + "functionCall": "world.installRootModule(keysWithValueModule, abi.encode(sourceTableId))", + "gasUsed": 626500 + }, + { + "source": "test/KeysWithValueModule.t.sol", + "name": "Setting the 10000th key", + "functionCall": "world.setRecord(namespace, sourceName, lastKey, abi.encodePacked(value))", + "gasUsed": 8337290 + }, + { + "source": "test/KeysWithValueModule.t.sol", + "name": "Get list of 10000 keys with a given value", + "functionCall": "bytes32[] memory keyTuples = getKeysWithValue(world, sourceTableId, abi.encode(value))", + "gasUsed": 12153155 + }, + { + "source": "test/KeysWithValueModule.t.sol", + "name": "compute the target table selector", + "functionCall": "bytes32 targetTableSelector = getTargetTableSelector(MODULE_NAMESPACE, sourceTableId)", + "gasUsed": 2240 + }, + { + "source": "test/KeysWithValueModule.t.sol", + "name": "install keys with value module", + "functionCall": "world.installRootModule(keysWithValueModule, abi.encode(sourceTableId))", + "gasUsed": 626500 + }, + { + "source": "test/KeysWithValueModule.t.sol", + "name": "set a record on a table with KeysWithValueModule installed", + "functionCall": "world.setRecord(namespace, sourceName, keyTuple1, abi.encodePacked(value))", + "gasUsed": 165712 + }, + { + "source": "test/KeysWithValueModule.t.sol", + "name": "install keys with value module", + "functionCall": "world.installRootModule(keysWithValueModule, abi.encode(sourceTableId))", + "gasUsed": 626500 + }, + { + "source": "test/KeysWithValueModule.t.sol", + "name": "change a record on a table with KeysWithValueModule installed", + "functionCall": "world.setRecord(namespace, sourceName, keyTuple1, abi.encodePacked(value2))", + "gasUsed": 135364 + }, + { + "source": "test/KeysWithValueModule.t.sol", + "name": "delete a record on a table with KeysWithValueModule installed", + "functionCall": "world.deleteRecord(namespace, sourceName, keyTuple1)", + "gasUsed": 54309 + }, + { + "source": "test/KeysWithValueModule.t.sol", + "name": "install keys with value module", + "functionCall": "world.installRootModule(keysWithValueModule, abi.encode(sourceTableId))", + "gasUsed": 626500 + }, + { + "source": "test/KeysWithValueModule.t.sol", + "name": "set a field on a table with KeysWithValueModule installed", + "functionCall": "world.setField(namespace, sourceName, keyTuple1, 0, abi.encodePacked(value1))", + "gasUsed": 173726 + }, + { + "source": "test/KeysWithValueModule.t.sol", + "name": "change a field on a table with KeysWithValueModule installed", + "functionCall": "world.setField(namespace, sourceName, keyTuple1, 0, abi.encodePacked(value2))", + "gasUsed": 138084 + }, + { + "source": "test/query.t.sol", + "name": "CombinedHasHasValueNotQuery", + "functionCall": "bytes32[][] memory keyTuples = query(world, fragments)", + "gasUsed": 168826 + }, + { + "source": "test/query.t.sol", + "name": "CombinedHasHasValueQuery", + "functionCall": "bytes32[][] memory keyTuples = query(world, fragments)", + "gasUsed": 78844 + }, + { + "source": "test/query.t.sol", + "name": "CombinedHasNotQuery", + "functionCall": "bytes32[][] memory keyTuples = query(world, fragments)", + "gasUsed": 232573 + }, + { + "source": "test/query.t.sol", + "name": "CombinedHasQuery", + "functionCall": "bytes32[][] memory keyTuples = query(world, fragments)", + "gasUsed": 146143 + }, + { + "source": "test/query.t.sol", + "name": "CombinedHasValueNotQuery", + "functionCall": "bytes32[][] memory keyTuples = query(world, fragments)", + "gasUsed": 144801 + }, + { + "source": "test/query.t.sol", + "name": "CombinedHasValueQuery", + "functionCall": "bytes32[][] memory keyTuples = query(world, fragments)", + "gasUsed": 19555 + }, + { + "source": "test/query.t.sol", + "name": "HasQuery", + "functionCall": "bytes32[][] memory keyTuples = query(world, fragments)", + "gasUsed": 33032 + }, + { + "source": "test/query.t.sol", + "name": "HasQuery with 1000 keys", + "functionCall": "bytes32[][] memory keyTuples = query(world, fragments)", + "gasUsed": 11272548 + }, + { + "source": "test/query.t.sol", + "name": "HasQuery with 100 keys", + "functionCall": "bytes32[][] memory keyTuples = query(world, fragments)", + "gasUsed": 1056092 + }, + { + "source": "test/query.t.sol", + "name": "HasValueQuery", + "functionCall": "bytes32[][] memory keyTuples = query(world, fragments)", + "gasUsed": 9494 + }, + { + "source": "test/query.t.sol", + "name": "NotValueQuery", + "functionCall": "bytes32[][] memory keyTuples = query(world, fragments)", + "gasUsed": 72026 + }, + { + "source": "test/UniqueEntityModule.t.sol", + "name": "install unique entity module", + "functionCall": "world.installModule(uniqueEntityModule, new bytes(0))", + "gasUsed": 824012 + }, + { + "source": "test/UniqueEntityModule.t.sol", + "name": "get a unique entity nonce (non-root module)", + "functionCall": "uint256 uniqueEntity = uint256(getUniqueEntity(world))", + "gasUsed": 73389 + }, + { + "source": "test/UniqueEntityModule.t.sol", + "name": "installRoot unique entity module", + "functionCall": "world.installRootModule(uniqueEntityModule, new bytes(0))", + "gasUsed": 794087 + }, + { + "source": "test/UniqueEntityModule.t.sol", + "name": "get a unique entity nonce (root module)", + "functionCall": "uint256 uniqueEntity = uint256(getUniqueEntity(world))", + "gasUsed": 73389 + }, + { + "source": "test/World.t.sol", + "name": "Delete record", + "functionCall": "world.deleteRecord(namespace, name, singletonKey)", + "gasUsed": 16152 + }, + { + "source": "test/World.t.sol", + "name": "Push data to the table", + "functionCall": "world.pushToField(namespace, name, keyTuple, 0, encodedData)", + "gasUsed": 96514 + }, + { + "source": "test/World.t.sol", + "name": "Register a fallback system", + "functionCall": "bytes4 funcSelector1 = world.registerFunctionSelector(namespace, name, \"\", \"\")", + "gasUsed": 87024 + }, + { + "source": "test/World.t.sol", + "name": "Register a root fallback system", + "functionCall": "bytes4 funcSelector2 = world.registerRootFunctionSelector(namespace, name, worldFunc, 0)", + "gasUsed": 78208 + }, + { + "source": "test/World.t.sol", + "name": "Register a function selector", + "functionCall": "bytes4 functionSelector = world.registerFunctionSelector(namespace, name, \"msgSender\", \"()\")", + "gasUsed": 107622 + }, + { + "source": "test/World.t.sol", + "name": "Register a new namespace", + "functionCall": "world.registerNamespace(\"test\")", + "gasUsed": 161681 + }, + { + "source": "test/World.t.sol", + "name": "Register a root function selector", + "functionCall": "bytes4 functionSelector = world.registerRootFunctionSelector(namespace, name, worldFunc, sysFunc)", + "gasUsed": 94114 + }, + { + "source": "test/World.t.sol", + "name": "Register a new table in the namespace", + "functionCall": "bytes32 tableSelector = world.registerTable(namespace, table, schema, defaultKeySchema)", + "gasUsed": 262408 + }, + { + "source": "test/World.t.sol", + "name": "Write data to a table field", + "functionCall": "world.setField(namespace, name, singletonKey, 0, abi.encodePacked(true))", + "gasUsed": 44803 + }, + { + "source": "test/World.t.sol", + "name": "Set metadata", + "functionCall": "world.setMetadata(namespace, name, tableName, fieldNames)", + "gasUsed": 283358 + }, + { + "source": "test/World.t.sol", + "name": "Write data to the table", + "functionCall": "Bool.set(world, tableId, true)", + "gasUsed": 42692 + }, + { + "source": "test/WorldDynamicUpdate.t.sol", + "name": "pop 1 address (cold)", + "functionCall": "world.popFromField(namespace, name, keyTuple, 0, byteLengthToPop)", + "gasUsed": 38042 + }, + { + "source": "test/WorldDynamicUpdate.t.sol", + "name": "pop 1 address (warm)", + "functionCall": "world.popFromField(namespace, name, keyTuple, 0, byteLengthToPop)", + "gasUsed": 22836 + }, + { + "source": "test/WorldDynamicUpdate.t.sol", + "name": "updateInField 1 item (cold)", + "functionCall": "world.updateInField(namespace, name, keyTuple, 0, 0, EncodeArray.encode(dataForUpdate))", + "gasUsed": 40376 + }, + { + "source": "test/WorldDynamicUpdate.t.sol", + "name": "updateInField 1 item (warm)", + "functionCall": "world.updateInField(namespace, name, keyTuple, 0, 0, EncodeArray.encode(dataForUpdate))", + "gasUsed": 25574 + } +] From ac1864bc990ea09f5fec12a14be961e571916310 Mon Sep 17 00:00:00 2001 From: alvrs Date: Fri, 16 Jun 2023 13:39:16 +0200 Subject: [PATCH 10/15] only report installation gas cost once --- packages/world/gas-report.json | 70 ++++----------------- packages/world/test/KeysInTableModule.t.sol | 7 ++- 2 files changed, 16 insertions(+), 61 deletions(-) diff --git a/packages/world/gas-report.json b/packages/world/gas-report.json index 1225a26ca1..9b3edea456 100644 --- a/packages/world/gas-report.json +++ b/packages/world/gas-report.json @@ -1,27 +1,15 @@ [ - { - "source": "test/KeysInTableModule.t.sol", - "name": "install keys in table module", - "functionCall": "world.installRootModule(keysInTableModule, abi.encode(tableId))", - "gasUsed": 1286932 - }, { "source": "test/KeysInTableModule.t.sol", "name": "Setting the 10th key", "functionCall": "world.setRecord(namespace, name, lastKey, abi.encodePacked(value))", - "gasUsed": 163063 + "gasUsed": 163062 }, { "source": "test/KeysInTableModule.t.sol", "name": "Get list of 10 keys with a given value", "functionCall": "bytes32[][] memory keyTuples = getKeysInTable(world, tableId)", - "gasUsed": 115647 - }, - { - "source": "test/KeysInTableModule.t.sol", - "name": "install keys in table module", - "functionCall": "world.installRootModule(keysInTableModule, abi.encode(tableId))", - "gasUsed": 1286932 + "gasUsed": 115635 }, { "source": "test/KeysInTableModule.t.sol", @@ -33,13 +21,7 @@ "source": "test/KeysInTableModule.t.sol", "name": "Get list of 100 keys with a given value", "functionCall": "bytes32[][] memory keyTuples = getKeysInTable(world, tableId)", - "gasUsed": 1054977 - }, - { - "source": "test/KeysInTableModule.t.sol", - "name": "install keys in table module", - "functionCall": "world.installRootModule(keysInTableModule, abi.encode(tableId))", - "gasUsed": 1286932 + "gasUsed": 1054871 }, { "source": "test/KeysInTableModule.t.sol", @@ -51,31 +33,19 @@ "source": "test/KeysInTableModule.t.sol", "name": "Get list of 1000 keys with a given value", "functionCall": "bytes32[][] memory keyTuples = getKeysInTable(world, tableId)", - "gasUsed": 11262721 - }, - { - "source": "test/KeysInTableModule.t.sol", - "name": "install keys in table module", - "functionCall": "world.installRootModule(keysInTableModule, abi.encode(tableId))", - "gasUsed": 1286932 + "gasUsed": 11261666 }, { "source": "test/KeysInTableModule.t.sol", "name": "Setting the 10000th key", "functionCall": "world.setRecord(namespace, name, lastKey, abi.encodePacked(value))", - "gasUsed": 8365172 + "gasUsed": 8365173 }, { "source": "test/KeysInTableModule.t.sol", "name": "Get list of 10000 keys with a given value", "functionCall": "bytes32[][] memory keyTuples = getKeysInTable(world, tableId)", - "gasUsed": 194783128 - }, - { - "source": "test/KeysInTableModule.t.sol", - "name": "install keys in table module", - "functionCall": "world.installRootModule(keysInTableModule, abi.encode(tableId))", - "gasUsed": 1286932 + "gasUsed": 194772580 }, { "source": "test/KeysInTableModule.t.sol", @@ -87,49 +57,31 @@ "source": "test/KeysInTableModule.t.sol", "name": "set a record on a table with keysInTableModule installed", "functionCall": "world.setRecord(namespace, name, keyTuple1, abi.encodePacked(value))", - "gasUsed": 193894 - }, - { - "source": "test/KeysInTableModule.t.sol", - "name": "install keys in table module", - "functionCall": "world.installRootModule(keysInTableModule, abi.encode(tableId))", - "gasUsed": 1286932 - }, - { - "source": "test/KeysInTableModule.t.sol", - "name": "install keys in table module", - "functionCall": "world.installRootModule(keysInTableModule, abi.encode(tableId))", - "gasUsed": 1286932 + "gasUsed": 193907 }, { "source": "test/KeysInTableModule.t.sol", "name": "change a composite record on a table with keysInTableModule installed", "functionCall": "world.setRecord(namespace, compositeName, keyTupleA, abi.encodePacked(value2))", - "gasUsed": 30066 + "gasUsed": 30065 }, { "source": "test/KeysInTableModule.t.sol", "name": "delete a composite record on a table with keysInTableModule installed", "functionCall": "world.deleteRecord(namespace, compositeName, keyTupleA)", - "gasUsed": 292110 - }, - { - "source": "test/KeysInTableModule.t.sol", - "name": "install keys in table module", - "functionCall": "world.installRootModule(keysInTableModule, abi.encode(tableId))", - "gasUsed": 1286932 + "gasUsed": 292109 }, { "source": "test/KeysInTableModule.t.sol", "name": "change a record on a table with keysInTableModule installed", "functionCall": "world.setRecord(namespace, name, keyTuple1, abi.encodePacked(value2))", - "gasUsed": 28689 + "gasUsed": 28692 }, { "source": "test/KeysInTableModule.t.sol", "name": "delete a record on a table with keysInTableModule installed", "functionCall": "world.deleteRecord(namespace, name, keyTuple1)", - "gasUsed": 153364 + "gasUsed": 153363 }, { "source": "test/KeysWithValueModule.t.sol", diff --git a/packages/world/test/KeysInTableModule.t.sol b/packages/world/test/KeysInTableModule.t.sol index 3df3a06b9e..e90654cbd6 100644 --- a/packages/world/test/KeysInTableModule.t.sol +++ b/packages/world/test/KeysInTableModule.t.sol @@ -70,7 +70,6 @@ contract KeysInTableModuleTest is Test { // Install the index module // TODO: add support for installing this via installModule // -> requires `callFrom` for the module to be able to register a hook in the name of the original caller - // !gasreport install keys in table module world.installRootModule(keysInTableModule, abi.encode(tableId)); world.installRootModule(keysInTableModule, abi.encode(singletonTableId)); world.installRootModule(keysInTableModule, abi.encode(compositeTableId)); @@ -121,7 +120,11 @@ contract KeysInTableModuleTest is Test { } function testInstall(uint256 value) public { - _installKeysInTableModule(); + tableId = world.registerTable(namespace, name, tableSchema, tableKeySchema); + + // !gasreport install keys in table module + world.installRootModule(keysInTableModule, abi.encode(tableId)); + // Set a value in the source table // !gasreport set a record on a table with keysInTableModule installed world.setRecord(namespace, name, keyTuple1, abi.encodePacked(value)); From 0928eacb55a7fcc9b7df625672ed410fe79bd709 Mon Sep 17 00:00:00 2001 From: alvrs Date: Fri, 16 Jun 2023 13:47:53 +0200 Subject: [PATCH 11/15] only report installation gas cost once --- packages/world/gas-report.json | 60 +++---------------- packages/world/test/KeysWithValueModule.t.sol | 7 ++- 2 files changed, 14 insertions(+), 53 deletions(-) diff --git a/packages/world/gas-report.json b/packages/world/gas-report.json index 9b3edea456..90b4233808 100644 --- a/packages/world/gas-report.json +++ b/packages/world/gas-report.json @@ -83,23 +83,11 @@ "functionCall": "world.deleteRecord(namespace, name, keyTuple1)", "gasUsed": 153363 }, - { - "source": "test/KeysWithValueModule.t.sol", - "name": "install keys with value module", - "functionCall": "world.installRootModule(keysWithValueModule, abi.encode(sourceTableId))", - "gasUsed": 626500 - }, { "source": "test/KeysWithValueModule.t.sol", "name": "Get list of keys with a given value", "functionCall": "bytes32[] memory keysWithValue = getKeysWithValue(world, sourceTableId, abi.encode(value1))", - "gasUsed": 7695 - }, - { - "source": "test/KeysWithValueModule.t.sol", - "name": "install keys with value module", - "functionCall": "world.installRootModule(keysWithValueModule, abi.encode(sourceTableId))", - "gasUsed": 626500 + "gasUsed": 7692 }, { "source": "test/KeysWithValueModule.t.sol", @@ -111,31 +99,19 @@ "source": "test/KeysWithValueModule.t.sol", "name": "Get list of 10 keys with a given value", "functionCall": "bytes32[] memory keyTuples = getKeysWithValue(world, sourceTableId, abi.encode(value))", - "gasUsed": 12156 - }, - { - "source": "test/KeysWithValueModule.t.sol", - "name": "install keys with value module", - "functionCall": "world.installRootModule(keysWithValueModule, abi.encode(sourceTableId))", - "gasUsed": 626500 + "gasUsed": 12153 }, { "source": "test/KeysWithValueModule.t.sol", "name": "Setting the 100th key", "functionCall": "world.setRecord(namespace, sourceName, lastKey, abi.encodePacked(value))", - "gasUsed": 192993 + "gasUsed": 192994 }, { "source": "test/KeysWithValueModule.t.sol", "name": "Get list of 100 keys with a given value", "functionCall": "bytes32[] memory keyTuples = getKeysWithValue(world, sourceTableId, abi.encode(value))", - "gasUsed": 57145 - }, - { - "source": "test/KeysWithValueModule.t.sol", - "name": "install keys with value module", - "functionCall": "world.installRootModule(keysWithValueModule, abi.encode(sourceTableId))", - "gasUsed": 626500 + "gasUsed": 57127 }, { "source": "test/KeysWithValueModule.t.sol", @@ -147,13 +123,7 @@ "source": "test/KeysWithValueModule.t.sol", "name": "Get list of 1000 keys with a given value", "functionCall": "bytes32[] memory keyTuples = getKeysWithValue(world, sourceTableId, abi.encode(value))", - "gasUsed": 571433 - }, - { - "source": "test/KeysWithValueModule.t.sol", - "name": "install keys with value module", - "functionCall": "world.installRootModule(keysWithValueModule, abi.encode(sourceTableId))", - "gasUsed": 626500 + "gasUsed": 571256 }, { "source": "test/KeysWithValueModule.t.sol", @@ -165,7 +135,7 @@ "source": "test/KeysWithValueModule.t.sol", "name": "Get list of 10000 keys with a given value", "functionCall": "bytes32[] memory keyTuples = getKeysWithValue(world, sourceTableId, abi.encode(value))", - "gasUsed": 12153155 + "gasUsed": 12151397 }, { "source": "test/KeysWithValueModule.t.sol", @@ -185,17 +155,11 @@ "functionCall": "world.setRecord(namespace, sourceName, keyTuple1, abi.encodePacked(value))", "gasUsed": 165712 }, - { - "source": "test/KeysWithValueModule.t.sol", - "name": "install keys with value module", - "functionCall": "world.installRootModule(keysWithValueModule, abi.encode(sourceTableId))", - "gasUsed": 626500 - }, { "source": "test/KeysWithValueModule.t.sol", "name": "change a record on a table with KeysWithValueModule installed", "functionCall": "world.setRecord(namespace, sourceName, keyTuple1, abi.encodePacked(value2))", - "gasUsed": 135364 + "gasUsed": 135367 }, { "source": "test/KeysWithValueModule.t.sol", @@ -203,23 +167,17 @@ "functionCall": "world.deleteRecord(namespace, sourceName, keyTuple1)", "gasUsed": 54309 }, - { - "source": "test/KeysWithValueModule.t.sol", - "name": "install keys with value module", - "functionCall": "world.installRootModule(keysWithValueModule, abi.encode(sourceTableId))", - "gasUsed": 626500 - }, { "source": "test/KeysWithValueModule.t.sol", "name": "set a field on a table with KeysWithValueModule installed", "functionCall": "world.setField(namespace, sourceName, keyTuple1, 0, abi.encodePacked(value1))", - "gasUsed": 173726 + "gasUsed": 173713 }, { "source": "test/KeysWithValueModule.t.sol", "name": "change a field on a table with KeysWithValueModule installed", "functionCall": "world.setField(namespace, sourceName, keyTuple1, 0, abi.encodePacked(value2))", - "gasUsed": 138084 + "gasUsed": 138083 }, { "source": "test/query.t.sol", diff --git a/packages/world/test/KeysWithValueModule.t.sol b/packages/world/test/KeysWithValueModule.t.sol index 0ffb82a4f4..fa5b67be6b 100644 --- a/packages/world/test/KeysWithValueModule.t.sol +++ b/packages/world/test/KeysWithValueModule.t.sol @@ -55,12 +55,15 @@ contract KeysWithValueModuleTest is Test { // Install the index module // TODO: add support for installing this via installModule // -> requires `callFrom` for the module to be able to register a hook in the name of the original caller - // !gasreport install keys with value module world.installRootModule(keysWithValueModule, abi.encode(sourceTableId)); } function testInstall() public { - _installKeysWithValueModule(); + // Register source table + sourceTableId = world.registerTable(namespace, sourceName, sourceTableSchema, sourceTableKeySchema); + // !gasreport install keys with value module + world.installRootModule(keysWithValueModule, abi.encode(sourceTableId)); + // Set a value in the source table uint256 value = 1; From 43a695657a396a074d90c120eb0641cde5f306f1 Mon Sep 17 00:00:00 2001 From: alvrs Date: Fri, 16 Jun 2023 13:57:31 +0200 Subject: [PATCH 12/15] remove gas report from multi line code --- packages/store/test/KeyEncoding.t.sol | 3 --- 1 file changed, 3 deletions(-) diff --git a/packages/store/test/KeyEncoding.t.sol b/packages/store/test/KeyEncoding.t.sol index 84b0c93851..52e6f67b7b 100644 --- a/packages/store/test/KeyEncoding.t.sol +++ b/packages/store/test/KeyEncoding.t.sol @@ -22,7 +22,6 @@ contract KeyEncodingTest is Test, StoreReadWithStubs { function testSetAndGet() public { KeyEncoding.registerSchema(); - // !gasreport set KeyEncoding record KeyEncoding.set( 42, -42, @@ -33,7 +32,6 @@ contract KeyEncodingTest is Test, StoreReadWithStubs { true ); - // !gasreport get KeyEncoding record bool value = KeyEncoding.get( 42, -42, @@ -49,7 +47,6 @@ contract KeyEncodingTest is Test, StoreReadWithStubs { function testKeyEncoding() public { KeyEncoding.registerSchema(); - // !gasreport encode KeyEncoding key tuple bytes32[] memory keyTuple = KeyEncoding.encodeKeyTuple( 42, -42, From 1c4a34cd38377873e67139225867ecf210fe10b5 Mon Sep 17 00:00:00 2001 From: alvrs Date: Fri, 16 Jun 2023 13:58:55 +0200 Subject: [PATCH 13/15] update gas report --- packages/store/gas-report.json | 24 +++--------------------- 1 file changed, 3 insertions(+), 21 deletions(-) diff --git a/packages/store/gas-report.json b/packages/store/gas-report.json index 80f5b34818..7ca8be11cd 100644 --- a/packages/store/gas-report.json +++ b/packages/store/gas-report.json @@ -95,30 +95,12 @@ "functionCall": "someContract.doSomethingWithBytes(customEncoded)", "gasUsed": 1381 }, - { - "source": "test/KeyEncoding.t.sol", - "name": "encode KeyEncoding key tuple", - "functionCall": "bytes32[] memory keyTuple = KeyEncoding.encodeKeyTuple(42, -42, bytes16(hex\"1234\"), 0xFFfFfFffFFfffFFfFFfFFFFFffFFFffffFfFFFfF, true, ExampleEnum.Third)", - "gasUsed": 680 - }, { "source": "test/KeyEncoding.t.sol", "name": "register KeyEncoding schema", "functionCall": "KeyEncoding.registerSchema()", "gasUsed": 64695 }, - { - "source": "test/KeyEncoding.t.sol", - "name": "set KeyEncoding record", - "functionCall": "KeyEncoding.set(42, -42, bytes16(hex\"1234\"), 0xFFfFfFffFFfffFFfFFfFFFFFffFFFffffFfFFFfF, true, ExampleEnum.Third, true)", - "gasUsed": 41889 - }, - { - "source": "test/KeyEncoding.t.sol", - "name": "get KeyEncoding record", - "functionCall": "bool value = KeyEncoding.get(42, -42, bytes16(hex\"1234\"), 0xFFfFfFffFFfffFFfFFfFFFFFffFFFffffFfFFFfF, true, ExampleEnum.Third)", - "gasUsed": 5244 - }, { "source": "test/Mixed.t.sol", "name": "store Mixed struct in storage (native solidity)", @@ -441,7 +423,7 @@ "source": "test/StoreCoreGas.t.sol", "name": "delete record on table with subscriber", "functionCall": "StoreCore.deleteRecord(table, key)", - "gasUsed": 24545 + "gasUsed": 24553 }, { "source": "test/StoreCoreGas.t.sol", @@ -459,7 +441,7 @@ "source": "test/StoreCoreGas.t.sol", "name": "set (dynamic) field on table with subscriber", "functionCall": "StoreCore.setField(table, key, 1, arrayDataBytes)", - "gasUsed": 34919 + "gasUsed": 34927 }, { "source": "test/StoreCoreGas.t.sol", @@ -495,7 +477,7 @@ "source": "test/StoreCoreGas.t.sol", "name": "StoreCore: get key schema (warm)", "functionCall": "StoreCore.getKeySchema(table)", - "gasUsed": 1204 + "gasUsed": 2330 }, { "source": "test/StoreCoreGas.t.sol", From 5e3416ebfadfa9b4c75efc1c403bd800f13b233b Mon Sep 17 00:00:00 2001 From: alvrs Date: Sat, 17 Jun 2023 00:25:30 +0200 Subject: [PATCH 14/15] skip test for setting 10000 keys --- packages/world/gas-report.json | 12 ----------- packages/world/test/KeysInTableModule.t.sol | 22 ++++++++++----------- 2 files changed, 10 insertions(+), 24 deletions(-) diff --git a/packages/world/gas-report.json b/packages/world/gas-report.json index a767753240..146ca866c4 100644 --- a/packages/world/gas-report.json +++ b/packages/world/gas-report.json @@ -35,18 +35,6 @@ "functionCall": "bytes32[][] memory keyTuples = getKeysInTable(world, tableId)", "gasUsed": 11261666 }, - { - "source": "test/KeysInTableModule.t.sol", - "name": "Setting the 10000th key", - "functionCall": "world.setRecord(namespace, name, lastKey, abi.encodePacked(value))", - "gasUsed": 8365173 - }, - { - "source": "test/KeysInTableModule.t.sol", - "name": "Get list of 10000 keys with a given value", - "functionCall": "bytes32[][] memory keyTuples = getKeysInTable(world, tableId)", - "gasUsed": 194772580 - }, { "source": "test/KeysInTableModule.t.sol", "name": "install keys in table module", diff --git a/packages/world/test/KeysInTableModule.t.sol b/packages/world/test/KeysInTableModule.t.sol index e90654cbd6..431685c260 100644 --- a/packages/world/test/KeysInTableModule.t.sol +++ b/packages/world/test/KeysInTableModule.t.sol @@ -435,20 +435,18 @@ contract KeysInTableModuleTest is Test { assertEq(keyTuples.length, amount); } - // NOTE: this test is expected to fail by setting too many keys - function testGetKeysWithValueMany10000() public { - uint256 amount = 10000; - uint256 value = 1; + // // NOTE: this test is expected to fail by setting too many keys + // function testGetKeysWithValueMany10000() public { + // uint256 amount = 10000; + // uint256 value = 1; - bytes32[] memory lastKey = setKeysHelper(amount, value); + // bytes32[] memory lastKey = setKeysHelper(amount, value); - // !gasreport Setting the 10000th key - world.setRecord(namespace, name, lastKey, abi.encodePacked(value)); + // world.setRecord(namespace, name, lastKey, abi.encodePacked(value)); - // !gasreport Get list of 10000 keys with a given value - bytes32[][] memory keyTuples = getKeysInTable(world, tableId); + // bytes32[][] memory keyTuples = getKeysInTable(world, tableId); - // Assert that the list is correct - assertEq(keyTuples.length, amount); - } + // // Assert that the list is correct + // assertEq(keyTuples.length, amount); + // } } From 865adc9f8588924e5387cb43407962ebe654ef44 Mon Sep 17 00:00:00 2001 From: alvrs Date: Sat, 17 Jun 2023 00:35:44 +0200 Subject: [PATCH 15/15] remove test for setting 10000 keys --- packages/world/test/KeysInTableModule.t.sol | 15 --------------- 1 file changed, 15 deletions(-) diff --git a/packages/world/test/KeysInTableModule.t.sol b/packages/world/test/KeysInTableModule.t.sol index 431685c260..c8d10b6463 100644 --- a/packages/world/test/KeysInTableModule.t.sol +++ b/packages/world/test/KeysInTableModule.t.sol @@ -434,19 +434,4 @@ contract KeysInTableModuleTest is Test { // Assert that the list is correct assertEq(keyTuples.length, amount); } - - // // NOTE: this test is expected to fail by setting too many keys - // function testGetKeysWithValueMany10000() public { - // uint256 amount = 10000; - // uint256 value = 1; - - // bytes32[] memory lastKey = setKeysHelper(amount, value); - - // world.setRecord(namespace, name, lastKey, abi.encodePacked(value)); - - // bytes32[][] memory keyTuples = getKeysInTable(world, tableId); - - // // Assert that the list is correct - // assertEq(keyTuples.length, amount); - // } }