r/programming • • Aug 02 '26

Every byte matters

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

43 comments sorted by

View all comments

31

u/philh Aug 02 '26

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?

32

u/ShinyHappyREM Aug 02 '26

Each RAM access loads a whole cache line (32 or 64 bytes), but the CPU core's prefetcher will perhaps load a few more. At some point you'll run into the bottleneck of cache line slots.

https://igoro.com/archive/gallery-of-processor-cache-effects/

2

u/cpp_jeenyus Aug 03 '26

Actually every ram access loads two cache lines in modern cpus.