#string #packed #array #static

packed_str

Store immutable strings in a single packed allocation

2 releases

0.2.1 Jul 10, 2023
0.2.0 Jul 10, 2023

#2538 in Data structures

MIT license

11KB
174 lines

Packed Immutable Strings

PackedStr is a replacement for Vec<String> when the strings are all immutable.

The benefit of using PackedStr is that all of the string data is stored in a single heap allocation. This may save space compared to Vec<String>, where each String has its own heap allocation, and associated costs (excess String capacity, allocator metadata, heap fragmentation).

In addition, each String stores its own size and capacity, which are not necessary due to the PackedStr design.

PackedStr is implemented as one large buffer containing all the string data, and a list of indexes into the buffer. Each string slice can be reconstructed from its index, and the index of the next string (or the end of the buffer, for the last string).


lib.rs:

Packed Immutable Strings

PackedStr is a replacement for Vec<String> when the strings are all immutable.

The benefit of using PackedStr is that all of the string data is stored in a single heap allocation. This may save space compared to Vec<String>, where each String has its own heap allocation, and associated costs (excess String capacity, allocator metadata, heap fragmentation).

In addition, each String stores its own size and capacity, which are not necessary due to the PackedStr design.

PackedStr is implemented as one large buffer containing all the string data, and a list of indexes into the buffer. Each string slice can be reconstructed from its index, and the index of the next string (or the end of the buffer, for the last string).

No runtime deps