Skip to content

RingBuffer.Resample on a part-filled buffer stretches the samples across the whole new length, inventing data and marking the buffer full #98

Closed

Description

@matt-edmondson

What's wrong

RingBuffer<T>.Resample(int length) (Containers/RingBuffer.cs, around lines 248–282) always pushes exactly length elements:

for (int i = 0; i < length; i++)
{
    double oldIndex = (double)i * (oldCount - 1) / Math.Max(length - 1, 1);
    ...
    PushBack(oldData[index]);
}

It maps oldCount (the number of valid elements) onto length (the new capacity). The XML docs describe resampling as old length → new length, for example when the sample rate changes. When the buffer isn't full, the few real samples get stretched to fill every slot, and the buffer comes back full.

Reproduction

Confirmed with a scratch console app against the net10.0 build:

Setup Call Result
new RingBuffer<int>(10), push 1, 2 Resample(10) (same length) Count = 10, [1,1,1,1,1,2,2,2,2,2]
capacity 1000, push 10 samples Resample(2000) Count = 2000
capacity 4, push 7 Resample(4) [7,7,7,7]

Why it matters

  • A history or metering buffer that is only partly filled at the moment of a rate change gets padded with duplicated samples and reports itself as full. Any consumer that reads Count or iterates then works on data that never existed.
  • Resample(Length) should be a no-op, but it changes both the contents and Count.
  • The existing RingBufferTests only resample full or empty buffers, so none of them catch this.

Suggested fix / acceptance criteria

Activity

  1. added
    bug
    on Oct 5, 2026
  2. matt-edmondson commented on Oct 5, 2026

    @matt-edmondson
    ContributorAuthor

    Triage

    Next step: scale the element count by the length ratio, interpolate only the valid samples, and add tests for part-filled buffers.


    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

bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions