I have read this blog and I am still unsure about the importance of locality. Why is locality important for cache performance? Is it because it leads to fewer cache misses? Furthermore, how is a program written in order to achieve good locality and hence good cache performance?
Asked
Active
Viewed 999 times
0
-
1Because without spatial and temporal locality, a cache line will have been evicted before you reference it again (assuming you touch other memory), or you won't ever have touched the cache line containing your data (compulsory miss). That's fundamental to how CPU caches work. See [Temporal vs Spatial Locality with arrays](//stackoverflow.com/q/16289423) for more. Hardware prefetching can expand the benefit of spatial locality across cache lines for sequential accesses... – Peter Cordes Apr 19 '18 at 14:33
1 Answers
1
Caches are smaller, and usually much smaller, than the main memories they are associated with. For example, on x86 chips, the L1 cache is typically 32 KiB, while memory sizes of 32 GiB or larger are common, which is more than a million times larger.
Without spatial locality, memory requests would be uniformly distributed in the memory of the application, and then given the very large ratios between memory size and cache size, the chance of hitting in the cache would be microscopic (about one in one million for the example above). So the cache hit rate would be microscopic and the cache would be useless.

BeeOnRope
- 60,350
- 16
- 207
- 386
-
Spatial locality also impacts TLB (and page table structure cache) hit rate. DRAM row "hits" (making an access to a row that is still open) also have lower latency. Some implementations also prefetch the adjacent block and some limit stride-based prefetch to within a page. – Apr 21 '18 at 11:35