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(store,world): simplify constants #1569

Merged
merged 4 commits into from
Sep 22, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
6 changes: 6 additions & 0 deletions .changeset/tough-flowers-breathe.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@latticexyz/store": patch
"@latticexyz/world": patch
---

Simplified a couple internal constants used for bitshifting.
2 changes: 1 addition & 1 deletion packages/store/gas-report.json
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,7 @@
"file": "test/ResourceId.t.sol",
"test": "testEncode",
"name": "encode table ID with name and type",
"gasUsed": 185
"gasUsed": 80
},
{
"file": "test/ResourceId.t.sol",
Expand Down
7 changes: 3 additions & 4 deletions packages/store/src/ResourceId.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@ pragma solidity >=0.8.21;

type ResourceId is bytes32;

uint256 constant TYPE_BYTES = 2;
uint256 constant NAME_BYTES = 32 - TYPE_BYTES;
uint256 constant BYTES_TO_BITS = 8;
uint256 constant TYPE_BITS = 2 * 8;
uint256 constant NAME_BITS = 32 * 8 - TYPE_BITS;

bytes32 constant TYPE_MASK = bytes32(hex"ffff");

library ResourceIdLib {
function encode(bytes2 typeId, bytes30 name) internal pure returns (ResourceId) {
return ResourceId.wrap(bytes32(typeId) | (bytes32(name) >> (TYPE_BYTES * BYTES_TO_BITS)));
return ResourceId.wrap(bytes32(typeId) | (bytes32(name) >> TYPE_BITS));
}
}

Expand Down
Loading