Memory reaches the processor in cache lines, 64-byte blocks on most machines. Touching one byte brings in its whole line, so data that is used together should sit together, and data that is not used should stay out of the way. Four habits follow:
- Order struct members by size, largest first. The compiler pads each member to its alignment, so
char, long long, char takes 24 bytes where long long, char, char takes 16. sizeof and alignof give the real numbers.
- Structure of arrays. A loop that reads only prices from an array of whole order structs drags every other field through the cache too. Keeping each field in its own array makes that loop read nothing but prices.
- Hot and cold split. Move the fields the hot path never reads (a client name, a comment) to a separate struct, so the hot one stays small.
- One line per writer. Counters written by different threads belong in different lines, or the line bounces between processors.
The tests measure what they can measure exactly here: sizes, alignments, addresses and the number of lines a loop touches. Timings in a browser tab would vary from run to run; these numbers do not.
Same members, different order: a third less memory, and four of them fit in a cache line instead of two and a bit.