Skip to main content

Crate surrealdb_strand

Crate surrealdb_strand 

Source
Expand description

A small-string-optimised immutable string type used throughout the value layer.

Strand backs string-shaped keys and values — Value::String, Object keys, TableName, RecordIdKey::String — and trades the mutability of String for three complementary storage strategies selected at construction time:

  • Inline — short strings (≤ INLINE_CAP bytes) stored directly on the stack, with no heap allocation.
  • Static — a &'static str wrapper for compile-time known values. Construction is const, clone is a pointer copy, and drop is a no-op. Ideal for long literals (table names, reserved keywords, response keys) that would otherwise allocate. Build one with Strand::new_static.
  • Boxed — dynamic long strings held in a Box<str>: one allocation per string, no atomic ops on construction or drop. Clone does malloc + memcpy, so reach for Strand::new_static whenever the value is known at compile time.

§Layout

Strand uses a custom 24-byte union layout. The first 16 bytes (on 64-bit) always form a valid &str fat pointer for Static and Boxed strings. For Inline strings, the string data is stored inline. The 24th byte (index 23) is used as a tag:

  • 0..=23: The string is Inline, and the tag is the length.
  • 254: The string is Static.
  • 255: The string is Boxed.

This layout allows as_str() to be completely branchless, significantly improving the performance of equality and ordering comparisons for all variants.

Structs§

Strand
Immutable string with inline small-string optimisation.

Constants§

INLINE_CAP
Maximum byte length of a string that can be stored inline.