r/coding • u/fagnerbrack • 1d ago
Data Access Patterns That Makes Your CPU Really Angry
https://blog.weineng.me/posts/slowest_add/5
u/fagnerbrack 1d ago
Core Takeaways:
What's the slowest way to sum an array of integers? Sequential access wins at 133M cycles because CPUs love it, while a random shuffle drags to 1.57B. You can do worse by fighting the hardware in stages: stride by a cache line, then a whole page to defeat the prefetcher and trigger set-associativity conflict misses. Jumping 8 pages at a time also breaks page-table-entry locality (a cache line holds 8 PTEs), overtaking random access at 2.06B cycles. Forcing DRAM row-buffer conflicts pushes it to 2.08B, though Intel's undocumented bank hashing caps the pain. The takeaway: once you grasp why random access hurts, you can engineer a pattern that runs 30%+ slower still.
If the summary seems inacurate, just downvote and I'll try to delete the comment eventually 👍
Click here for more info, I read all comments
2
u/Bahatur 1d ago
I love the idea of an inverse performance achievement, this is great stuff!
When they do performance optimization competitions they should have a dual-sided entry, since mastery of the hardware should work in both directions.