photon-ring 2.5.0

Ultra-low-latency SPMC/MPMC pub/sub using stamped ring buffers. Formally sound with atomic-slots feature. no_std compatible.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
// Copyright 2026 Photon Ring Contributors
// SPDX-License-Identifier: Apache-2.0

//! Tests that specifically exercise the `atomic-slots` feature — AtomicU64
//! stripe-based seqlock reads/writes. Covers partial stripes, multi-stripe
//! payloads, cache-line-boundary payloads, and concurrent stress scenarios.

#![cfg(feature = "atomic-slots")]

use photon_ring::{channel, channel_bounded, channel_mpmc, Pod, PublishError, TryRecvError};

// -------------------------------------------------------------------------
// Helper Pod types
// -------------------------------------------------------------------------

/// 7 bytes — NOT a multiple of 8. Tests partial-stripe handling.
/// `repr(C, packed)` suppresses trailing padding so size is truly 7.
#[repr(C, packed)]
#[derive(Clone, Copy, Debug, PartialEq)]
struct Odd {
    a: u32,
    b: u16,
    c: u8,
}

// SAFETY: Odd is #[repr(C)] with all numeric fields;
// every bit pattern is a valid Odd.
unsafe impl Pod for Odd {}

/// 48 bytes — 6 full u64 stripes. Tests multi-stripe read/write.
#[repr(C)]
#[derive(Clone, Copy, Debug, PartialEq)]
struct Large {
    f0: u64,
    f1: u64,
    f2: u64,
    f3: u64,
    f4: u64,
    f5: u64,
}

unsafe impl Pod for Large {}

/// 56 bytes — 7 u64 stripes, the maximum payload that fits in a single
/// 64-byte cache line alongside the 8-byte stamp.
#[repr(C)]
#[derive(Clone, Copy, Debug, PartialEq)]
struct CacheLine {
    f0: u64,
    f1: u64,
    f2: u64,
    f3: u64,
    f4: u64,
    f5: u64,
    f6: u64,
}

unsafe impl Pod for CacheLine {}

// -------------------------------------------------------------------------
// 1. Small payload (u8) — 1-byte partial stripe
// -------------------------------------------------------------------------

#[test]
fn test_small_payload_u8() {
    let (mut p, s) = channel::<u8>(4);
    let mut sub = s.subscribe();

    p.publish(0xAB);
    assert_eq!(sub.try_recv(), Ok(0xAB));

    // Multiple values through the ring.
    for i in 0u8..=255 {
        p.publish(i);
    }
    // Read them all back (ring size 4, so only the last 4 are available
    // without lag if we read fast enough — but we published synchronously
    // before subscribing reads, so lag will occur).
    let err = sub.try_recv().unwrap_err();
    match err {
        TryRecvError::Lagged { skipped } => {
            assert!(skipped > 0, "expected lag after overflow");
        }
        other => panic!("expected Lagged, got {other:?}"),
    }

    // Read remaining values — they should be valid u8s (no corruption).
    let mut count = 0;
    loop {
        match sub.try_recv() {
            Ok(_v) => count += 1,
            Err(TryRecvError::Empty) => break,
            Err(TryRecvError::Lagged { .. }) => {}
        }
    }
    assert!(count > 0, "should have received at least some u8 values");
}

// -------------------------------------------------------------------------
// 2. Odd-size payload — partial stripe handling
// -------------------------------------------------------------------------

#[test]
fn test_odd_size_payload() {
    // Verify the type is indeed not a multiple of 8 (7 bytes, packed).
    assert_eq!(core::mem::size_of::<Odd>(), 7);

    let (mut p, s) = channel::<Odd>(8);
    let mut sub = s.subscribe();

    let val = Odd {
        a: 0xDEAD_BEEF,
        b: 0xCAFE,
        c: 0x42,
    };
    p.publish(val);

    let received = sub.try_recv().unwrap();
    // Copy fields out to avoid unaligned references on the packed struct.
    let (ra, rb, rc) = (received.a, received.b, received.c);
    assert_eq!(ra, 0xDEAD_BEEF);
    assert_eq!(rb, 0xCAFE);
    assert_eq!(rc, 0x42);
    assert_eq!(received, val);

    // Multiple round-trips.
    for i in 0..100u32 {
        let v = Odd {
            a: i,
            b: (i & 0xFFFF) as u16,
            c: (i & 0xFF) as u8,
        };
        p.publish(v);
        assert_eq!(sub.try_recv(), Ok(v));
    }
}

// -------------------------------------------------------------------------
// 3. Large payload — multi-stripe (48 bytes = 6 stripes)
// -------------------------------------------------------------------------

