Skip to content

Conversation

@utkarshgupta137
Copy link
Contributor

@utkarshgupta137 utkarshgupta137 commented Aug 13, 2025

  • The first change is just minor code style changes to bring different implementations in sync.
  • In the 2nd change, I've moved is_closed check after is_full check. This way, you can optimistically push items until the queue becomes full & only then you'll know if the channel got closed. I've also added a is_closed flag so that you can manually check if the queue is closed from time to time.
  • In the 3rd change, I've changed the memory Orderings for the is_closed atomics to Relaxed. Since we don't use the values of these for indexing, we don't need use the more strict memory barrier.
  • In the 4th change, I've made it so that the single side of spsc/mpsc/spmc are able to cache the head or the tail of the other side to avoid an expensive load every time. So now, the sp push side can continue pushing without checking the tail up to the buffer len. Similarly, the sc side can continue popping elements until it has read all the elements from the last load.

All these changes add up to significant improvement in performance, especially the last change:

mpmc::ping_pong         time:   [276.64 ns 276.66 ns 276.68 ns]
                        change: [-20.062% -20.050% -20.037%] (p = 0.00 < 0.05)
mpmc::ping_pong_try     time:   [451.81 ns 451.83 ns 451.85 ns]
                        change: [+6.5298% +6.6577% +6.7261%] (p = 0.00 < 0.05)
mpsc::ping_pong         time:   [274.70 ns 274.73 ns 274.76 ns]
                        change: [-12.739% -12.726% -12.711%] (p = 0.00 < 0.05)
mpsc::ping_pong_try     time:   [466.36 ns 466.38 ns 466.41 ns]
                        change: [-4.5087% -4.4944% -4.4815%] (p = 0.00 < 0.05)
mpsc::ping_pong_std     time:   [7.3077 µs 7.3465 µs 7.3823 µs]
                        change: [+0.3924% +0.8050% +1.2300%] (p = 0.00 < 0.05)
spmc::ping_pong         time:   [205.56 ns 205.57 ns 205.58 ns]
                        change: [-37.772% -37.767% -37.762%] (p = 0.00 < 0.05)
spmc::ping_pong_try     time:   [276.08 ns 276.09 ns 276.10 ns]
                        change: [-27.241% -27.235% -27.229%] (p = 0.00 < 0.05)
spsc::ping_pong         time:   [220.11 ns 220.13 ns 220.15 ns]
                        change: [-26.017% -26.009% -26.001%] (p = 0.00 < 0.05)
spsc::ping_pong_try     time:   [266.36 ns 266.39 ns 266.42 ns]
                        change: [-29.223% -29.211% -29.195%] (p = 0.00 < 0.05)
spsc uncontended push   time:   [2.6856 ns 2.7026 ns 2.7216 ns]
                        change: [-66.540% -66.158% -65.745%] (p = 0.00 < 0.05)
spsc uncontended push (full)
                        time:   [1.0681 ns 1.0682 ns 1.0682 ns]
                        change: [-96.816% -96.771% -96.737%] (p = 0.00 < 0.05)
spsc uncontended pop    time:   [2.1455 ns 2.1457 ns 2.1458 ns]
                        change: [-95.614% -95.049% -94.406%] (p = 0.00 < 0.05)
spsc uncontended pop (empty)
                        time:   [947.97 ps 948.17 ps 948.38 ps]
                        change: [-10.788% -10.750% -10.712%] (p = 0.00 < 0.05)

@utkarshgupta137 utkarshgupta137 force-pushed the master branch 2 times, most recently from e77716c to 7f79f4b Compare August 14, 2025 01:35
@utkarshgupta137 utkarshgupta137 marked this pull request as draft August 14, 2025 01:35
@utkarshgupta137 utkarshgupta137 marked this pull request as ready for review August 14, 2025 19:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant