Skip to content

Commit

Permalink
refactor(store,world): simplify constants (#1569)
Browse files Browse the repository at this point in the history
Co-authored-by: Kevin Ingersoll <kingersoll@gmail.com>
  • Loading branch information
alvrs and holic authored Sep 22, 2023
1 parent 9ddb28e commit 22ba7b6
Show file tree
Hide file tree
Showing 8 changed files with 95 additions and 94 deletions.
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

0 comments on commit 22ba7b6

Please sign in to comment.