Direct mapping is a method of storing information for easy access on a computer. Recently used information is stored in a cache so the computer can quickly find the information the next time it is needed. In computer programming, a cache is a small section of random access memory (RAM) that is set aside for the purpose of easily retrieving data. With direct mapping, each piece of data in memory is assigned a space in the cache, which it shares with other pieces of data. Cache data is constantly being overwritten as new data is needed.
A cache is organized into lines. Each line is only large enough to store one block of data and a tag to identify where the data came from. When a user requests a piece of data, the computer first scans the cache to see if the information is there. If it is, the information is returned to the user. This is known as a cache hit, which is quicker than retrieving the data from its original location.
The percentage of requests that results in cache hits is called the hit rate. If the data the user requested is not in the cache, the computer will find it in memory. A copy of the data will be deposited into the cache so that it can be found quickly the next time the user requests it, in theory increasing the hit rate. All of this happens behind the scenes. The user does not know if the data received came from the cache or memory.
Direct mapping is one method of deciding where blocks of memory will be stored in the cache. Each block of memory is assigned a specific line in the cache. Since the cache is smaller than the memory, multiple blocks will share a single line in the cache. If a line is already full when a new block needs to be written to it, an old block will be overwritten.
Though direct mapping is a very simple and easy way to design a cache, it does present some problems. If a program continually accesses multiple blocks of data that share the same line in a direct mapping cache, the line will be rewritten often. This results in a lot of misses because the data the computer needs is less likely to be the data that is actually in that cache line at the moment. So direct mapping has a lower hit rate than other cache mapping models.