Skip to content

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

Closed

Description

@matt-edmondson

What's wrong

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:

Activity

  1. added
    enhancement
    on Nov 4, 2026
  2. matt-edmondson commented on Nov 4, 2026

    @matt-edmondson
    ContributorAuthor

    Triage


    Generated by Claude Code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

enhancement

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions