Skip to main content

Crate cold_string

Crate cold_string 

Source
Expand description

§cold-string

Github Crates.io docs.rs MSRV

A 1-word (8-byte) sized representation of immutable UTF-8 strings that in-lines up to 8 bytes.

  • Strings ≤ 8 bytes require no allocation
  • Compact variable-sized length encoding
  • Up to 67% smaller than other short-string optimizations (see Memory Comparisons)
  • Atomic reference counting support
  • Niche optimized: Option<ColdString> is the same size as ColdString
  • no_std compatible, MSRV 1.60
  • Uses strict provenance API. Miri, loom, and property tested (see Safety)

§Usage

Use it like a String:

use cold_string::ColdString;

let s = ColdString::new("qwerty");
assert_eq!(s.as_str(), "qwerty");
use cold_string::ColdString;
use std::mem::size_of;

assert_eq!(size_of::<ColdString>(), size_of::<usize>());
assert_eq!(size_of::<Option<ColdString>>(), size_of::<ColdString>());

Use ArcColdString when clones should share long-string storage:

use cold_string::ArcColdString;

let first = ArcColdString::new("a string longer than one machine word");
let second = first.clone();
assert_eq!(first, second);

ArcColdString uses a native-width reference count. ArcColdString8, ArcColdString16, and ArcColdString32 use 1-, 2-, and 4-byte counts when a smaller heap header is preferable. All variants remain one word in size and inline up to 8 bytes.

§How It Works

ColdString is an 8-byte tagged NonNull pointer (4 bytes on 32-bit machines) that has one of 3 representations:

  • the first byte is 10xxxxxx: The pointer encodes a heap address pointing to the length followed by the UTF-8 bytes. Rotating the 10 to the least significant position and setting to 00 decodes to the address (the heap allocation uses 4-byte alignment). The length of string is encoded with a variable length int that requires 1 bytes for 9–256 length, 2 bytes for lengths 257–511, 3 bytes for 512–65,791, etc.
  • otherwise, the string is inlined: the pointer represent 0-8 UTF-8 bytes, starting from the first byte. Trailing bytes are 0xFF, and the length is calculated from trailing_ones.
  • the one exception to the rule above is if the whole pointer is exactly (usize::MAX >> 4) << 2, in which the string represents "\0\0\0\0\0\0\0\0" (an invalid value for NonNull). This number is chosen because
    • the first 2 bits are 0, classifying it as inlined,
    • it’s an impossible UTF-8 representation, so doesn’t collide with other inlined strings.
    • it’s symmetrical, so it’s the same on big or little endian.

This design minimizes per-string overhead for both short and large strings (counting everything except the UTF-8 payload bytes):

Representation0–89–256257–511512–65,79165,792–16,777,471usize::MAX
ColdString8 bytes9 bytes10 bytes11 bytes12 bytes17 bytes
String24 bytes24 bytes24 bytes24 bytes24 bytes24 bytes

ArcColdString stores [atomic reference count][variable-length length][UTF-8 bytes] in one allocation and does not support weak references.

§Why “Cold”?

The heap representation stores the length on the heap, not inline in the struct. This saves memory in the struct itself but slightly increases the cost of len() since it requires a heap read. In practice, the len() cost is only marginally slower than inline storage and is typically negligible compared to memory savings, cache density improvements, and 3x faster operations on inlined strings.

§Safety

ColdString uses unsafe to implement its packed representation and pointer tagging. Usage of unsafe is narrowly scoped to where layout control is required, and each instance is documented with // SAFETY: <invariant>. To further ensure soundness, ColdString is written using Rust’s strict provenance API, handles unaligned access internally, maintains explicit heap alignment guarantees, and is validated with property testing and MIRI.

§Benchmarks

Overall, ColdString can lead to substantial memory savings over both String and other short-string crates:

  • 12% – 67% smaller Vec<S>
  • 35% – 67% smaller HashSet<S>
  • 30% – 75% smaller BTreeSet<S> (vs String)
  • 13% – 63% smaller BTreeSet<S> (vs other crates)

§Memory Comparisons (Allocator)

Memory usage per string, measured by tracking the memory requested by the allocator:

string_memory

§Memory Comparison (System RSS)

Resident set size in bytes per insertion of various collections. Insertions are strings with random length 0..=N:

Vec0..=40..=80..=160..=320..=64
cold-string8.08.023.233.753.4
compact_str24.024.024.034.660.6
compact_string22.924.931.639.755.7
smallstr24.024.038.050.368.4
smartstring24.024.024.040.465.4
smol_str24.024.024.039.971.2
std35.837.445.854.270.5
HashSet0..=40..=80..=160..=320..=64
cold-string18.918.934.545.564.0
compact_str52.452.452.462.288.9
compact_string23.230.039.649.165.9
smallstr52.452.466.578.696.9
smartstring52.452.452.468.294.0
smol_str52.452.452.468.399.4
std56.861.972.281.798.5
BTreeSet0..=40..=80..=160..=320..=64
cold-string10.118.949.379.1117.2
compact_str24.848.461.590.5145.7
compact_string19.743.767.088.3122.4
smallstr24.848.189.7121.9162.0
smartstring24.548.661.1102.3155.8
smol_str25.048.361.6100.7166.7
std35.870.4102.9128.9165.5

§Benchmark: as_str (ns/op)

Typelen=0..=4len=0..=8len=0..=16len=0..=32len=0..=255len=4..=4len=8..=8len=16..=16len=32..=32len=255..=255
String0.690.690.690.690.690.690.690.690.690.69
cold_string::ColdString1.661.611.201.000.941.621.630.770.800.83
compact_str::CompactString0.750.760.750.720.740.760.740.750.710.70
compact_string::CompactString0.730.720.720.730.760.720.720.720.750.76
smallstr::SmallString<[u8; 8]>0.670.670.850.690.730.670.680.670.660.66
smartstring::alias::String2.432.432.432.321.492.432.432.431.331.34
smol_str::SmolStr0.670.670.670.660.810.670.680.670.680.67

