Closed pchampin closed 1 month ago
thinking more about this, I'm reluctant to make that change. It requires to know precisely the layout of the internal struct (is the len stored before or after the pointer physically?), the endian-ness of usize (is the most significant bit really at the "beginning" of the usize?)...
And the recent experience with debugging UB (#5, #8) makes me wary of making the code more complex.
Inspired by what is done in
EcoString
,MownStr
could sacrifice one more bit in thelen
metadata to "internalize" small (≤15B) strings.More precisely:
xlen
andaddr
would have to be stored in that order in memory;xlen
is 0,addr
points to borrowed data (like a&str
), andxlen
is the length;xlen
is 0,addr
points to owned data (like a box), andxlen
(without its first bit) is the length;xlen
(without its first two bits) is the length, and the remaining 7 bytes ofxlen
+ the 8 bytes ofaddr
are interpreted as a buffer ofu8
.NB: this makes the assumption that
xlen
andaddr
are 64bits long... the code would actually need to be adapted for other architectures.Maybe that's overly complicated for the potential benefit...