News for hackers. Stuff that matters.
Re:Small Strings in Rust: smolstr vs. smartstring by tomcam on Sunday August 25, 2024 @12:16AM (#41344340)
Not sure why the downvotes? This is a sincere question.
Re:Small Strings in Rust: smolstr vs. smartstring by tialaramex on Sunday August 25, 2024 @05:39AM (#41345935)
For a lot of people it will seem obvious that String (Rust's standard library growable string type) is 24 bytes. CompactString is 24 bytes so that it is exactly the same size as String, that's the main idea. That's why they may not have believed you were sincere in asking.

Why is Rust's String 24 bytes? Well, this is a growable string type. So we need to store 1) where on the heap our growable buffer is and 2) how big that heap space is ("Capacity"), and 3) how much of it we're using already to store the string ("Size" or "Length"). On modern 64-bit computers it's reasonable to use a 64-bit (8 byte) value for all three of these facts, 3 times 8 = 24.

In fact Rust (unlike C++) insists on doing this the simplest and fastest way possible so the String type is more or less literally Vec<u8> - a growable array of bytes, plus a guarantee that those bytes are UTF-8 encoded text. Vec<u8> is likewise 24-bytes.

The rationale is that being simple is a good fundamental design, and (as CompactString illustrates) people can build more sophisticated types if they want to unlock specific optimisations which may suit their application.

Re:Small Strings in Rust: smolstr vs. smartstring by tomcam on Sunday August 25, 2024 @08:54AM (#41346951)
Fantastic, thanks. Keeping this answer for reference.