Thumb rule: largest to smallest; this gives perfect alignment when the dimensions of the elements are two, otherwise manual optimization is possible.
Note that proper alignment is generally important for speed, even if the processor is recovering from irregularities. While the x86 and (AFAIK) x64 CPUs manually shift access internally with a second read, the time "wasted" on reading misalignment is usually much more than the time saved due to the smaller working set. Therefore, I would only "pack tightly" when you are doing comparisons on multiple processors.
For non-POD you will need to check sizeof(element)
.
(If there are many objects, I would probably go with a simple parser generating C ++ to reset these sizes)
As an alternative, PVS-Studio has an analysis of the size of structures and gives recommendations for reordering. I haven't reviewed them yet, but you can use eval to find out if it works for you.
source share