Suboptimal padding of the optional type #1517
Replies: 1 comment 1 reply
-
Initially I thought you were right about the padding being suboptimal. The rule of thumb for optimizing struct size is normally to sort the members by decreasing size, but see also the famous The Lost Art of Structure Packing. The more I thought about it though, I don't think the order makes difference when it comes to the size of the When you say you replaced Therefore I would say that the I can't tell if there is any good reason to re-order the layout of optional. It is possible that if the |
Beta Was this translation helpful? Give feedback.
-
The memory layout of the optional type is currently like this:
This means that if
Type
requires more alignment thanbool
, there will be padding.For example, if
Type=double
, the memory might look like so:bpppppppdddddddd
b = bool, p = padding, d = double. That means almost half of the size of the type is just padding.If instead the memory layout was like:
There would not be this problem, as
bool
has no padding requirement (well, at least on x86).I'm assuming there is a good reason for this design, so pardon my ignorance, but I was wondering what the reason
for this is? I discovered this recently when replacing optional with some separate
bool
members, halved thesize of an important type in my codebase.
Beta Was this translation helpful? Give feedback.
All reactions