Skip to content

perf(circular-buffer): add lock-free SPSC variant#491

Merged
kcenon merged 1 commit into
mainfrom
perf/485-spsc-circular-buffer
Mar 19, 2026
Merged

perf(circular-buffer): add lock-free SPSC variant#491
kcenon merged 1 commit into
mainfrom
perf/485-spsc-circular-buffer

Conversation

@kcenon

@kcenon kcenon commented Mar 19, 2026

Copy link
Copy Markdown
Owner

Summary

  • Add SPSCCircularBuffer<T, Capacity> class for lock-free single-producer single-consumer scenarios
  • Uses std::atomic<size_t> head/tail with acquire/release ordering instead of mutex
  • Cache-line aligned (alignas(64)) head/tail to prevent false sharing
  • Existing CircularBuffer class is unchanged

Why

The mutex-based CircularBuffer has unnecessary synchronization overhead for SPSC use cases (e.g., logger batch processor queue, event pipelines). Lock-free SPSC eliminates mutex contention entirely.

Closes #485

Test plan

  • 9 new unit tests added (basic ops, wraparound, move semantics, capacity-of-one, concurrent stress)
  • All 26 tests pass locally (17 existing + 9 new)
  • CI passes on all platforms

Add SPSCCircularBuffer<T, Capacity> alongside the existing mutex-based
CircularBuffer. Uses std::atomic head/tail with acquire/release memory
ordering for lock-free operation when there is exactly one producer and
one consumer thread.

Key design decisions:
- Internal buffer has Capacity+1 slots to distinguish full from empty
  without an extra counter
- head_ and tail_ are cache-line aligned (alignas(64)) to avoid false
  sharing between producer and consumer threads
- Existing CircularBuffer class is unchanged

Includes 9 new tests covering basic ops, wraparound, move semantics,
capacity-of-one edge case, and a 100k-element concurrent SPSC stress test.
@codecov

codecov Bot commented Mar 19, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 0.00%. Comparing base (d0780ab) to head (8484537).
⚠️ Report is 11 commits behind head on main.

Additional details and impacted files
@@          Coverage Diff          @@
##            main    #491   +/-   ##
=====================================
  Coverage   0.00%   0.00%           
=====================================
  Files         22      22           
  Lines       2540    2542    +2     
=====================================
- Misses      2540    2542    +2     
Flag Coverage Δ
integration-tests 0.00% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@kcenon kcenon merged commit 4f53914 into main Mar 19, 2026
29 checks passed
@kcenon kcenon deleted the perf/485-spsc-circular-buffer branch April 4, 2026 05:31
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.

perf(circular-buffer): provide lock-free SPSC variant for single-producer single-consumer

1 participant