News for hackers. Stuff that matters.
Re:Small Strings in Rust: smolstr vs. smartstring by dataflow on Saturday August 24, 2024 @02:43PM (#41340417)
> That said, I don’t see why it wouldn’t be possible to cram in 24 bytes of null-terminated payload

Note that I didn't say this is impossible, just that the given trick wouldn't work.

However, this is impossible for general strings. The only way could possibly make this work is if you constrain the inline string somehow (e.g., to UTF-8), so that some shorter strings failing that constraint are forced to go on the heap too. Otherwise you have 1 fixed zero byte at the end, and 23 fully flexible bytes, leaving you no way to represent an out-of-line string.

(Well, you could do it if you use the address as a key into some static map or such where you shove the real data, but that's cheating and beside the point here.)

Re:Small Strings in Rust: smolstr vs. smartstring by Dylan16807 on Sunday August 25, 2024 @09:36AM (#41347212)
Think that through again. An inline string needs a fixed 0 byte at the end. A heap string does not. Therefore if the last byte is anything other than 0 you have a heap string.

Inline strings only use 0.4% of your possible values.

Re:Small Strings in Rust: smolstr vs. smartstring by dataflow on Sunday August 25, 2024 @09:44AM (#41347264)
Oof, you're right, thank you. In my mind the last byte was obviously zero for a heap string too, since the pointer or sizes would've had a zero upper byte. Somehow I never accounted for the fact that on 64-bit there's no need to represent it that way. Fantastic point!