What is the locality of reference?

Operating SystemHardwareSoftware & Coding

Locality of reference refers to the tendency of the computer program to access the same set of memory locations for a particular time period. The property of Locality of Reference is mainly shown by loops and subroutine calls in a program.

On an abstract level there are two types of localities which are as follows −

  • Temporal locality

  • Spatial locality

Temporal locality

This type of optimization includes bringing in the frequently accessed memory references to a nearby memory location for a short duration of time so that the future accesses are much faster.

For example, if in an instruction set we have a variable declared that is being accessed very frequently we bring in that variable in a memory register which is the nearest in memory hierarchy for faster access.

Spatial locality

This type of optimization assumes that if a memory location has been accessed it is highly likely that a nearby/consecutive memory location will be accessed as well and hence we bring in the nearby memory references too in a nearby memory location for faster access.

For example, traversal of a one-dimensional array in any instruction set will benefit from this optimization.

Using these optimizations we can greatly improve upon the efficiency of the programs and can be implemented on hardware level or on software level.

Let us see the locality of reference relationship with cache memory and hit ratio.

Relationship with Cache memory

Cache is a specially designed faster but smaller memory area, generally used to keep recently referenced data and data near recently referenced data, which can lead to potential performance increases.

Data in cache does not necessarily correspond to data that is spatially close in main memory. However, data elements are brought into cache one cache line at a time. This means that spatial locality is again important. If one element is referenced, a few neighbour elements will also be brought into cache.

Finally, temporal locality plays a role on the lowest level, since results that are referenced very closely together can be kept in the machine registers. Programming languages such as C allow the programmer to suggest that certain variables are kept in registers.

Relationship with Hit ratio

Hit ratio is the concept defined for any two adjacent levels of a memory hierarchy. It is the probability that an information item will be found in the memory we are looking at.

So locality of reference needs a good Hit ratio for achieving Fast Access time.

Published on 26-Nov-2021 11:02:38