r/cpp • u/Middle_Ad4847 • Mar 30 '26
Iteratively optimizing an SPSC queue
I recently documented walking through the iterative process of optimizing a SPSC queue.
- Starting from
atomic<int64_t> sizewhich has thelockinstruction bottleneck. - Refactoring to separate atomic push, popInd. Fixing false sharing, 3 cacheline and 2 cacheline approach.
- Implementing index caching to minimize cross-core traffic and the impact of mm_pause
Link: https://blog.c21-mac.com/posts/spsc/
For reasons I don't understand the 3 cacheline performs considerably worse than 2 cacheline, I initially assumed it due to 128-byte-rule, but that doesn't explain L1d-cache misses being relatively higher in 3 cacheline vs 2 cacheline implementation. If anyone has any insights on what might be causing this or any feedback on the post, I would love to hear.
11
Upvotes
View all comments
Show parent comment
1
u/Middle_Ad4847 Mar 31 '26
Thanks, will take a look
Have added code here
I wasn't aware of this, will look. What I was referring to is the increased code size due to 32-byte displacement addressing, which can impact the µop cache or loopback buffer. I quickly tried with
alignas(128)now but it made results worse.I did try run with and without
-falign-loopsand-falign-labelsbut didn't notice any considerable difference