Inspired by this recent question on SO and the answers given, which made me feel very ignorant, I decided I'd spend some time to learn more about CPU caching and wrote a small program to verify whether I am getting this whole thing right (most likely not, I'm afraid). I'll first write down the assumptions that underlie my expectations, so you could possibly stop me here if those are wrong. Based on what I've read, in general:
- An
n-way associative cache is divided intossets, each containingnlines, each line having a fixed sizeL; - Each main memory address
Acan be mapped into any of thencache lines of one set; - The set into which address
Ais mapped can be found by splitting the address space into slots each of the size of one cache line, then computing the index ofA's slot (I = A / L), and finally performing a modulo operation to map the index into the target setT(T = I % s); - A cache read miss causes a higher delay than a cache write miss, because the CPU is less likely to stall and stay idle while waiting for the main memory line to be fetched.
My first question is: are these assumptions correct?
Assuming they are, I tried to play a bit with these concepts so I could actually see them having a concrete impact on a program. I wrote a simple test that allocates a memory buffer of B bytes and repeatedly accesses locations of that buffer with fixed increments of a given step from the beginning of the buffer (meaning that if B is 14 and the step is 3, I repeatedly visit only l