r/programming
•
u/Double_Ad641
•
Jun 27 '26
•
2
Data Access Patterns That Makes Your CPU Really Angry
https://blog.weineng.me/posts/slowest_add/
332
Upvotes
View all comments
Show parent comment
34
u/Double_Ad641 Jun 28 '26 edited Jun 28 '26
interestingly backwards is 20% faster! `data` and `position` pointer are of a certain memory distance apart (i "malloc" them one after another". in the forward iterator, both pointers are advancing in the same lockstep, meaning they have to content for the same cache/tlb. for the backwards iterator, `data` and `position` increments in opposite direction and this de-corelate their addresses.
I am accessing data and positions at the same time, but a normal accumulator would only do `for (i in data)` or `for (i in reverse(data))`. for this, forward is about 5% faster, probably because the HW prefetcher is tuned for forward scan than reverse.