Memory Locality

  1. Spatial locality happens when a program accesses memory locations that are close to each other within short time.
    • This occurs because data is often stored and accessed in contiguous blocks.
    • Example: When processing an array, elements are accessed sequentially. If the program accesses element A[i], it is likely to access A[i+1] or A[i-1] next, as they are stored in adjacent memory locations.
  2. Sequential locality is a specific case of spatial locality where the memory accesses follow a linear sequence.
    • This is particularly common in instruction fetching and certain data access patterns.
    • Example: The CPU fetching instructions from memory often reads them sequentially, executing one instruction and then moving to the next in memory order.

Understanding and leveraging these types of locality helps optimize system performance. For example, cache designs are structured to prioritize frequently accessed (temporal locality) and nearby (spatial and sequential locality) data to minimize costly access to slower memory levels.