4

From what I understand, spacial locality has to do with nearby memory being used in the nearby future. However I was wondering if a loop is executed many times, does this lead to good spacial locality? Thanks in advance, and sorry if I'm hard to understand.

Jonathon Reinhart
  • 132,704
  • 33
  • 254
  • 328
  • Without code, this question cannot be answered. Some loops may exhibit strong spatial locality, while other may exhibit none whatsoever. – Jonathon Reinhart May 06 '14 at 05:33
  • Sorry for being too broad. Could you possibly give me an example of a type of loop that would be strong and one that would not? – user3277606 May 06 '14 at 05:35

1 Answers1

5

The number of iterations of a loop doesn't necessarily affect spatial locality. What the loop is doing does.

In practice, the key to spatial locality really has to do with cache lines. In simple terms, a program that limits its accesses to a small number of different cache lines will exhibit more cache hits, and thus better performance. A program that accesses a large number of different cache lines will encounter more cache misses, and thus lower peformance.

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 loop has very good spatial locality. The array is tiny, and the loop only ever accesses indices 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];
    }
}

Here we have an array that is aligned to exactly one cache line. Since the loop only accesses elements in that array, we can say it has good spatial locality - accesses will only ever touch that one cache line.


Poor spatial locality:

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

This loop has remarkably poor spatial locality. It is accessing random locations all over memory. Every loop iteration you can probably expect it to bounce to a different cache line. This will cause all kinds of cache misses, and the cache essentially becomes useless.

Jonathon Reinhart
  • 132,704
  • 33
  • 254
  • 328
  • 1
    @user3277606 Personally, I think it would be a *good thing* to leave this question without an accepted answer yet. It may help bring other opinions to the table. There are a lot of smart people here (especially with the `performance` tag I've added), and I'd like to hear their opinions as well. – Jonathon Reinhart May 06 '14 at 05:50
  • A good example to use is looping over a 2D array rows and columns vs. columns and rows vs. 4x4 squares. – Gabe May 06 '14 at 07:28