You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
ColdString is an 8 byte array (4 bytes on 32-bit machines):
35
-
```rust,ignore
36
-
pub struct ColdString([u8; 8]);
34
+
ColdString is 8-byte tagged pointer (4 bytes on 32-bit machines):
35
+
```rust
36
+
#[repr(packed)]
37
+
pubstructColdString {
38
+
/// The first byte of `encoded` is the "tag" and it determines the type:
39
+
/// - 10xxxxxx: an encoded address for the heap. To decode, 10 is set to 00 and swapped
40
+
/// with the LSB bits of the tag byte. The address is always a multiple of 4 (`HEAP_ALIGN`).
41
+
/// - 11111xxx: xxx is the length in range 0..=7, followed by length UTF-8 bytes.
42
+
/// - xxxxxxxx (valid UTF-8): 8 UTF-8 bytes.
43
+
encoded:*mutu8,
44
+
}
37
45
```
38
-
The array acts as either a pointer to heap data for strings longer than 8 bytes or is the inlined data itself. The first byte indicates one of 3 encodings:
46
+
`encoded`acts as either a pointer to the heap for strings longer than 8 bytes or is the inlined data itself. The first/"tag" byte indicates one of 3 encodings:
39
47
40
48
## Inline Mode (0 to 7 Bytes)
41
-
The first byte has bits 11111xxx, where xxx is the length. `self.0[1]` to `self.0[7]` store the bytes of string.
49
+
The tag byte has bits 11111xxx, where xxx is the length. `self.0[1]` to `self.0[7]` store the bytes of string.
42
50
43
51
## Inline Mode (8 Bytes)
44
-
`self.0` stores the bytes of string. Since the string is UTF-8, the first byte is guaranteed to not be 10xxxxx or 11111xxx.
52
+
The tag byte is any valid UTF-8 byte. `self.0` stores the bytes of string. Since the string is UTF-8, the tag byte is guaranteed to not be 10xxxxx or 11111xxx.
45
53
46
54
## Heap Mode
47
-
`self.0` encodes the pointer to heap, where first byte is 10xxxxxx. 10xxxxxx is chosen because it's a UTF-8 continuation byte and therefore an impossible first byte for inline mode. Since a heap-alignment of 4 is chosen, the pointer's least significant 2 bits are guaranteed to be 0 ([See more](https://doc.rust-lang.org/beta/std/alloc/struct.Layout.html#method.from_size_align)). These bits are swapped with the 10 "tag" bits when de/coding between `self.0` and the address value.
55
+
`self.0` encodes the pointer to heap, where tag byte is 10xxxxxx. 10xxxxxx is chosen because it's a UTF-8 continuation byte and therefore an impossible tag byte for inline mode. Since a heap-alignment of 4 is chosen, the pointer's least significant 2 bits are guaranteed to be 0 ([See more](https://doc.rust-lang.org/beta/std/alloc/struct.Layout.html#method.from_size_align)). These bits are swapped with the 10 "tag" bits when de/coding between `self.0` and the address value.
48
56
49
57
On the heap, the data starts with a variable length integer encoding of the length, followed by the bytes.
0 commit comments