Skip to content

Ultra-fast ring (circular) buffer based on mapping the underlying memory segment to two contiguous regions of virtual memory

License

Notifications You must be signed in to change notification settings

artelk/MagicRingBuffer

Repository files navigation

NuGet

MagicRingBuffer

Fast ring (circular) buffer based on mapping the underlying memory segment to two contiguous regions of virtual memory. Tested on Windows, Linux and macOS platforms.

Explanation of how it works:

Usage

var buffer = new RingBuffer<byte>(4096);

while(...)
{
    var writerSpan = buffer.WriterSpan; // or WriterMemory or WriterChunk
    //... <write M bytes to the writerSpan>
    buffer.AdvanceWriter(M); // let buffer know how much was written

    Debug.Assert(buffer.ReaderSpan.Length + buffer.WriterSpan.Length == buffer.Size); // always true

    var readerSpan = buffer.ReaderSpan; // or ReaderMemory or ReaderChunk
    //... <read N bytes from the readerSpan>
    buffer.AdvanceReader(N); // let buffer know how much was read/consumed

    Debug.Assert(buffer.ReaderSpan.Length + buffer.WriterSpan.Length == buffer.Size); // always true
}

About

Ultra-fast ring (circular) buffer based on mapping the underlying memory segment to two contiguous regions of virtual memory

Topics

Resources

License

Stars

Watchers

Forks

Languages