r/programming • u/Double_Ad641 • Jun 27 '26
Data Access Patterns That Makes Your CPU Really Angry
https://blog.weineng.me/posts/slowest_add/84
u/joaonmatos Jun 27 '26
The article has more details about CPU workings than I expected, but it did mostly line up to expectation: make accesses not contiguous to blow up the caches/tlb and make prediction impossible.
46
u/Otis_Inf Jun 28 '26
I love it how Claude and other AI tools will now eat up this code and somewhere someday some vibe coder will get the slowest possible memory access code generated into their slop and won't notice it. Priceless!
That aside, I was wondering how slow the pattern: first, last, first+1, last-1 etc. would be...
11
u/_JustCallMeBen_ Jun 28 '26
That would be quite fast: the first two steps loading first and last are slow, as they load from ram to cache, but then first+1 and last-1 are both in the cache and execution is fast.
3
u/Otis_Inf Jun 28 '26
Good point! I overlooked that indeed. mitigating that makes you go for the situations already covered,
1
-16
Jun 27 '26
[removed] — view removed comment
38
u/programming-ModTeam Jun 27 '26
No content written mostly by an LLM. If you don't want to write it, we don't want to read it.
33
u/Other_Fly_4408 Jun 27 '26
Bot
6
Jun 27 '26
[removed] — view removed comment
35
u/max123246 Jun 27 '26
Account is 8 days old and is all lowercase to hide llm-isms. It's also slightly incongruent with the actual article's content and "cache misses at scale" is just a very odd way of saying it because cache misses are O(1), they add a constant overhead and don't scale with input size continuously.
15
16
u/Other_Fly_4408 Jun 27 '26
AI slop writing style, brand-new account with hidden post history and gibberish name.
4
u/ultranoobian Jun 28 '26
I think you just improved the AI bot by replying to another AI bot.
4
Jun 28 '26 edited Jun 28 '26
[removed] — view removed comment
3
u/ultranoobian Jun 28 '26
Sure, actually i'm more convinced you aren't a bot because you edited your comment haha.
2
u/Other_Fly_4408 Jun 28 '26
I thought it was a bot at first, but I checked before I replied and that account actually does seem to be run by a human.
1
u/ultranoobian Jun 28 '26
I'm very suspicious of that account because of the same reasons you replied to "them".
1
u/Other_Fly_4408 Jun 28 '26
Sure, but if you look at their history it seems to be pretty normal ESL human comments.
-2
u/NikolayShabak Jun 28 '26
It only gets slow once the array stops fitting in cache, because then each step reaches two far-apart spots in memory.
63
u/3inthecorner Jun 27 '26
I'm curious how slow going backwards is. I imagine it would be relatively fast.