channel<T> static method

(SpscSender<T>, SpscReceiver<T>) channel<T>(
  1. int capacity, {
  2. String? metricsId,
})

Creates an SPSC channel with the specified buffer capacity.

Parameters:

  • capacity: Buffer size (rounded up to next power-of-two internally)

Returns: A tuple of (sender, receiver) for 1:1 communication

Performance Notes:

  • Capacity should be a power-of-two for optimal performance
  • Larger buffers reduce contention but increase memory usage
  • Typical values: 256-8192 for high-frequency scenarios

Example:

final (tx, rx) = Spsc.channel<int>(capacity: 1024);

// Producer
for (int i = 0; i < 1000000; i++) {
  await tx.send(i);
}

// Consumer
await for (final value in rx.stream()) {
  print('Received: $value');
}

Implementation

static (SpscSender<T>, SpscReceiver<T>) channel<T>(int capacity,
    {String? metricsId}) {
  final core = _SpscCore<T>(SrswBuffer<T>(capacity), metricsId: metricsId);
  final tx = core.attachSender((c) => SpscSender<T>._(c));
  final rx = core.attachReceiver((c) => SpscReceiver<T>._(c));
  return (tx, rx);
}