§Benchmark: eq (ns/op)

Average comparison speed with a 0%, 50%, and 100% chance of them being equal.

Typelen=4, eq=0len=4, eq=0.5len=4, eq=1len=8, eq=0len=8, eq=0.5len=8, eq=1len=16, eq=0len=16, eq=0.5len=16, eq=1len=32, eq=0len=32, eq=0.5len=32, eq=1len=255, eq=0len=255, eq=0.5len=255, eq=1
String2.332.982.742.512.942.602.392.972.812.202.842.622.1811.6113.55
cold_string::ColdString1.311.311.311.311.311.311.424.795.721.445.736.681.5312.2515.54
compact_str::CompactString2.903.953.243.043.853.253.044.443.472.843.553.302.8412.8014.56
compact_string::CompactString2.453.032.832.053.173.962.064.214.532.035.025.112.1112.8214.62
smallstr::SmallString<[u8; 8]>2.453.253.032.813.032.813.043.873.472.834.563.262.8711.9913.97
smartstring::alias::String6.557.426.896.137.337.386.119.057.994.976.405.424.9713.6914.18
smol_str::SmolStr4.213.291.963.773.131.963.843.332.084.335.724.594.1913.5415.20

§Benchmark: len (ns/op)

Typelen=4len=8len=16len=255
String0.430.430.430.43
cold_string::ColdString0.420.430.430.43
compact_str::CompactString0.420.420.430.43
compact_string::CompactString0.430.430.430.43
smallstr::SmallString<[u8; 8]>0.430.420.420.43
smartstring::alias::String1.291.281.301.29
smol_str::SmolStr0.430.420.450.45

§Benchmark: new (ns/op)

Typelen=0..=4len=0..=8len=0..=16len=0..=32len=0..=255len=4..=4len=8..=8len=16..=16len=32..=32len=255..=255
String29.6431.9935.7133.8441.2428.5828.8029.5530.3433.59
cold_string::ColdString10.7710.2225.6830.4443.246.255.0433.7933.7437.48
compact_str::CompactString11.5212.0612.3118.0753.3910.8810.8410.9538.4645.86
compact_string::CompactString34.9536.8440.2838.2444.9329.8830.2631.8432.2035.72
smallstr::SmallString<[u8; 8]>9.5210.0122.5029.0341.244.812.3829.0631.1533.29
smartstring::alias::String15.6015.8215.6631.4688.7215.6715.6510.89160.3772.01
smol_str::SmolStr17.6117.9315.9922.9455.7318.2713.8613.7539.4642.07

§Reference Counting Memory Comparison

Theoretical overhead on a 64-bit target, excluding one pointer and the UTF-8 payload. Inline values have no pointer or heap allocation, so their overhead is shown relative to the string bytes alone. Allocator rounding is not included.

Type8 bytes128 bytes512 bytes
Arc<str>242424
arcstr::ArcStr161616
ArcColdString0 (inline)910
ArcColdString320 (inline)56

RSS bytes per unique string in a pre-sized Vec, measured on 64-bit Windows with 1,000,000 strings per subprocess. Each cell is the median of three isolated release-mode runs and includes the Vec element, heap allocation, and allocator rounding. RSS results will vary by operating system and allocator.

Type4..=48..=816..=1632..=3264..=64
cold_string::ArcColdString8.08.056.370.2102.4
cold_string::ArcColdString328.08.038.254.586.3
arcstr::ArcStr38.338.454.470.5102.5
Arc<str>46.346.462.478.5110.6
Type0..=40..=80..=160..=320..=64
cold_string::ArcColdString8.08.023.238.058.0
cold_string::ArcColdString328.08.021.735.955.5
arcstr::ArcStr32.034.844.153.069.6
Arc<str>46.446.454.062.177.9

§Reference Counting Speed Comparison

Criterion point estimates in nanoseconds per operation on 64-bit Windows (release mode, 100 samples). Lower is better. Allocation and clone outputs are dropped outside the timed region; drop measures a non-final reference-count decrement.

Fresh allocation and string copy:

Type16 bytes128 bytes512 bytes
Arc<str>28.0329.5736.98
arcstr::ArcStr27.1928.7038.46
ArcColdString30.1232.6143.70

Clone/reference-count increment:

Type16 bytes128 bytes512 bytes
Arc<str>3.863.823.82
arcstr::ArcStr3.393.413.43
ArcColdString3.443.453.43

String access:

Type16 bytes128 bytes512 bytes
Arc<str>1.021.031.04
arcstr::ArcStr1.021.021.02
ArcColdString1.381.681.70

Non-final drop/reference-count decrement:

Type16 bytes128 bytes512 bytes
Arc<str>2.432.492.43
arcstr::ArcStr2.522.532.52
ArcColdString2.172.182.19

Clone and drop contention across 4 threads sharing strings in a fixed size pool: Type | Light (1024) | Medium (16) | Heavy (1)
:— | :—: | :—: | :—:
Arc<str> | 26.55 | 54.69 | 144.81 arcstr::ArcStr | 29.29 | 119.39 | 214.31 cold_string::ArcColdString32| 25.69 | 90.53 | 262.83

§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.

Type Aliases§

ArcColdString
A one-word, atomically reference-counted immutable UTF-8 string.
ArcColdString8
An ArcColdString with an 8-bit reference count.
ArcColdString16
An ArcColdString with a 16-bit reference count.
ArcColdString32
An ArcColdString with a 32-bit reference count.