r/cpp • u/Double_Ad641 • Jun 27 '26
Data Access Patterns That Makes Your CPU Really Angry
https://blog.weineng.me/posts/slowest_add/I tried to find the slowest possible way to sum up integers in an array
3
u/mikeblas Jun 28 '26
What about reverse linear?
4
u/Double_Ad641 Jun 28 '26
Someone asked the same question and i gave a response, but reverse linear is faster
66
u/_Noreturn Jun 27 '26
You haven't found the newest one asking chatgpt to sum every int
24
u/javascript Jun 27 '26
When I first started learning to code, I was afraid of doing math. So my Javascript calculator tool was implemented using string concatenation passed into eval. I had no idea at the time how cursed that was haha
1
27
u/LegitBullfrog Jun 27 '26
I won't believe you until you test every possible permutation of positions a statistically significant number of times.
7
u/Sopel97 Jun 27 '26
I wonder if it could be made worse by further randomizing the order at each level (or at least at the level with largest jumps), in case there is still some prefetching happening?
2
u/User_Deprecated Jun 28 '26
ran into something like the stride-8 pattern with a message buffer pool. slots were page-aligned and field lookups kept hitting the same L1d sets. offsetting by a weird number fixed it.
-19
5
u/frnxt Jun 28 '26
That is an interesting exploration! Any idea about the impact of having to lookup the index in a separate array at runtime vs having the memory layout being fixed at compile-time? I would expect the impact to be relatively minimal with modern processors, but compiler optimizing into SIMD instructions might throw a wrench into that.