You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
gosuv/vendor/github.com/glycerine/rbuf
李光春 97132fd582
update
2 years ago
..
.gitignore update 2 years ago
LICENSE update 2 years ago
README.md update 2 years ago
atomic_rbuf.go update 2 years ago
atomic_rbuf_test.go update 2 years ago
pbuf.go update 2 years ago
pbuf_test.go update 2 years ago
rbuf.go update 2 years ago
rbuf_test.go update 2 years ago

README.md

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