Search code examples
architecturespatialcpu-architecturetemporal

Is accessing the same array address repeatedly both spatial and temporal locality?


So I'm having questions about whether or not this portion of code has any spatial locality at all. I have a strong feeling it does have temporal locality. A peer of mine says there is some spatial locality, but I feel like it does not have spatial locality. The portion of code is this:

for (i = 0; i < S; i++) {
read A[2];
}

Solution

  • Spatial locality refers to accessing near by memory locations. In your example, the array is always indexed by a constant, hence will only access one memory location (to be precise it will access the cache line aligned address of the array index). Therefore it only has temporal locality.