Locality of Reference

The tendency of a processor to access the same memory block again and again is called locality of reference. Mean to say if CPU refers a memory block now or location now then the same memory block or location will be referenced again in the future. In this way, hit chances are increased, and the Miss penalty is reduced.

There are two types of Locality of Reference

  • Spatial
  • Temporal

1. Spatial Locality of Reference

The spatial locality of reference is totally concerned with the Space of the Cache line.

The Rule of Spatial: If a word of a block is accessed now, then the word adjacent to it of the same Block will access the next.

Keeping more words in the block affects the spatial locality. Because we load the entire block in the cache, and if the block contains a large number of words, the chances of a cache hit will be increased.

2. Temporal Locality of Reference

It is totally concerned with time. This means that if the cache is full and we want to add a new block, the least recently used block is replaced.

The Rule of Temporal: If the word is referenced now, then the same word of that Block will be referenced again in the future.

LRU is used in the temporal locality.

Tip:  CPU generates an address of Byte or word (which exists inside Block) of Main Memory. Then, the whole Block (having that Byte or Word from main memory) is copied to Cache Memory. Then, the CPU gets that byte or word from the cache.