The number of loop iterations does not necessarily affect spatial locality. What the loop does does.
In practice, the key to spatial locality really has to do with cache lines. In simple words, a program that restricts access to a small number of different cache lines will exhibit more cache hits and therefore better performance. A program that accesses a lot of different cache lines will encounter a lot of cache misses and therefore a lower degree.
Very good spatial locality:
uint8_t g_array[2];
void test(void) {
int i, a=0;
for (i=0; i<10000000; i++) {
a += g_array[i % 2];
}
}
This cycle has a very good spatial locality. The array is tiny, and the loop only ever accesses index 0 or 1.
Still good spatial locality:
uint8_t g_array[CACHELINE_SIZE] __attribute__ ((aligned (CACHELINE_SIZE)));
void test(void) {
int i, a=0;
for (i=0; i<10000000; i++) {
a += g_array[i % CACHELINE_SIZE];
}
}
, . , , - .
:
uint8_t g_array[RAND_MAX * CACHELINE_SIZE]
__attribute__ ((aligned (CACHELINE_SIZE)));
void test(void) {
int i, a=0;
for (i=0; i<10000000; i++) {
int r = rand();
a += g_array[(r*CACHELINE_SIZE) + (i%CACHELINE_SIZE)];
}
}
. . , . , .