As an example, suppose that a function uses local variables i and j as subscripts into a 2-dimensional array. They might be declared as follows:
int i, j;
These variables are commonly used together. But they can fall in different cache lines, which could be detrimental to performance. You can instead declare them as follows:
__declspec(align(8)) struct { int i, j; } sub;
The compiler now ensures that they are allocated in the same cache line.
In C++, you can omit the struct variable name (written as sub in the above example). In C, however, it is required, and you must write references to i and j as sub.i and sub.j.
Partager