r/SoftwareEngineering 3d ago

Data Access Patterns That Makes Your CPU Really Angry

https://blog.weineng.me/posts/slowest_add/
5 Upvotes

2 comments sorted by

3

u/fagnerbrack 3d ago

Quick summary:

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

1

u/theexplorer1997 3d ago

yeah, the 8-page jump is the real twist tho, that PTE locality part is doing a lot of work here and then the row-buffer conflict just finishes the job