#[test]
fn test_large_payload() {
    assert_eq!(core::mem::size_of::<Large>(), 48);

    let (mut p, s) = channel::<Large>(8);
    let mut sub = s.subscribe();

    let val = Large {
        f0: 0x0011_2233_4455_6677,
        f1: 0x8899_AABB_CCDD_EEFF,
        f2: u64::MAX,
        f3: 0,
        f4: 42,
        f5: 0xDEAD_BEEF_CAFE_BABE,
    };
    p.publish(val);

    let received = sub.try_recv().unwrap();
    assert_eq!(received, val);
    assert_eq!(received.f0, 0x0011_2233_4455_6677);
    assert_eq!(received.f5, 0xDEAD_BEEF_CAFE_BABE);

    // Cycle the ring multiple times.
    for i in 0..50u64 {
        let v = Large {
            f0: i,
            f1: i.wrapping_mul(3),
            f2: i.wrapping_mul(7),
            f3: i.wrapping_mul(11),
            f4: i.wrapping_mul(13),
            f5: i.wrapping_mul(17),
        };
        p.publish(v);
        assert_eq!(sub.try_recv(), Ok(v));
    }
}

// -------------------------------------------------------------------------
// 4. Cache-line payload (56 bytes = 7 stripes, max single cache line)
// -------------------------------------------------------------------------

#[test]
fn test_cache_line_payload() {
    assert_eq!(core::mem::size_of::<CacheLine>(), 56);

    let (mut p, s) = channel::<CacheLine>(8);
    let mut sub = s.subscribe();

    let val = CacheLine {
        f0: 1,
        f1: 2,
        f2: 3,
        f3: 4,
        f4: 5,
        f5: 6,
        f6: 7,
    };
    p.publish(val);

    let received = sub.try_recv().unwrap();
    assert_eq!(received, val);

    // Verify each field individually.
    assert_eq!(received.f0, 1);
    assert_eq!(received.f1, 2);
    assert_eq!(received.f2, 3);
    assert_eq!(received.f3, 4);
    assert_eq!(received.f4, 5);
    assert_eq!(received.f5, 6);
    assert_eq!(received.f6, 7);

    // Edge: all bits set.
    let all_ones = CacheLine {
        f0: u64::MAX,
        f1: u64::MAX,
        f2: u64::MAX,
        f3: u64::MAX,
        f4: u64::MAX,
        f5: u64::MAX,
        f6: u64::MAX,
    };
    p.publish(all_ones);
    assert_eq!(sub.try_recv(), Ok(all_ones));
}

// -------------------------------------------------------------------------
// 5. Cross-thread SPMC under atomic-slots — 100K messages, 2 subscribers
// -------------------------------------------------------------------------

#[test]
fn test_cross_thread_atomic_slots() {
    let (mut p, s) = channel::<u64>(1024);
    let mut s1 = s.subscribe();
    let mut s2 = s.subscribe();

    let n = 100_000u64;

    let writer = std::thread::spawn(move || {
        for i in 0..n {
            p.publish(i);
        }
    });

    let reader1 = std::thread::spawn(move || {
        let mut last = None;
        let mut count = 0u64;
        loop {
            match s1.try_recv() {
                Ok(v) => {
                    if let Some(prev) = last {
                        assert!(v > prev, "ordering violation: {prev} -> {v}");
                    }
                    last = Some(v);
                    count += 1;
                    if v == n - 1 {
                        break;
                    }
                }
                Err(TryRecvError::Empty) => core::hint::spin_loop(),
                Err(TryRecvError::Lagged { .. }) => {}
            }
        }
        count
    });

    let reader2 = std::thread::spawn(move || {
        let mut last = None;
        let mut count = 0u64;
        loop {
            match s2.try_recv() {
                Ok(v) => {
                    if let Some(prev) = last {
                        assert!(v > prev, "ordering violation: {prev} -> {v}");
                    }
                    last = Some(v);
                    count += 1;
                    if v == n - 1 {
                        break;
                    }
                }
                Err(TryRecvError::Empty) => core::hint::spin_loop(),
                Err(TryRecvError::Lagged { .. }) => {}
            }
        }
        count
    });

    writer.join().unwrap();
    let c1 = reader1.join().unwrap();
    let c2 = reader2.join().unwrap();

    assert!(c1 > 0, "reader1 should have received messages");
    assert!(c2 > 0, "reader2 should have received messages");
}

// -------------------------------------------------------------------------
// 6. Stress: 1M messages through a 4096-slot ring under atomic-slots
// -------------------------------------------------------------------------

