Most processor architectures prefer natural alignment as the default alignment requirement but I think that processor-word alignment is a more efficient alignment requirement that saves memory without any performance overhead over natural alignment.
For example, a double has an alignment of 8 according to natural alignment but on 32-bit processors, it would have no performance overhead if double had an alignment of 4 and it would have saved memory. This source#3.6.4 states that double has an alignment of 8 on 32-bit processors:
Align 64-bit data so that its base address is a multiple of eight.
Similar examples can be seen in 64-bit processors, 16 byte-sized data type(int128) has an alignment of 16 whereas it could have been beneficial to keep the alignment equal to the size of a processor word(i.e. 8 bytes long in 64-bit processors).
My guess is that this standard of natural alignment was created because when data was read directly from the wire, the machines could default to natural alignment and not have to deal with different alignments of the same data type according to the CPU architecture of the sender of the data.
When all the fields of a data structure are being stored in a single CPU word, they still have padding inside due to natural alignment whereas I do not think that padding is needed when all the fields are stored in a single CPU word because any field of the structure would take the same amount of byte shifts to access it regardless of where in the CPU word it is stored(please correct me on this if I am wrong).
For example, consider this struct:
struct example {
char i; // 1 byte
// 1 byte padding
short j; // 2 bytes
int k; // 4 bytes
char l; // 1 byte
// 3 bytes trailing padding
} foo;
The padding between foo.i
and foo.j
is not needed in my opinion because foo.j
would still need 6-byte shifts to access.
To summarize my question, I want to know what are the benefits of natural alignment over the processor-word-based alignment.
I also want to know whether inserting padding in between the CPU word where all your data is stored is any better than storing those fields without any padding. Also, does the position of fields in the same CPU word make any difference?