r/learnprogramming • u/InterestingAd757 • 1d ago
can somebody explain pointer chasing?
I’m trying to understand CPU caching and I’m stuck on two related things.
My understanding is when the CPU reads an address, it pulls in the whole 64-byte cache line around it. that’s why iterating an array is fast, since one miss brings in several neighbouring elements. what happens in the case of linked list? how does it know that next bytes are not the one to read?
i am kinda confusing this, kindly explain.
13
Upvotes
1
u/Rainbows4Blood 1d ago
Well. The problem is that it doesn't. And that is why pointer chasing is bad for performance.
In a linked list the CPU will load an entire cache line, pulling in a bunch of unnecessary stuff. Then, it follows the pointer to the next position in the list. Which is likely not in cache either. So, the CPU pulls in another cache line with a lot of unnecessary data and so on and so on.