Skip to content

Commit 40eb078

Browse files
committed
docs: update documentation for v1.1.0 MPSC release
1 parent d7212b8 commit 40eb078

2 files changed

Lines changed: 126 additions & 33 deletions

File tree

README.md

Lines changed: 86 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,27 @@
22

33
[![CI](https://github.com/codenimja/nimsync/actions/workflows/ci.yml/badge.svg)](https://github.com/codenimja/nimsync/actions/workflows/ci.yml)
44
[![Benchmark](https://github.com/codenimja/nimsync/actions/workflows/benchmark.yml/badge.svg)](https://github.com/codenimja/nimsync/actions/workflows/benchmark.yml)
5-
[![Nimble](https://img.shields.io/badge/nimble-v1.0.0-orange.svg)](https://nimble.directory/pkg/nimsync)
5+
[![Nimble](https://img.shields.io/badge/nimble-v1.1.0-orange.svg)](https://nimble.directory/pkg/nimsync)
66
[![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)](LICENSE)
77
[![Nim](https://img.shields.io/badge/nim-2.0.0%2B-yellow.svg?style=flat&logo=nim)](https://nim-lang.org)
88
![Peak](https://img.shields.io/badge/peak-615M_ops/sec-success)
99
![P99](https://img.shields.io/badge/p99_latency-31ns-blue)
1010
![Contention](https://img.shields.io/badge/contention-0%25-brightgreen)
1111

12-
**Lock-free SPSC channels for Nim with production-grade performance validation**
12+
**Lock-free SPSC and MPSC channels for Nim with production-grade performance validation**
1313

14-
nimsync v1.0.0 is production-ready for SPSC channels with comprehensive benchmarking following industry standards (Tokio, Go, LMAX Disruptor, Redis). Performance: 615M ops/sec peak throughput, 31ns P99 latency, stable under burst loads. This is verified, tested, real code.
14+
nimsync v1.1.0 is production-ready for both SPSC and MPSC channels with comprehensive benchmarking following industry standards (Tokio, Go, LMAX Disruptor, Redis). Performance: 615M ops/sec SPSC peak, 16M ops/sec MPSC (2 producers), 31ns P99 latency, stable under burst loads. This is verified, tested, real code.
1515

1616
## Features
1717

18-
- **High throughput**: 615M ops/sec (raw), 512K ops/sec (async) - [See all 7 benchmarks](#performance)
18+
- **High throughput**: 615M ops/sec SPSC (raw), 16M ops/sec MPSC (2 producers), 512K ops/sec (async) - [See benchmarks](#performance)
1919
- **Production-validated**: Comprehensive benchmark suite (throughput, latency, burst, stress, sustained)
2020
- **Industry-standard testing**: Following Tokio, Go, Rust Criterion, LMAX Disruptor methodologies
21+
- **SPSC and MPSC modes**: Single-producer or multi-producer with single consumer
2122
- Lock-free ring buffer with atomic operations
2223
- Zero GC pressure with ORC memory management
2324
- Cache-line aligned (64 bytes) to prevent false sharing
25+
- Wait-free MPSC algorithm (based on dbittman + JCTools patterns)
2426
- Power-of-2 sizing for efficient operations
2527
- Non-blocking `trySend`/`tryReceive`
2628
- Async `send`/`recv` wrappers for Chronos
@@ -45,7 +47,7 @@ nimble install
4547

4648
## Quick Start
4749

48-
### Basic Usage
50+
### Basic Usage (SPSC)
4951
```nim
5052
import nimsync
5153
@@ -61,6 +63,36 @@ if chan.tryReceive(value):
6163
echo "Received: ", value
6264
```
6365

66+
### Multi-Producer Usage (MPSC)
67+
```nim
68+
import nimsync
69+
import std/[os, threadpool]
70+
71+
# Create MPSC channel for multiple producers
72+
let chan = newChannel[int](1024, ChannelMode.MPSC)
73+
74+
# Multiple producer threads
75+
proc producer(ch: Channel[int], id: int) =
76+
for i in 0..<1000:
77+
while not ch.trySend(id * 1000 + i):
78+
discard # Spin until space available
79+
80+
# Start multiple producers
81+
spawn producer(chan, 1)
82+
spawn producer(chan, 2)
83+
spawn producer(chan, 3)
84+
85+
# Single consumer
86+
var count = 0
87+
var value: int
88+
while count < 3000:
89+
if chan.tryReceive(value):
90+
echo "Received: ", value
91+
count.inc
92+
93+
sync()
94+
```
95+
6496
### Async Operations
6597
```nim
6698
import nimsync
@@ -89,7 +121,10 @@ waitFor main()
89121
proc newChannel[T](size: int, mode: ChannelMode): Channel[T]
90122
```
91123
Creates a channel with specified size (rounded to next power of 2).
92-
Only `ChannelMode.SPSC` is implemented.
124+
125+
**Modes**:
126+
- `ChannelMode.SPSC`: Single Producer Single Consumer (fastest)
127+
- `ChannelMode.MPSC`: Multi-Producer Single Consumer (wait-free producers)
93128

94129
### Non-Blocking Operations
95130
```nim
@@ -115,7 +150,9 @@ proc isFull[T](channel: Channel[T]): bool
115150

116151
### Comprehensive Benchmark Suite
117152

118-
nimsync includes 7 official benchmarks following industry best practices:
153+
nimsync includes benchmarks for both SPSC and MPSC modes following industry best practices:
154+
155+
#### SPSC Benchmarks (Single Producer Single Consumer)
119156

120157
| Benchmark | Metric | Result | Industry Reference |
121158
|-----------|--------|--------|--------------------|
@@ -127,18 +164,36 @@ nimsync includes 7 official benchmarks following industry best practices:
127164
| **Sustained** | Long-duration | Stable over 10s | Cassandra/ScyllaDB |
128165
| **Async** | Overhead | 512K ops/sec | Standard async benchmarking |
129166

167+
#### MPSC Benchmarks (Multi-Producer Single Consumer)
168+
169+
| Producers | Throughput | Latency (avg) | Notes |
170+
|-----------|-----------|---------------|-------|
171+
| 1P | 33M ops/sec | 30ns | Comparable to SPSC |
172+
| 2P | 16M ops/sec | 62ns | Optimal sweet spot |
173+
| 4P | 9M ops/sec | 111ns | Good scalability |
174+
| 8P | 4M ops/sec | 250ns | Contention limited |
175+
176+
**Key Findings**:
177+
- Wait-free MPSC algorithm: No CAS retry loops
178+
- Best performance: 2 producers (16M ops/sec)
179+
- Stress test: 1M items across 8 producers (5.65M ops/sec)
180+
- Latency stable: 96-117ns across 1-4 producers
181+
130182
### Quick Run
131183
```bash
132-
# Run complete benchmark suite (~18 seconds)
184+
# Run SPSC benchmark suite (~18 seconds)
133185
./tests/performance/run_all_benchmarks.sh
134186

135-
# Run individual benchmarks
187+
# Run MPSC benchmarks
188+
nim c -d:danger --opt:speed --mm:orc tests/performance/benchmark_mpsc.nim
189+
./tests/performance/benchmark_mpsc
190+
191+
# Run individual SPSC benchmarks
136192
nim c -d:danger --opt:speed --mm:orc tests/performance/benchmark_latency.nim
137193
./tests/performance/benchmark_latency
138194
```
139195

140196
**Full Documentation**: See [`tests/performance/README.md`](tests/performance/README.md) for detailed explanations of each benchmark.
141-
```
142197

143198
### Third-Party Verification
144199

@@ -150,9 +205,10 @@ Want to verify these claims yourself?
150205

151206
## Limitations
152207

153-
1. **SPSC Only** - Single Producer Single Consumer only
154-
- Each channel: ONE sender, ONE receiver
155-
- MPSC/SPMC/MPMC will raise `ValueError`
208+
1. **Single Consumer Only** - All modes require single consumer
209+
- SPSC: ONE sender, ONE receiver (fastest)
210+
- MPSC: MULTIPLE senders, ONE receiver (wait-free)
211+
- SPMC/MPMC not implemented
156212

157213
2. **No close()** - Channels don't have close operation
158214
- Use sentinel values for shutdown signaling
@@ -164,12 +220,16 @@ Want to verify these claims yourself?
164220
- Starts at 1ms, backs off to 100ms max
165221
- Use `trySend`/`tryReceive` for zero-latency
166222

223+
5. **MPSC contention** - Best performance with 2-4 producers
224+
- 8+ producers experience diminishing returns due to contention
225+
167226
## Development
168227

169228
### Testing
170229
```bash
171-
nim c -r tests/unit/test_channel.nim # Basic tests
172-
nim c -r tests/unit/test_basic.nim # Version check
230+
nim c -r tests/unit/test_channel.nim # Basic SPSC tests
231+
nim c -r tests/unit/channels/test_mpsc_channel.nim # MPSC tests
232+
nim c -r tests/unit/test_basic.nim # Version check
173233
```
174234

175235
### Benchmarking
@@ -198,8 +258,8 @@ This repository contains experimental implementations of:
198258
## Roadmap
199259

200260
-**v1.0.0**: Production SPSC channels (DONE!)
201-
- **v1.1.0**: MPSC channels + TaskGroup fixes
202-
- **v1.2.0**: Production-ready Streams
261+
- **v1.1.0**: MPSC channels (DONE!)
262+
- **v1.2.0**: TaskGroup fixes + Production-ready Streams
203263
- **v2.0.0**: Full async runtime with actors
204264

205265
## Known Issues
@@ -208,18 +268,17 @@ See [GitHub Issues](.github/) for experimental features and known limitations:
208268

209269
- **Async wrappers use polling** - exponential backoff (1ms-100ms), use `trySend`/`tryReceive` for zero-latency
210270
- **TaskGroup has bugs** - nested async macros fail (not exported) - [See issue template](.github/ISSUE_TASKGROUP_BUG.md)
211-
- **MPSC not implemented** - multi-producer channels needed for actors - [See issue template](.github/ISSUE_MPSC_CHANNELS.md)
212271
- **NUMA untested** - cross-socket performance unknown - [See issue template](.github/ISSUE_NUMA_VALIDATION.md)
213272

214273
**These are documented limitations, not intentional behavior.** Contributions to fix welcome!
215274

216275
## Contributing
217276

218277
Contributions welcome! Priority areas:
219-
1. Fix TaskGroup nested async bug (blocking v0.3.0) - [Details](.github/ISSUE_TASKGROUP_BUG.md)
220-
2. Implement MPSC channels (enables actors) - [Details](.github/ISSUE_MPSC_CHANNELS.md)
221-
3. Validate NUMA performance - [Details](.github/ISSUE_NUMA_VALIDATION.md)
222-
4. Cross-platform support (macOS/Windows)
278+
1. Fix TaskGroup nested async bug - [Details](.github/ISSUE_TASKGROUP_BUG.md)
279+
2. Validate NUMA performance - [Details](.github/ISSUE_NUMA_VALIDATION.md)
280+
3. Cross-platform support (macOS/Windows)
281+
4. SPMC/MPMC channel implementations
223282

224283
See [issue templates](.github/) for detailed specifications and acceptance criteria.
225284

@@ -229,16 +288,17 @@ MIT License - see LICENSE for details.
229288

230289
---
231290

232-
**Status**: Production-ready SPSC channels with comprehensive validation. Other features (TaskGroup, MPSC, actors) are experimental - see [GitHub Issues](.github/) for contributor opportunities.
291+
**Status**: Production-ready SPSC and MPSC channels with comprehensive validation. Other features (TaskGroup, actors, streams) are experimental - see [GitHub Issues](.github/) for contributor opportunities.
233292

234293
---
235294

236295
## Disclaimer
237296

238-
**nimsync v1.0.0 is production-ready for SPSC channels.**
297+
**nimsync v1.1.0 is production-ready for SPSC and MPSC channels.**
239298

240-
**SPSC channels verified** - 615M ops/sec peak, 31ns P99 latency, 7-benchmark suite validation
241-
⚠️ **Experimental features** - TaskGroup, MPSC, actors not yet production-ready ([help wanted](.github/))
299+
**SPSC channels verified** - 615M ops/sec peak, 31ns P99 latency, 7-benchmark suite validation
300+
**MPSC channels verified** - 16M ops/sec (2 producers), wait-free algorithm, comprehensive stress testing
301+
⚠️ **Experimental features** - TaskGroup, actors, streams not yet production-ready ([help wanted](.github/))
242302

243303
We document performance honestly. We benchmark rigorously. We're transparent about limitations.
244304

tests/performance/README.md

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,22 @@ Official benchmark suite following industry best practices from Tokio, Go, and R
55
## Quick Start
66

77
```bash
8-
# Run all benchmarks
8+
# Run all SPSC benchmarks
99
./tests/performance/run_all_benchmarks.sh
1010

11-
# Or run individually
11+
# Run MPSC benchmarks
12+
nim c -d:danger --opt:speed --mm:orc tests/performance/benchmark_mpsc.nim
13+
./tests/performance/benchmark_mpsc
14+
15+
# Or run SPSC benchmarks individually
1216
nim c -d:danger --opt:speed --mm:orc tests/performance/benchmark_latency.nim
1317
./tests/performance/benchmark_latency
1418
```
1519

1620
## Benchmark Suite
1721

22+
## SPSC Benchmarks (Single Producer Single Consumer)
23+
1824
### 1. benchmark_spsc_simple.nim - Throughput (Baseline)
1925
**What it measures**: Raw SPSC channel throughput
2026
**Industry reference**: Standard practice for lock-free queue benchmarking
@@ -114,6 +120,32 @@ nim c -r tests/performance/benchmark_concurrent.nim
114120

115121
**Key insight**: Channel itself is 600M+ ops/sec, async wrapper adds polling overhead
116122

123+
## MPSC Benchmarks (Multi-Producer Single Consumer)
124+
125+
### 8. benchmark_mpsc.nim - Multi-Producer Performance
126+
**What it measures**: MPSC channel throughput, latency, and scalability
127+
**Industry reference**: JCTools MPSC queue benchmarking, Disruptor patterns
128+
**Results**: 16M ops/sec (2 producers), 9M ops/sec (4 producers), wait-free algorithm
129+
**Use case**: Concurrent producer scenarios (worker threads, event aggregation)
130+
131+
```bash
132+
nim c -d:danger --opt:speed --mm:orc tests/performance/benchmark_mpsc.nim
133+
./tests/performance/benchmark_mpsc
134+
```
135+
136+
**Benchmark suite includes**:
137+
- **Throughput comparison**: SPSC vs MPSC with 1/2/4/8 producers
138+
- **Latency measurement**: Average latency across different producer counts
139+
- **Scalability analysis**: Fixed items per producer, measuring scalability
140+
- **Size impact**: Performance across buffer sizes (64/256/1024/4096)
141+
- **Burst workload**: Handling bursty traffic patterns
142+
143+
**Key findings**:
144+
- **2 producers**: Optimal sweet spot (16M ops/sec)
145+
- **Wait-free algorithm**: No CAS retry loops, predictable latency
146+
- **Stress tested**: 1M items across 8 producers (5.65M ops/sec)
147+
- **Latency stability**: 96-117ns across 1-4 producers
148+
117149
## Design Principles
118150

119151
**Non-redundant**: Each benchmark measures a different aspect
@@ -126,11 +158,12 @@ nim c -r tests/performance/benchmark_concurrent.nim
126158

127159
| Category | Benchmarks | Purpose |
128160
|----------|-----------|---------|
129-
| **Throughput** | simple, concurrent | Raw performance numbers |
130-
| **Latency** | latency | Tail latency analysis |
131-
| **Stability** | burst, sustained | Real-world behavior |
132-
| **Tuning** | sizes | Optimization guidance |
133-
| **Limits** | stress | Breaking point analysis |
161+
| **Throughput** | simple, concurrent, mpsc | Raw performance numbers |
162+
| **Latency** | latency, mpsc | Tail latency analysis |
163+
| **Stability** | burst, sustained, mpsc | Real-world behavior |
164+
| **Tuning** | sizes, mpsc | Optimization guidance |
165+
| **Limits** | stress, mpsc | Breaking point analysis |
166+
| **Scalability** | mpsc | Multi-producer scaling |
134167

135168
## CI Integration
136169

0 commit comments

Comments
 (0)