tinystr icon indicating copy to clipboard operation
tinystr copied to clipboard

TinyStrAuto is 32-bytes large, larger than the String

Open kornelski opened this issue 2 years ago • 0 comments

String is 24 bytes large, but because TinyStrAuto is a regular Rust enum, it adds an extra usize for the tag, so it's larger than a regular string. Not tiny at all ;(

I expected it to be an unsafe union that takes advantage of String's layout to store data over its pointer and capacity fields when len allows.

This is 24 bytes:

enum Auto {
    Small([u8; 23]),
    Big(Box<str>),
}

This is 32 bytes:

enum Auto {
    Small([u8; 31]),
    Big(String),
}

So even in safe Rust using only 16 bytes inline is suboptimal.

kornelski avatar Feb 01 '22 16:02 kornelski