You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
SpscRingBuffer(1024) allocates 2048 slots (Capacity 2047): the reserved empty slot is no longer needed since #131, so every power-of-two buffer is twice its size #140
The SpscRingBuffer<T> constructor (Containers/SpscRingBuffer.cs:98-102) sizes its array as NextPower2(capacity + 1) and then sets Capacity = arrayLength - 1. The extra slot exists to tell a full buffer from an empty one when head and tail are masked indices.
Since #131 made head and tail free-running counters, "full" is just tail - head == arrayLength and "empty" is tail == head. Nothing needs the reserved slot any more.
Effect
Requested capacity
Capacity
Array length
4
7
8
1024
2047
2048
4096
8191
8192
2^30
throws
—
Requesting 2^30 throws because MaxCapacity is 2^30 - 1 (line 57).
The buffer is aimed at real-time audio and telemetry, where buffer sizes are almost always powers of two. Every such buffer therefore takes twice the memory and cache footprint it needs.
Suggested fix
Use arrayLength = NextPower2(capacity) and Capacity = arrayLength, and keep the full check as tail - head == Capacity.
Raise MaxCapacity to 2^30.
Update the remark about the reserved slot. The constructor doc already only promises "at least capacity", so no caller relies on the extra slot.
Acceptance:
new SpscRingBuffer<T>(1024).Capacity == 1024 with a 1024-element array;
write/read tests at exactly full and at exactly empty still pass;
Priority: Low. The buffer behaves correctly; it just doubles its array for every power-of-two request, and requesting 2^30 throws. That matters for the real-time audio and telemetry use case, but no caller is broken today. The constructor already promises only "at least capacity", so shrinking the array to the request is safe.
Area / suggested assignment: The SpscRingBuffer<T> constructor, MaxCapacity and the reserved-slot remark (Containers/SpscRingBuffer.cs:57, 98-102).
Next step: Use arrayLength = NextPower2(capacity) with Capacity = arrayLength, raise MaxCapacity to 2^30, and update the remark. Re-run the exactly-full, exactly-empty and Keep SpscRingBuffer head and tail free-running so Count can't pair a stale lap [patch] #131 multi-threaded Count tests, since the full check now depends on the counters alone. Be careful that tail - head can't be confused at exactly 2^30 with int counters.
Activity
matt-edmondson commentedon Nov 4, 2026
Triage
capacity", so shrinking the array to the request is safe.SpscRingBuffer<T>constructor,MaxCapacityand the reserved-slot remark (Containers/SpscRingBuffer.cs:57, 98-102).OverflowExceptionfor sizes above 2^30, and a failedRingBuffer.Resizecorrupts the buffer #89 / PR Reject ring buffer sizes above 2^30 with ArgumentOutOfRangeException [patch] #114. SpscRingBuffer and DelayLine have no benchmarks, though the benchmarking plan requires one per container and marks coverage as complete #106 (no SpscRingBuffer benchmarks) would be a natural place to show the cache-footprint win.arrayLength = NextPower2(capacity)withCapacity = arrayLength, raiseMaxCapacityto 2^30, and update the remark. Re-run the exactly-full, exactly-empty and Keep SpscRingBuffer head and tail free-running so Count can't pair a stale lap [patch] #131 multi-threaded Count tests, since the full check now depends on the counters alone. Be careful thattail - headcan't be confused at exactly 2^30 with int counters.Generated by Claude Code