Skip to main content

Module smart_string

Module smart_string 

Source
Expand description

SmartString - A 16-byte string type with inline small string optimization

This is a compact string representation that enables 16-byte Value enums through niche optimization. The design:

  • Inline: strings ≤15 bytes stored inline (no heap allocation)
  • Heap: strings >15 bytes stored as Arc<str> for O(1) clone

§Memory Layout (16 bytes, 8-byte aligned)

Inline (≤15 bytes):
  [tag: 1 byte] [data: 15 bytes]
  tag = 0-15 (encodes length)

Heap (>15 bytes) on 64-bit:
  [tag: 1 byte] [length: 7 bytes] [Arc<str> data pointer: 8 bytes]
  tag = 16 (heap marker), pointer at data[7..15]

Heap (>15 bytes) on 32-bit:
  [tag: 1 byte] [length: 11 bytes] [Arc<str> data pointer: 4 bytes]
  tag = 16 (heap marker), pointer at data[11..15]

§Niche Optimization

StringTag only uses values 0-16, leaving values 17-255 as “niches”. The Rust compiler uses these niches to store the Value enum’s discriminant, enabling a 16-byte Value enum without explicit discriminant storage.

Structs§

SmartString
A 16-byte string with inline SSO (Small String Optimization).

Enums§

StringTag
String tag that encodes storage mode and inline length.

Constants§

MAX_INLINE_LEN
Maximum inline string length (15 bytes)