The JavaString uses short string optimizations and a lack of a "capacity" field to reduce struct size and heap fragmentation in certain cases.
Here's how it works:
len
, the length of the string, and data
, the pointer to the
string itself.data
is a valid pointer if and only if
it points to something that's aligned to 2 bytes.data
, and use that to see whether or not to dereference it.data
only uses one bit for its flag, we can use the entire lower
order byte for length information when it's interned. We do this with a
bitshift right.std::mem::size_of::<usize>() * 2 - 1
bytes of space.
On x64, this is 15 bytes, and on 32-bit architectures, this is 7 bytes.License: MIT