From d00c4a9af5fe54b1d21caa9f5cd525e48b3960f5 Mon Sep 17 00:00:00 2001 From: Sanbao Zheng Date: Sat, 13 Jan 2024 00:56:04 +0800 Subject: [PATCH] fix(world): inline debug constants [L-11] (#1976) Co-authored-by: Fraser Scott Co-authored-by: Kevin Ingersoll --- .changeset/gold-wombats-add.md | 5 +++ packages/world-modules/gas-report.json | 44 +++++++++++------------ packages/world/gas-report.json | 8 ++++- packages/world/src/WorldResourceId.sol | 7 ++-- packages/world/test/WorldResourceId.t.sol | 14 ++++++++ 5 files changed, 50 insertions(+), 28 deletions(-) create mode 100644 .changeset/gold-wombats-add.md diff --git a/.changeset/gold-wombats-add.md b/.changeset/gold-wombats-add.md new file mode 100644 index 0000000000..b1d4e32ea9 --- /dev/null +++ b/.changeset/gold-wombats-add.md @@ -0,0 +1,5 @@ +--- +"@latticexyz/world": patch +--- + +Removed `ROOT_NAMESPACE_STRING` and `ROOT_NAME_STRING` exports in favor of inlining these constants, to avoid reuse as they're meant for internal error messages and debugging. diff --git a/packages/world-modules/gas-report.json b/packages/world-modules/gas-report.json index 3f640e13ca..417529465d 100644 --- a/packages/world-modules/gas-report.json +++ b/packages/world-modules/gas-report.json @@ -3,85 +3,85 @@ "file": "test/ERC20.t.sol", "test": "testApprove", "name": "approve", - "gasUsed": 114366 + "gasUsed": 114369 }, { "file": "test/ERC20.t.sol", "test": "testBurn", "name": "burn", - "gasUsed": 75931 + "gasUsed": 75937 }, { "file": "test/ERC20.t.sol", "test": "testMint", "name": "mint", - "gasUsed": 161770 + "gasUsed": 161776 }, { "file": "test/ERC20.t.sol", "test": "testTransfer", "name": "transfer", - "gasUsed": 93016 + "gasUsed": 93022 }, { "file": "test/ERC20.t.sol", "test": "testTransferFrom", "name": "transferFrom", - "gasUsed": 130355 + "gasUsed": 130367 }, { "file": "test/ERC721.t.sol", "test": "testApproveAllGas", "name": "setApprovalForAll", - "gasUsed": 113993 + "gasUsed": 113996 }, { "file": "test/ERC721.t.sol", "test": "testApproveGas", "name": "approve", - "gasUsed": 87999 + "gasUsed": 88005 }, { "file": "test/ERC721.t.sol", "test": "testBurnGas", "name": "burn", - "gasUsed": 101937 + "gasUsed": 101949 }, { "file": "test/ERC721.t.sol", "test": "testMintGas", "name": "mint", - "gasUsed": 169501 + "gasUsed": 169510 }, { "file": "test/ERC721.t.sol", "test": "testSafeMintToEOAGas", "name": "safeMint", - "gasUsed": 169772 + "gasUsed": 169781 }, { "file": "test/ERC721.t.sol", "test": "testSafeTransferFromToEOAGas", "name": "safeTransferFrom", - "gasUsed": 143774 + "gasUsed": 143789 }, { "file": "test/ERC721.t.sol", "test": "testTransferFromGas", "name": "transferFrom", - "gasUsed": 136934 + "gasUsed": 136946 }, { "file": "test/KeysInTableModule.t.sol", "test": "testInstallComposite", "name": "install keys in table module", - "gasUsed": 1413357 + "gasUsed": 1413360 }, { "file": "test/KeysInTableModule.t.sol", "test": "testInstallGas", "name": "install keys in table module", - "gasUsed": 1413357 + "gasUsed": 1413360 }, { "file": "test/KeysInTableModule.t.sol", @@ -93,13 +93,13 @@ "file": "test/KeysInTableModule.t.sol", "test": "testInstallSingleton", "name": "install keys in table module", - "gasUsed": 1413357 + "gasUsed": 1413360 }, { "file": "test/KeysInTableModule.t.sol", "test": "testSetAndDeleteRecordHookCompositeGas", "name": "install keys in table module", - "gasUsed": 1413357 + "gasUsed": 1413360 }, { "file": "test/KeysInTableModule.t.sol", @@ -117,7 +117,7 @@ "file": "test/KeysInTableModule.t.sol", "test": "testSetAndDeleteRecordHookGas", "name": "install keys in table module", - "gasUsed": 1413357 + "gasUsed": 1413360 }, { "file": "test/KeysInTableModule.t.sol", @@ -135,7 +135,7 @@ "file": "test/KeysWithValueModule.t.sol", "test": "testGetKeysWithValueGas", "name": "install keys with value module", - "gasUsed": 668168 + "gasUsed": 668171 }, { "file": "test/KeysWithValueModule.t.sol", @@ -153,7 +153,7 @@ "file": "test/KeysWithValueModule.t.sol", "test": "testInstall", "name": "install keys with value module", - "gasUsed": 668168 + "gasUsed": 668171 }, { "file": "test/KeysWithValueModule.t.sol", @@ -165,7 +165,7 @@ "file": "test/KeysWithValueModule.t.sol", "test": "testSetAndDeleteRecordHook", "name": "install keys with value module", - "gasUsed": 668168 + "gasUsed": 668171 }, { "file": "test/KeysWithValueModule.t.sol", @@ -183,7 +183,7 @@ "file": "test/KeysWithValueModule.t.sol", "test": "testSetField", "name": "install keys with value module", - "gasUsed": 668168 + "gasUsed": 668171 }, { "file": "test/KeysWithValueModule.t.sol", @@ -315,7 +315,7 @@ "file": "test/UniqueEntityModule.t.sol", "test": "testInstallRoot", "name": "installRoot unique entity module", - "gasUsed": 663827 + "gasUsed": 663830 }, { "file": "test/UniqueEntityModule.t.sol", diff --git a/packages/world/gas-report.json b/packages/world/gas-report.json index 6a3c0400aa..bf83136050 100644 --- a/packages/world/gas-report.json +++ b/packages/world/gas-report.json @@ -105,7 +105,7 @@ "file": "test/World.t.sol", "test": "testRegisterRootFunctionSelector", "name": "Register a root function selector", - "gasUsed": 80462 + "gasUsed": 80457 }, { "file": "test/World.t.sol", @@ -172,5 +172,11 @@ "test": "testGetType", "name": "get type from a resource ID", "gasUsed": 4 + }, + { + "file": "test/WorldResourceId.t.sol", + "test": "testToString", + "name": "convert resource ID to string", + "gasUsed": 3313 } ] diff --git a/packages/world/src/WorldResourceId.sol b/packages/world/src/WorldResourceId.sol index d085428251..4c6b340103 100644 --- a/packages/world/src/WorldResourceId.sol +++ b/packages/world/src/WorldResourceId.sol @@ -10,9 +10,6 @@ import { RESOURCE_NAMESPACE, MASK_RESOURCE_NAMESPACE } from "./worldResourceType uint256 constant NAMESPACE_BITS = 14 * 8; // 14 bytes * 8 bits per byte uint256 constant NAME_BITS = 16 * 8; // 16 bytes * 8 bits per byte -bytes16 constant ROOT_NAMESPACE_STRING = bytes16("ROOT_NAMESPACE"); -bytes16 constant ROOT_NAME_STRING = bytes16("ROOT_NAME"); - bytes32 constant NAMESPACE_MASK = bytes32(~bytes14("")) >> (TYPE_BITS); /** @@ -102,9 +99,9 @@ library WorldResourceIdInstance { abi.encodePacked( resourceType, ":", - resourceNamespace == ROOT_NAMESPACE ? ROOT_NAMESPACE_STRING : resourceNamespace, + resourceNamespace == ROOT_NAMESPACE ? "" : WorldResourceIdLib.toTrimmedString(resourceNamespace), ":", - resourceName == ROOT_NAME ? ROOT_NAME_STRING : resourceName + resourceName == ROOT_NAME ? "" : WorldResourceIdLib.toTrimmedString(resourceName) ) ); } diff --git a/packages/world/test/WorldResourceId.t.sol b/packages/world/test/WorldResourceId.t.sol index 1ed06df233..e8e4d8dcea 100644 --- a/packages/world/test/WorldResourceId.t.sol +++ b/packages/world/test/WorldResourceId.t.sol @@ -53,6 +53,20 @@ contract WorldResourceIdTest is Test, GasReporter { assertEq(ResourceId.unwrap(namespaceId), ResourceId.unwrap(WorldResourceIdLib.encodeNamespace("namespace"))); } + function testToString() public { + ResourceId resourceId = WorldResourceIdLib.encode({ + typeId: RESOURCE_SYSTEM, + namespace: "namespace", + name: "name" + }); + + startGasReport("convert resource ID to string"); + string memory resourceIdString = resourceId.toString(); + endGasReport(); + + assertEq(resourceIdString, "sy:namespace:name"); + } + function testGetType() public { ResourceId resourceId = WorldResourceIdLib.encode({ typeId: RESOURCE_SYSTEM,