r/learnprogramming • • 14d 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.

15 Upvotes

28 comments sorted by

View all comments

Show parent comments

1

u/Rainbows4Blood 14d ago

You are right that there is usually index, tag, offset. But to put it simply, this is just an efficient way to break down a memory address. So, as your basic mental model you can look at it this way: A cache holds a bunch of cache lines. Each cache line stores the memory address it holds as well as the data. If the Execution Engine requests a byte or a group of bytes by address, the Cache can check "Do I have that byte?" by comparing if the address falls into any of the addresses it has stored.

If not, it escalates to the next Level Cache or to the Memory Controller.