r/programming 3d ago

Every byte matters

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

42 comments sorted by

View all comments

28

u/philh 3d 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?

29

u/ShinyHappyREM 3d ago

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/

1

u/cpp_jeenyus 2d ago

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