Remix.run Logo
tialaramex 4 hours ago

On a 64-bit machine the String type, and likewise a C++ std::string are 24 bytes, 8 bytes for a pointer to the allocated memory on the heap, then twice that for a size and capacity or their pointer equivalents depending on implementation.

The 3rd party library type CompactString can fit up to 24 bytes of UTF-8 text internally, yet it is still the same size as String and just like String, Option<CompactString> is the same size as CompactString. It does add complexity (and of course a library dependency if you care about that) but if you have lots of short strings this may be the best small string type for you.

[The key is UTF-8 encoding can only end with certain bytes, CompactString's documentation explains in more detail]

epage 4 hours ago | parent [-]

For more string types, see https://github.com/rosetta-rs/string-rosetta-rs

tialaramex 35 minutes ago | parent [-]

Yes - this is definitely about performance tuning which means you should be measuring and not just assume whatever you're about to do is a good idea. CompactString absolutely might make the wrong trades for your needs and the alternatives are very different.