feat: Add MPSC (Multi-Producer Single-Consumer) channel support#1
Merged
Conversation
Add lock-free MPSC channel implementation alongside existing SPSC: - Implement wait-free MPSC algorithm based on dbittman/JCTools patterns - Use dual atomic counters (mpscHead + mpscCount) for wait-free operations - Add cache-line padding (64 bytes) to prevent false sharing - Dispatch trySend/tryReceive based on ChannelMode (SPSC/MPSC) Performance characteristics: - SPSC: 438M ops/sec (unchanged, micro-benchmark) - MPSC 1P: 10M ops/sec (single producer with MPSC overhead) - MPSC 2P: 16M ops/sec (optimal for 2 concurrent producers) - MPSC 4P: 9M ops/sec (acceptable with contention) - MPSC 8P: 4M ops/sec (high contention, memory bandwidth limited) Testing: - Unit tests: 40K multi-producer test, 1M stress test - Benchmarks: throughput, latency, scalability, burst workloads - All tests pass with correctness verified (no duplicates/missing items) Use cases: - Actor mailboxes with multiple senders - Work-stealing queues with 2-4 producers - Event loops with multi-threaded I/O submission
Owner
Author
|
feat: add MPSC channel support for multi-producer scenarios Implements lock-free Multi-Producer Single-Consumer channels alongside existing SPSC implementation, enabling safe concurrent access from multiple producer threads. Key features:
Performance:
Testing:
Use cases:
Breaking changes: None (backward compatible, opt-in via ChannelMode) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds lock-free MPSC (Multi-Producer Single-Consumer) channel implementation to complement the existing SPSC channels, enabling safe concurrent access from multiple producer threads.
Motivation
Current SPSC channels require single producer/single consumer semantics. Real-world use cases like actor mailboxes and work-stealing schedulers often need multiple threads to safely enqueue to a single consumer.
Implementation
Algorithm
mpscHead+mpscCount) eliminate CAS retry loopsAPI
Changes
ChannelModeenum withMPSCvariantChannel[T]typetrySendMPSCandtryReceiveMPSCwith proper memory orderingtrySend/tryReceiveAPIsPerformance
Benchmarks (realistic multi-threaded workloads)
SPSC micro-benchmark (tight loop, no threading): 438M ops/sec (peak 442M)
Analysis
Testing
Unit Tests (
tests/unit/channels/test_mpsc_channel.nim)Benchmarks (
tests/performance/benchmark_mpsc.nim)Run benchmarks:
Use Cases
1. Actor Mailboxes
2. Work-Stealing Scheduler
3. Multi-threaded I/O Event Loop
Breaking Changes
None. This is a backward-compatible addition:
ChannelModeparameterFuture Work
moAcquiremay be relaxable tomoRelaxed)Related
src/private/channel_spsc.nimChecklist