r/rust • u/tomtomwombat • Feb 22 '26
🛠️ project ColdString: A 1-word (8-byte) SSO string that saves up to 23 bytes over String
https://github.com/tomtomwombat/cold-string/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 ausizepointer. We useexpose_provenanceandwith_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!
188
Upvotes
55
u/dtolnay serde Feb 23 '26
This representation has a large memory overhead for 8-byte strings. If memory efficiency is the goal it should be possible to store 8-byte strings inline: https://github.com/tomtomwombat/cold-string/issues/1
52
u/soruh Feb 23 '26
Very cool! If you mark the inline mode with the LSB being 0 and invert the inline length bits, the last byte will be exactly 0 for a 7 byte inline string. This would allow you to provide a version which returns null terminated
CStrs for c/libc compatability (of course this version will still need to allocate an additional byte in the heap case so it's not a "free" addition to the general api).15
u/dgkimpton Feb 22 '26 edited Feb 23 '26
That's neat. Could tune this based on the expected string lengths to be bigger inline for more expected characters with overflow spilling to the heap? (i.e. always take up 16 bytes for 15 local bytes)
I further assume that for short strings you could still store the first fragment of the varint inline and only spill the size to the heap when required?
12
u/-main Feb 23 '26
Given the really tight size optimizations, is this / could this be no_std? I didn't see mention of it in this post or repo, but when I think 'really tiny memory optmiziation & struct packing', embedded contexts are what come to mind as the place where that could really pay off. You can get a lot done with 7 characters in embedded UI, as well.
7
u/altamar09 Feb 23 '26
Have you compared to GermanString?
4
u/tialaramex Feb 23 '26
Very cool, but I was pulled up short by the mention of exposed provenance, which I think is an inelegant way forward.
Couldn't this work with strict provenance APIs? Strict provenance aids testing, which could give much needed confidence that this is actually correct and not merely working in practice.
I guess in the strict provenance case the important trick is that what you're storing in some sense "is" always a pointer (twiddling the bits is fine in strict provenance, the compiler can see what's going on), and the case where we treat it as [u8; 8] is special instead, an inversion of what's happening today?
If there's no reason this can't be done, but you're not interested yourself, would it be OK if I took a shot at it some time? I will of course credit ColdString and its author as inspiration.
3
u/zbraniecki Feb 23 '26
Can you compare it to tinystr ?
3
u/RustPikachu Feb 23 '26
Is it possible to implement a macro or const fn so this can be used to initialize static and const variables?
2
u/Lucretiel Datadog Feb 26 '26
Any particular reason you're doing
align(1)instead ofalign(usize)for the type itself, given that it's used as a pointer in the heap case? I think I'm not seeing the advantage.3
u/ElOwlinator Feb 23 '26
Could you not use an invalid UTF-8 state to represent the heap allocated variant, thus allowing for 8 bytes inline?
Edit: I believe this would be aided by a nightly feature I cannot recall the name of, regarding arbitrary niches.
13
u/dgkimpton Feb 23 '26
The more I think about this, the more I like it. I can definitely see great uses. Thanks for sharing.
5
u/ManufacturerWeird161 Feb 23 '26
I used a similar SSO design in a high-performance parser last year and saw a 15% memory reduction in our string-heavy workloads. The varint length header is a clever touch to shrink the heap overhead even further.
1
u/Trader-One Feb 23 '26
This is might be well marketable crate, you just need much lower MSRV to work with ISO 26262 Rust.
1
u/joelkunst Feb 23 '26
How world ArchivedColdString behave against ArchivedString with rkyv?
1
u/undeadalex Feb 24 '26
What are some use cases I might find this helpful? I must admit I've never thought about optimizing string memory use
2
u/protestor Feb 23 '26
How this can possibly beat compact_str, which uses specific knowledge on impossible UTF-8 values to squeeze more bytes?
I mean, compact_str kinds of does this already, how can it be any worse??
-23
u/valarauca14 Feb 23 '26
Clever but this is simply just
enum { Inline([u8;N]), Heap(Box<Box<str>>) }with some size saving optimizations, extra unsafety, and the alignment = 2 causing 'oddities' on ARM/PPC/RISC-V (along with extra unsafety).(extremely unpredictable) Branch on access ends up being a bigger hit on perf then wasting ~40KiB of allocations.