Skip to main content

Crate cold_string

Crate cold_string 

Source
Expand description

§cold-string

Github Crates.io docs.rs Downloads

Compact representation of immutable UTF-8 strings. Optimized for memory usage and struct packing.

§Usage

Use it like a String:

use cold_string::ColdString;

let s = ColdString::new("qwerty");
assert_eq!(s.as_str(), "qwerty");

Packs well with other types:

use std::mem;
use cold_string::ColdString;

assert_eq!(mem::size_of::<ColdString>(), 8);
assert_eq!(mem::align_of::<ColdString>(), 1);

assert_eq!(mem::size_of::<(ColdString, u8)>(), 9);
assert_eq!(mem::align_of::<(ColdString, u8)>(), 1);

§How It Works

ColdString is an 8 byte array (4 bytes on 32-bit machines):

pub struct ColdString([u8; 8]);

The array acts as either a pointer to heap data for strings longer than 7 bytes or is the inlined data itself.

§Inline Mode

self.0[1] to self.0[7] store the bytes of string. In the least significant byte, self.0[0], the least significant bit signifies the inline/heap flag, and is set to “1” for inline mode. The next bits encode the length (always between 0 and 7).

b0 b1 b2 b3 b4 b5 b6 b7
b0 = <7 bit len> | 1

For example, "qwerty" = [13, 'q', 'w', 'e', 'r', 't', 'y', 0], where 13 is "qwerty".len() << 1 | 1.

§Heap Mode

The bytes act as a pointer to heap. The data on the heap has alignment 2, causing the least significant bit to always be 0 (since alignment 2 implies addr % 2 == 0), signifying heap mode. On the heap, the data starts with a variable length integer encoding of the length, followed by the bytes.

ptr --> <var int length> <data>

§Memory Comparisons

string_memory

§Memory Usage Comparison (RSS per String)

Crate0–4 chars0–8 chars0–16 chars0–32 chars0–64 chars
std36.9 B38.4 B46.8 B55.3 B71.4 B
smol_str24.0 B24.0 B24.0 B41.1 B72.2 B
compact_str24.0 B24.0 B24.0 B35.4 B61.0 B
compact_string24.1 B25.8 B32.6 B40.5 B56.5 B
cold-string8.0 B11.2 B24.9 B36.5 B53.5 B

Note: Columns represent string length (bytes/chars). Values represent average Resident Set Size (RSS) in bytes per string instance. Measurements taken with 10M iterations.

§License

Licensed under either of

  • Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
  • MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)

at your option.

§Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Structs§

ColdString
Compact representation of immutable UTF-8 strings. Optimized for memory usage and struct packing.