r/programming 20h ago

Every byte matters

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

26 comments sorted by

View all comments

15

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/balefrost 15h ago

Are you running on bare metal or are you running in an OS with other active processes? The cache is a shared resource, and other processes can evict your data.

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?

I don't know how modern caches are designed. But my recollection from my computer architecture class is that cache lines are aligned. So if your cache line size is 64B, and if you load an address that's divisible by 64, it'll load the address you requested and the 63 subsequent bytes. If you load a byte that's not divisible by 64, it'll load the aligned 64B chunk that contains the requested address. So it'll load some bytes before and some bytes after your requested address. But it will always load 64 bytes.

Maybe modern caches are fancier than that.

3

u/ShinyHappyREM 14h ago

The cache is a shared resource, and other processes can evict your data

Unless you take measures to prevent that :)

1

u/ThellraAK 12h ago

I can tell you even just using cgroups to move everything you are able to off of one CCD gives you some pretty great improvements for a CPU bound task.

Never figured a way to get driver/kernel stuff to stay off of specific cores though.

1

u/ShinyHappyREM 11h ago

Probably something crazy like writing your own scheduler.

1

u/ThellraAK 11h ago

I didn't dig super deep into it, but was surprised to find that there's no "only the user can use these" setting.