CACHE MEMORY
11
Cache Memory
Small amount of fast memory, expensive memory Sits between normal main memory (slower) and CPU May be located on CPU chip or module It keeps a copy of the most frequently used data from the main memory. Reads and writes to the most frequently used addresses will be serviced by the cache. We only need to access the slower main memory for less frequently used data.
Principle of Locality
In practice, most programs exhibit locality, which the
cache can take advantage of. The principle of temporal locality says that if a program accesses one memory address, there is a good chance that it will access the same address again. The principle of spatial locality says that if a program accesses one memory address, there is a good chance that it will also access other nearby addresses.
Temporal Locality in Programs and Data
Programs: Loops are excellent examples of temporal
locality in programs.
The loop body will be executed many times. The computer will need to access those same few locations of the instruction memory repeatedly.
Data: Programs often access the same variables over
and over, especially within loops.
Spatial Locality in Programs and Data
Programs: Nearly every program exhibits spatial
locality, because instructions are usually executed in sequenceif we execute an instruction at memory location i, then we will probably also execute the next instruction, at memory location i+1. Code fragments such as loops exhibit both temporal and spatial locality. Data: Programs often access data that is stored contiguously.
Arrays, like a in the code on the top, are stored in memory contiguously. The individual fields of a record or object like employee are also kept contiguously in memory.
5
How caches take advantage of temporal locality
The first time the processor reads from an address in main memory,
a copy of that data is also stored in the cache. The next time that same address is read, we can use the copy of the data in the cache instead of accessing the slower dynamic memory. So the first read is a little slower than before since it goes through both main memory and the cache, but subsequent reads are much faster.
How caches take advantage of spatial locality
When the CPU reads location i from main memory, a copy of that
data is placed in the cache. But instead of just copying the contents of location i, we can copy several values into the cache at once, such as the four bytes from locations i through i + 3.
If the CPU later does need to read from locations i + 1, i + 2 or i + 3, it can access that data from the cache and not the slower main memory. For example, instead of reading just one array element at a time, the cache might actually be loading four array elements at once.
Again, the initial load incurs a performance penalty, but were
gambling on spatial locality and the chance that the CPU will need the extra data.
Hits and Misses
A cache hit occurs if the cache contains the data that
were looking for.
cache can return the data much faster than main memory.
A cache miss occurs if the cache does not contain the
requested data.
CPU must then wait for the slower main memory.
Cache/Main Memory Structure
Cache read operation
CPU requests
contents of memory location Check cache for this data If present, get from cache (fast) If not present, read required block from main memory to cache Then deliver from cache to CPU Cache includes tags to identify which block of main memory is in each cache slot
10
Size of Cache Design
Cost
More cache is expensive
Speed
More cache is faster (up to a point) Checking cache for data takes time
11
ERROR: undefined OFFENDING COMMAND: f~ STACK: