r/programming • u/Double_Ad641 • Jun 27 '26
Data Access Patterns That Makes Your CPU Really Angry
https://blog.weineng.me/posts/slowest_add/
332
Upvotes
r/programming • u/Double_Ad641 • Jun 27 '26
61
u/3inthecorner Jun 27 '26
I'm curious how slow going backwards is. I imagine it would be relatively fast.
85
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.
47
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...
-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.
-16
u/[deleted] Jun 27 '26
[removed] — view removed comment