4 ms·
Option<u64> would look like tagged union in memory, so one bit "tag", 8 bytes u64 and padding. If type inside of Option<_> has invalid values (like references
by goldsteinq 5y ago
Option<u64> would look like tagged union in memory, so one bit "tag", 8 bytes u64 and padding.
If type inside of Option<_> has invalid values (like references which can't be null) one of these would be used for None, so Option<&T> is the same size as &T.
- hawk_ 5y agothanks and is the padding for aligned access (wasted space) or just full byte (poor performance on non x86/64 like architecture)? because that's the interesting part as to what's the tradeoff chosen and whether a dev can choose a different one. Any doc references are appreciated.
- Nullabillity 5y agoThey are padded for aligned access, so on amd64 Option<u64> will be 16 bytes.[0] However, the unused discriminant values are counted as niches for enums wrapping that, so Option<Option<u64>> is still 16 bytes (for example). [0]: https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=ccfc6db189ae326a52d3d5187fce11fc https://play.rust-lang.org/?version=stable&mode=debug&editio...
- ahahahahah 5y ago> If type inside of Option<_> has invalid values Can it actually use any general invalid bit pattern? I expect that it only supports an optimization for values that cannot be zero (and then it uses zero to indicate None).