Skip to content

RingBuffer exposes no length or capacity, so callers can't tell whether the next PushBack evicts, and can't write Resample(buffer.Length * 2) #148

Closed

Description

@matt-edmondson

What's wrong

RingBuffer<T> stores its logical length but keeps it private:

  • Containers/RingBuffer.cs:58: private int Capacity { get; set; }
  • Containers/RingBuffer.cs:63: private int Length { get; set; }
  • Containers/RingBuffer.cs:68: public int Count { get; private set; }, the only size member that is public

Code that receives a RingBuffer<T> can see only Count. It cannot read the length the buffer was built with (new RingBuffer<T>(length), Resize(length), Resample(length)), so it cannot tell a part-filled buffer from a full one.

The sibling containers do expose a capacity: SpscRingBuffer<T>.Capacity (SpscRingBuffer.cs:85), DelayLine.Capacity (DelayLine.cs:72) and ContiguousCollection<T>.Capacity (ContiguousCollection.cs:67).

Concrete failure scenarios

  1. Sliding window "is it warmed up yet?" Here is the common moving-average pattern:
    void OnSample(RingBuffer<float> window, float x)
    {
        window.PushBack(x);
        if (window.Count == /* window length? */) { Publish(window.Average()); }
    }
    There is nothing to compare Count against. The caller has to carry the length alongside the buffer and keep that copy in sync through every Resize/Resample.
  2. Will this push evict? PushBack (RingBuffer.cs:172) silently drops the oldest element once Count == Length. A caller that wants to process or persist the element before it is lost cannot detect that state.
  3. Relative resizing. buffer.Resample(buffer.Length * 2) or buffer.Resize(buffer.Length / 2) (RingBuffer.cs:252, :274) cannot be written. A part-filled buffer makes this worse: Count is not the length, so Resample(Count * 2) gives a different, wrong result.

Suggested fix / acceptance criteria

  • Add a public read-only int Length (or Capacity, to match the siblings; it is the logical length, not the power-of-two backing size) that returns the value passed to the constructor, Resize or Resample.
  • Optionally add bool IsFull => Count == Length.
  • Keep the power-of-two backing size private.
  • Tests: Length after each constructor overload, after Resize, after Resample, and after a failed Resize (which leaves the buffer unchanged); IsFull false while filling and true from the Length-th push on.

Activity

  1. added
    enhancement
    on Nov 6, 2026
  2. matt-edmondson commented on Nov 6, 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