Skip to content

Commit

Permalink
refactor: rename PackedCounter to EncodedLengths (#2490)
Browse files Browse the repository at this point in the history
  • Loading branch information
holic committed Mar 21, 2024
1 parent a52ca17 commit 3e7d83d
Show file tree
Hide file tree
Showing 129 changed files with 1,261 additions and 1,230 deletions.
10 changes: 10 additions & 0 deletions .changeset/weak-rules-lick.md
@@ -0,0 +1,10 @@
---
"@latticexyz/cli": patch
"@latticexyz/protocol-parser": patch
"@latticexyz/store": major
"@latticexyz/world-modules": patch
"@latticexyz/world": major
"create-mud": patch
---

Renamed `PackedCounter` to `EncodedLengths` for consistency.
4 changes: 2 additions & 2 deletions docs/pages/guides/extending-a-world.mdx
Expand Up @@ -464,7 +464,7 @@ The easiest way to create the Solidity code is to use the MUD template:
Word 0 shows us that this field's value starts at `0x60`, which is word 3.
Because there are no static fields, word 3 is all zeros.

Word 1 is a [`PackedCounter`](https://github.com/latticexyz/mud/blob/main/packages/store/src/PackedCounter.sol) with the lengths of the dynamic fields.
Word 1 is a [`EncodedLengths`](https://github.com/latticexyz/mud/blob/main/packages/store/src/EncodedLengths.sol) with the lengths of the dynamic fields.
Here is the interpretation.

| Bytes | Value | Meaning |
Expand All @@ -476,7 +476,7 @@ The easiest way to create the Solidity code is to use the MUD template:
| 26-22 | `0000000000` | The length of the (non-existent in `Messages`) fourth dynamic field is zero |
| 31-27 | `0000000000` | The length of the (non-existent in `Messages`) fifth dynamic field is zero |

MUD tables can only have up to five dynamic fields because `PackedCounter` needs to fit in a 32 byte word.
MUD tables can only have up to five dynamic fields because `EncodedLengths` needs to fit in a 32 byte word.

Word 2 shows us that the field with the dynamic lengths starts at byte 0x80, which is word 4.
Word 4 gives us the length of the string.
Expand Down
10 changes: 5 additions & 5 deletions docs/pages/store/encoding.mdx
Expand Up @@ -275,14 +275,14 @@ Example: encoded static data of a table with a `uint64` and a `uint40` field.

## Dynamic length data

### Encoded lengths (`PackedCounter`)
### Encoded lengths (`EncodedLengths`)

Tables can have up to five dynamic length fields, each of which has a maximum size of `2**40` (~1 trillion) bytes.
This makes it possible to pack the lengths of all dynamic length fields of a table in a single `bytes32` word, instead of prefixing each dynamic length field with 32 bytes to store its length.

The data structure to store the lengths of all dynamic length fields is called `PackedCounter`.
The data structure to store the lengths of all dynamic length fields is called `EncodedLengths`.

Example: `PackedCounter` with 5 fields.
Example: `EncodedLengths` with 5 fields.

```
0x 0000000000000F 0000000001 0000000002 0000000003 0000000004 0000000005
Expand Down Expand Up @@ -353,7 +353,7 @@ function _getStaticDataLocation(ResourceId tableId, bytes32[] memory keyTuple) i
}
```

The [encoded lengths](#encoded-lengths-packedcounter) of all dynamic length fields is stored in a single storage slot, which is determined by a hash of the table ID and the key tuple.
The [encoded lengths](#encoded-lengths-encodedlengths) of all dynamic length fields is stored in a single storage slot, which is determined by a hash of the table ID and the key tuple.

```solidity
function _getDynamicDataLengthLocation(ResourceId tableId, bytes32[] memory keyTuple) internal pure returns (uint256) {
Expand All @@ -376,7 +376,7 @@ function _getDynamicDataLocation(

## Events

Data emitted as part of events uses the same encoding for [static length data](#static-length-data), [encoded lengths](#encoded-lengths-packedcounter) and [dynamic length data](#dynamic-length-data-encoding) as described above.
Data emitted as part of events uses the same encoding for [static length data](#static-length-data), [encoded lengths](#encoded-lengths-encodedlengths) and [dynamic length data](#dynamic-length-data-encoding) as described above.

Unlike the [layout in storage](#storage-layout), where each dynamic length data field is stored in its own storage location, in events all data of dynamic length fields is concatenated to a single `bytes` blob.
The encoded lengths emitted as part of the event contain the information required to slice up this concatenated `bytes` blob into the individual fields.
Expand Down
4 changes: 2 additions & 2 deletions docs/pages/store/reference/misc.mdx
Expand Up @@ -1616,7 +1616,7 @@ From left to right, the bytes are laid out as follows:
- 1 byte for number of static size fields
- 1 byte for number of dynamic size fields
- 28 bytes for 28 static field lengths
(MAX_DYNAMIC_FIELDS allows PackedCounter to pack the dynamic lengths into 1 word)\*
(MAX_DYNAMIC_FIELDS allows EncodedLengths to pack the dynamic lengths into 1 word)\*

```solidity
type FieldLayout is bytes32;
Expand Down Expand Up @@ -2476,7 +2476,7 @@ uint256 constant MAX_STATIC_FIELDS = 28;

#### MAX_DYNAMIC_FIELDS

_Represents the maximum number of dynamic fields that can be packed in a PackedCounter._
_Represents the maximum number of dynamic fields that can be packed in EncodedLengths._

```solidity
uint256 constant MAX_DYNAMIC_FIELDS = 5;
Expand Down

0 comments on commit 3e7d83d

Please sign in to comment.