Spatial locality in a loop

From what I understand, spatial locality is related to neighboring memory used in the near future. However, I was wondering if a loop runs many times, does this lead to good spatial locality? Thanks in advance, and sorry if I find it hard to understand.

+4
source share
1 answer

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];      // Only ever accesses [0] or [1]
    }
}

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)];
    }
}

. . , . , .

+5

Source: https://habr.com/ru/post/1539381/


All Articles