Vixen
02b45cc4
csharp
public sealed class RingBuffer<T>

A fixed-capacity queue that overwrites its oldest entry when full: the log ring, the profiler's sample history, the input event queue.

No guide page documents this yet — the page shows what the code says about itself.

Remarks

Overwriting rather than growing is the point. A log that grows is a memory leak with a long fuse; a log that keeps the last ten thousand lines and drops the rest is a diagnostic tool with a fixed cost. TryEnqueue is there for the callers that would rather be told than lose the oldest entry.

Not thread-safe. The profiler writes from one thread per ring and reads them after a barrier; a lock on the write path would cost more than the samples are worth.

Fields and properties (6)

  • public int Capacity

    How many elements the buffer can hold.

  • public int Count

    How many elements it currently holds.

  • public bool IsFull

    Whether the buffer is at capacity, so the next write overwrites.

  • public bool IsEmpty

    Whether the buffer is empty.

  • public long OverwrittenCount

    How many elements have been dropped to make room, since the buffer was created.

  • public ref T this[int index]

    The element at , counting from the oldest.

Methods (8)

  • public RingBuffer(int capacity)

    Creates a buffer holding at most elements.

  • public void Enqueue(T item)

    Appends an element, dropping the oldest if the buffer is full.

  • public bool TryEnqueue(T item)

    Appends an element unless the buffer is full.

  • public bool TryDequeue(out T item)

    Removes and returns the oldest element.

  • public bool TryPeek(out T item)

    Reads the oldest element without removing it.

  • public void Clear()

    Empties the buffer. Does not reset OverwrittenCount.

  • public int CopyTo(Span<T> destination)

    Copies the elements out in order, oldest first.

  • public RingBuffer<T>.Enumerator GetEnumerator()

    Enumerates the elements oldest first.

Used by (7)

  • CollectionTestsVixen.Core.Collections.Tests
  • EnumeratorVixen.Core.Collections
  • ProfilerVixen.Core.Diagnostics
  • RemoteSinkVixen.Core.Diagnostics
  • RingBufferExtensionsVixen.Core.Collections.Tests
  • RingBufferSinkVixen.Core.Diagnostics
  • ThreadRecorderVixen.Core.Diagnostics