Skip to content

glycerine/rbuf

Repository files navigation

rbuf: a circular ring buffer in Golang

type FixedSizeRingBuf struct:

  • is a fixed-size circular ring buffer. Yes, just what is says. This structure is only for bytes, as it was written to optimize I/O, but could be easily adapted to any other type.

  • We keep a pair of ping/pong buffers so that we can linearize the circular buffer into a contiguous slice if need be.

For efficiency, a FixedSizeRingBuf may be vastly preferred to a bytes.Buffer. The ReadWithoutAdvance(), Advance(), and Adopt() methods are all non-standard methods written for speed.

For an I/O heavy application, I have replaced bytes.Buffer with FixedSizeRingBuf and seen memory consumption go from 8GB to 25MB. Yes, that is a 300x reduction in memory footprint. Everything ran faster too.

Note that Bytes(), while inescapable at times, is expensive: avoid it if possible. If all you need is len(Bytes()), then it is better to use the FixedSizeRingBuf.Readable member directly. Bytes() is expensive because it may copy the back and then the front of a wrapped buffer A[Use] into A[1-Use] in order to get a contiguous, unwrapped, slice. If possible use ContigLen() first to get the size that can be read without copying, Read() that amount, and then Read() a second time -- to avoid the copy.

copyright (c) 2014, Jason E. Aten

license: MIT

About

a small circular ring buffer library in go / golang

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages