ColdString: A 1-word (8-byte) SSO string that saves up to 23 bytes over String

184 points tomtomwombat 53 comments

I’ve been working on a specialized string type called ColdString. The goal was to create the most memory-efficient string representation possible.

  • Size: Exactly 1 usize (8 bytes on 64-bit).
  • Alignment: 1 byte (Uses repr(transparent) around a [u8; 8]).
  • Inline Capacity: Up to 7 bytes (Small String Optimization).
  • Heap Overhead: Only 1–9 bytes (VarInt length header) instead of the standard 16-byte (pointer, length) pair.

Usage

use cold_string::ColdString;

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

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

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

Memory Comparisons

(Average RSS size per string, in bytes, of 10 million ASCII strings).

Crate 0–4 chars 0–8 chars 0–16 chars 0–32 chars 0–64 chars
std 36.9 B 38.4 B 46.8 B 55.3 B 71.4 B
smol_str 24.0 B 24.0 B 24.0 B 41.1 B 72.2 B
compact_str 24.0 B 24.0 B 24.0 B 35.4 B 61.0 B
compact_string 24.1 B 25.8 B 32.6 B 40.5 B 56.5 B
cold-string 8.0 B 11.2 B 24.9 B 36.5 B 53.5 B

How it works

ColdString uses a Tagged Pointer approach. Because we enforce an alignment of 2 for heap allocations, the least-significant bit (LSB) of any heap address is guaranteed to be 0.

  • Inline Mode: If the LSB of the first byte is 1, the remaining bits in that byte represent the length (len<<1∣1), and the rest of the 8-byte array holds the UTF-8 data.
  • Heap Mode: If the LSB is 0, the 8 bytes are treated as a usize pointer. We use expose_provenance and with_exposed_provenance (Stable as of 1.84+) to safely round-trip the pointer through the array.
  • Length Storage: To keep the struct at 8 bytes, we don't store the length in the struct. Instead, we use a VarInt (LEB128) encoded length header at the start of the heap allocation, immediately followed by the string data.

As always, any feedback welcome!

Repo: https://github.com/tomtomwombat/cold-string/