#[test]
fn test_stress_1m_atomic_slots() {
    let (mut p, s) = channel::<u64>(4096);
    let mut sub = s.subscribe();
    let n = 1_000_000u64;

    let writer = std::thread::spawn(move || {
        for i in 0..n {
            p.publish(i);
        }
    });

    let reader = std::thread::spawn(move || {
        let mut expected = 0u64;
        let mut lag_total = 0u64;
        loop {
            match sub.try_recv() {
                Ok(v) => {
                    assert_eq!(v, expected, "corruption: expected {expected}, got {v}");
                    expected += 1;
                    if expected == n {
                        break;
                    }
                }
                Err(TryRecvError::Empty) => core::hint::spin_loop(),
                Err(TryRecvError::Lagged { skipped }) => {
                    lag_total += skipped;
                    expected += skipped;
                }
            }
        }
        lag_total
    });

    writer.join().unwrap();
    let lags = reader.join().unwrap();
    eprintln!("test_stress_1m_atomic_slots: lags = {lags}");
}

// -------------------------------------------------------------------------
// 7. MPMC under atomic-slots — 2 publishers, 1 subscriber, 10K each
// -------------------------------------------------------------------------

#[test]
fn test_mpmc_atomic_slots() {
    let (pub1, subs) = channel_mpmc::<u64>(4096);
    let pub2 = pub1.clone();
    let mut sub = subs.subscribe();
    let n = 10_000u64;

    let writer1 = std::thread::spawn(move || {
        for i in 0..n {
            pub1.publish(i * 2); // even numbers
        }
    });

    let writer2 = std::thread::spawn(move || {
        for i in 0..n {
            pub2.publish(i * 2 + 1); // odd numbers
        }
    });

    writer1.join().unwrap();
    writer2.join().unwrap();

    // Drain all available messages.
    let mut received = Vec::new();
    loop {
        match sub.try_recv() {
            Ok(v) => received.push(v),
            Err(TryRecvError::Empty) => break,
            Err(TryRecvError::Lagged { .. }) => {}
        }
    }

    // Separate streams and verify per-stream ordering.
    let mut evens = Vec::new();
    let mut odds = Vec::new();
    for &v in &received {
        assert!(v < 2 * n, "value {v} out of range");
        if v % 2 == 0 {
            evens.push(v / 2);
        } else {
            odds.push((v - 1) / 2);
        }
    }

    // Within each producer's stream, values must appear in order
    // (may have gaps from lag).
    for window in evens.windows(2) {
        assert!(
            window[1] > window[0],
            "even stream out of order: {} -> {}",
            window[0],
            window[1]
        );
    }
    for window in odds.windows(2) {
        assert!(
            window[1] > window[0],
            "odd stream out of order: {} -> {}",
            window[0],
            window[1]
        );
    }

    assert!(!received.is_empty(), "should have received some messages");
}

// -------------------------------------------------------------------------
// 8. Bounded channel under atomic-slots — backpressure verification
// -------------------------------------------------------------------------

#[test]
fn test_bounded_atomic_slots() {
    let (mut p, s) = channel_bounded::<u64>(4, 0);
    let mut sub = s.subscribe();

    // Fill the ring (4 slots).
    for i in 0..4u64 {
        p.try_publish(i).unwrap();
    }

    // Ring is full — backpressure must kick in.
    assert_eq!(p.try_publish(99), Err(PublishError::Full(99)));

    // Drain and verify values.
    for i in 0..4u64 {
        assert_eq!(sub.try_recv(), Ok(i));
    }
    assert_eq!(sub.try_recv(), Err(TryRecvError::Empty));

    // After draining, publisher can write again.
    for i in 100..104u64 {
        p.try_publish(i).unwrap();
    }

    // Full again.
    assert_eq!(p.try_publish(999), Err(PublishError::Full(999)));

    // Drain second batch.
    for i in 100..104u64 {
        assert_eq!(sub.try_recv(), Ok(i));
    }
    assert_eq!(sub.try_recv(), Err(TryRecvError::Empty));

    // Cross-thread bounded under atomic-slots: verify no corruption.
    let (mut p2, s2) = channel_bounded::<u64>(64, 0);
    let mut sub2 = s2.subscribe();
    let n = 10_000u64;

    let writer = std::thread::spawn(move || {
        for i in 0..n {
            p2.publish(i);
        }
    });

    let reader = std::thread::spawn(move || {
        for expected in 0..n {
            loop {
                match sub2.try_recv() {
                    Ok(v) => {
                        assert_eq!(
                            v, expected,
                            "bounded atomic-slots corruption at seq {expected}"
                        );
                        break;
                    }
                    Err(TryRecvError::Empty) => core::hint::spin_loop(),
                    Err(TryRecvError::Lagged { .. }) => {
                        panic!("bounded channel should never lag");
                    }
                }
            }
        }
    });

    writer.join().unwrap();
    reader.join().unwrap();
}