logoalt Hacker News

tialaramexyesterday at 1:28 PM1 replyview on HN

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]


Replies

epageyesterday at 1:42 PM

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

show 1 reply