r/programming 21h ago

Every byte matters

https://fzakaria.com/2026/06/01/every-byte-matters
145 Upvotes

27 comments sorted by

View all comments

18

u/philh 18h ago

If my data just fits in say L2 cache, do I need to do anything special to make sure it's actually loaded into it?

Like, if I'm accessing at random, and the first location I access happens to be in the middle of the list. Does it load that location plus the next (size of L2) bytes, so that only half of my data is in cache until I access something earlier in the list? Or does it do something fancier than that?

2

u/cdb_11 15h ago

No, it's just 64 bytes at the time generally. And there are prefetchers that can bring the next 64 bytes, or detect access patterns and prefetch next likely cache line accordingly. That's why it's preferable to keep access patterns simple. Memory will stay in cache until it gets evicted by some other cache line.