Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(schema-type): add gas-report, optimize #1155

Merged
merged 3 commits into from
Jul 13, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
110 changes: 110 additions & 0 deletions packages/schema-type/gas-report.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
[
{
"file": "test/solidity/SchemaType.t.sol",
"test": "testGas",
"name": "getStaticByteLength: uint8",
"gasUsed": 124
},
{
"file": "test/solidity/SchemaType.t.sol",
"test": "testGas",
"name": "getStaticByteLength: uint32",
"gasUsed": 123
},
{
"file": "test/solidity/SchemaType.t.sol",
"test": "testGas",
"name": "getStaticByteLength: uint256",
"gasUsed": 123
},
{
"file": "test/solidity/SchemaType.t.sol",
"test": "testGas",
"name": "getStaticByteLength: int8",
"gasUsed": 123
},
{
"file": "test/solidity/SchemaType.t.sol",
"test": "testGas",
"name": "getStaticByteLength: int32",
"gasUsed": 123
},
{
"file": "test/solidity/SchemaType.t.sol",
"test": "testGas",
"name": "getStaticByteLength: int256",
"gasUsed": 123
},
{
"file": "test/solidity/SchemaType.t.sol",
"test": "testGas",
"name": "getStaticByteLength: bytes1",
"gasUsed": 123
},
{
"file": "test/solidity/SchemaType.t.sol",
"test": "testGas",
"name": "getStaticByteLength: bytes4",
"gasUsed": 123
},
{
"file": "test/solidity/SchemaType.t.sol",
"test": "testGas",
"name": "getStaticByteLength: bytes32",
"gasUsed": 123
},
{
"file": "test/solidity/SchemaType.t.sol",
"test": "testGas",
"name": "getStaticByteLength: bool",
"gasUsed": 123
},
{
"file": "test/solidity/SchemaType.t.sol",
"test": "testGas",
"name": "getStaticByteLength: address",
"gasUsed": 165
},
{
"file": "test/solidity/SchemaType.t.sol",
"test": "testGas",
"name": "getStaticByteLength: uint8[]",
"gasUsed": 166
},
{
"file": "test/solidity/SchemaType.t.sol",
"test": "testGas",
"name": "getStaticByteLength: uint32[]",
"gasUsed": 166
},
{
"file": "test/solidity/SchemaType.t.sol",
"test": "testGas",
"name": "getStaticByteLength: uint256[]",
"gasUsed": 166
},
{
"file": "test/solidity/SchemaType.t.sol",
"test": "testGas",
"name": "getStaticByteLength: int256[]",
"gasUsed": 166
},
{
"file": "test/solidity/SchemaType.t.sol",
"test": "testGas",
"name": "getStaticByteLength: bytes32[]",
"gasUsed": 166
},
{
"file": "test/solidity/SchemaType.t.sol",
"test": "testGas",
"name": "getStaticByteLength: bytes",
"gasUsed": 166
},
{
"file": "test/solidity/SchemaType.t.sol",
"test": "testGas",
"name": "getStaticByteLength: string",
"gasUsed": 166
}
]
2 changes: 2 additions & 0 deletions packages/schema-type/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,15 @@
"clean": "pnpm run clean:js",
"clean:js": "rimraf dist/typescript",
"dev": "tsup --watch",
"gas-report": "mud-gas-report --save gas-report.json",
"test": "vitest typecheck --run && vitest --run && forge test"
},
"dependencies": {
"abitype": "0.8.7",
"viem": "1.1.7"
},
"devDependencies": {
"@latticexyz/gas-report": "workspace:*",
"ds-test": "https://github.com/dapphub/ds-test.git#c9ce3f25bde29fc5eb9901842bf02850dfd2d084",
"forge-std": "https://github.com/foundry-rs/forge-std.git#b4f121555729b3afb3c5ffccb62ff4b6e2818fd3",
"rimraf": "^3.0.2",
Expand Down
1 change: 1 addition & 0 deletions packages/schema-type/remappings.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
ds-test/=node_modules/ds-test/src/
forge-std/=node_modules/forge-std/src/
@latticexyz/=node_modules/@latticexyz/
29 changes: 11 additions & 18 deletions packages/schema-type/src/solidity/SchemaType.sol
Original file line number Diff line number Diff line change
Expand Up @@ -211,27 +211,20 @@ enum SchemaType {
* Get the length of the data for the given schema type
* (Because Solidity doesn't support constant arrays, we need to use a function)
*/
function getStaticByteLength(SchemaType schemaType) pure returns (uint256) {
function getStaticByteLength(SchemaType schemaType) pure returns (uint256 r) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for free functions, do we not need/get any benefit from internal? (I assume not)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

free functions can't have visibility modifiers at all
effectively they're all always internal

uint256 index = uint8(schemaType);

if (index < 32) {
// uint8-256
return index + 1;
} else if (index < 64) {
// int8-256, offset by 32
return index + 1 - 32;
} else if (index < 96) {
// bytes1-32, offset by 64
return index + 1 - 64;
}

// Other static types
if (schemaType == SchemaType.BOOL) {
return 1;
if (index < 97) {
// SchemaType enum elements are cyclically ordered for optimal static length lookup
// indexes: 00-31, 32-63, 64-95, 96, 97, ...
// lengths: 01-32, 01-32, 01-32, 01, 20, (the rest are 0s)
unchecked {
return (index & 31) + 1;
}
} else if (schemaType == SchemaType.ADDRESS) {
return 20;
} else {
// Return 0 for all dynamic types
return 0;
}

// Return 0 for all dynamic types
return 0;
}
65 changes: 64 additions & 1 deletion packages/schema-type/test/solidity/SchemaType.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ pragma solidity >=0.8.0;

import { Test } from "forge-std/Test.sol";
import { stdError } from "forge-std/StdError.sol";
import { GasReporter } from "@latticexyz/gas-report/src/GasReporter.sol";
import { SchemaType } from "../../src/solidity/SchemaType.sol";

contract SchemaTypeTest is Test {
contract SchemaTypeTest is Test, GasReporter {
uint256 internal constant SCHEMA_TYPE_LENGTH = 198;

function testGetStaticByteLength() public {
Expand Down Expand Up @@ -53,4 +54,66 @@ contract SchemaTypeTest is Test {
vm.expectRevert(stdError.enumConversionError);
SchemaType(SCHEMA_TYPE_LENGTH).getStaticByteLength();
}

function testGas() public {
startGasReport("getStaticByteLength: uint8");
SchemaType.UINT8.getStaticByteLength();
endGasReport();
startGasReport("getStaticByteLength: uint32");
SchemaType.UINT32.getStaticByteLength();
endGasReport();
startGasReport("getStaticByteLength: uint256");
SchemaType.UINT256.getStaticByteLength();
endGasReport();

startGasReport("getStaticByteLength: int8");
SchemaType.INT8.getStaticByteLength();
endGasReport();
startGasReport("getStaticByteLength: int32");
SchemaType.INT32.getStaticByteLength();
endGasReport();
startGasReport("getStaticByteLength: int256");
SchemaType.INT256.getStaticByteLength();
endGasReport();

startGasReport("getStaticByteLength: bytes1");
SchemaType.BYTES1.getStaticByteLength();
endGasReport();
startGasReport("getStaticByteLength: bytes4");
SchemaType.BYTES4.getStaticByteLength();
endGasReport();
startGasReport("getStaticByteLength: bytes32");
SchemaType.BYTES32.getStaticByteLength();
endGasReport();

startGasReport("getStaticByteLength: bool");
SchemaType.BOOL.getStaticByteLength();
endGasReport();
startGasReport("getStaticByteLength: address");
SchemaType.ADDRESS.getStaticByteLength();
endGasReport();

startGasReport("getStaticByteLength: uint8[]");
SchemaType.UINT8_ARRAY.getStaticByteLength();
endGasReport();
startGasReport("getStaticByteLength: uint32[]");
SchemaType.UINT32_ARRAY.getStaticByteLength();
endGasReport();
startGasReport("getStaticByteLength: uint256[]");
SchemaType.UINT256_ARRAY.getStaticByteLength();
endGasReport();

startGasReport("getStaticByteLength: int256[]");
SchemaType.INT256_ARRAY.getStaticByteLength();
endGasReport();
startGasReport("getStaticByteLength: bytes32[]");
SchemaType.BYTES32_ARRAY.getStaticByteLength();
endGasReport();
startGasReport("getStaticByteLength: bytes");
SchemaType.BYTES.getStaticByteLength();
endGasReport();
startGasReport("getStaticByteLength: string");
SchemaType.STRING.getStaticByteLength();
endGasReport();
}
}
30 changes: 15 additions & 15 deletions packages/store/gas-report.json
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@
"file": "test/KeyEncoding.t.sol",
"test": "testRegisterAndGetSchema",
"name": "register KeyEncoding schema",
"gasUsed": 64686
"gasUsed": 63192
},
{
"file": "test/Mixed.t.sol",
Expand All @@ -111,7 +111,7 @@
"file": "test/Mixed.t.sol",
"test": "testRegisterAndGetSchema",
"name": "register Mixed schema",
"gasUsed": 61218
"gasUsed": 60210
},
{
"file": "test/Mixed.t.sol",
Expand All @@ -123,7 +123,7 @@
"file": "test/Mixed.t.sol",
"test": "testSetAndGet",
"name": "get record from Mixed",
"gasUsed": 13458
"gasUsed": 13135
},
{
"file": "test/PackedCounter.t.sol",
Expand Down Expand Up @@ -153,7 +153,7 @@
"file": "test/Schema.t.sol",
"test": "testEncodeDecodeSchema",
"name": "encode schema with 6 entries [SchemaLib.encode]",
"gasUsed": 6040
"gasUsed": 5639
},
{
"file": "test/Schema.t.sol",
Expand Down Expand Up @@ -195,7 +195,7 @@
"file": "test/Schema.t.sol",
"test": "testValidate",
"name": "validate schema",
"gasUsed": 18266
"gasUsed": 16381
},
{
"file": "test/Slice.t.sol",
Expand Down Expand Up @@ -369,7 +369,7 @@
"file": "test/StoreCoreGas.t.sol",
"test": "testAccessEmptyData",
"name": "access static field of non-existing record",
"gasUsed": 2974
"gasUsed": 2914
},
{
"file": "test/StoreCoreGas.t.sol",
Expand Down Expand Up @@ -423,7 +423,7 @@
"file": "test/StoreCoreGas.t.sol",
"test": "testHooks",
"name": "set static field on table with subscriber",
"gasUsed": 32051
"gasUsed": 31943
},
{
"file": "test/StoreCoreGas.t.sol",
Expand Down Expand Up @@ -471,7 +471,7 @@
"file": "test/StoreCoreGas.t.sol",
"test": "testRegisterAndGetSchema",
"name": "StoreCore: register schema",
"gasUsed": 54869
"gasUsed": 54509
},
{
"file": "test/StoreCoreGas.t.sol",
Expand Down Expand Up @@ -531,25 +531,25 @@
"file": "test/StoreCoreGas.t.sol",
"test": "testSetAndGetField",
"name": "set static field (1 slot)",
"gasUsed": 37985
"gasUsed": 37925
},
{
"file": "test/StoreCoreGas.t.sol",
"test": "testSetAndGetField",
"name": "get static field (1 slot)",
"gasUsed": 2975
"gasUsed": 2915
},
{
"file": "test/StoreCoreGas.t.sol",
"test": "testSetAndGetField",
"name": "set static field (overlap 2 slot)",
"gasUsed": 35007
"gasUsed": 34887
},
{
"file": "test/StoreCoreGas.t.sol",
"test": "testSetAndGetField",
"name": "get static field (overlap 2 slot)",
"gasUsed": 3875
"gasUsed": 3755
},
{
"file": "test/StoreCoreGas.t.sol",
Expand Down Expand Up @@ -627,7 +627,7 @@
"file": "test/StoreMetadata.t.sol",
"test": "testSetAndGet",
"name": "get record from StoreMetadataTable",
"gasUsed": 12689
"gasUsed": 12486
},
{
"file": "test/StoreSwitch.t.sol",
Expand Down Expand Up @@ -735,7 +735,7 @@
"file": "test/Vector2.t.sol",
"test": "testRegisterAndGetSchema",
"name": "register Vector2 schema",
"gasUsed": 57995
"gasUsed": 57391
},
{
"file": "test/Vector2.t.sol",
Expand All @@ -747,6 +747,6 @@
"file": "test/Vector2.t.sol",
"test": "testSetAndGet",
"name": "get Vector2 record",
"gasUsed": 5090
"gasUsed": 4970
}
]
Loading