Documentation
¶
Overview ¶
Package rebytes provides types that recycle bytes slices to reduce allocation and garbage collection.
Index ¶
- Variables
- type Buffer
- func (b *Buffer) Chunks() int
- func (b *Buffer) Free()
- func (b *Buffer) Read(p []byte) (n int, err error)
- func (b *Buffer) ReadAt(p []byte, off int64) (n int, err error)
- func (b *Buffer) String() string
- func (b *Buffer) Write(p []byte) (n int, err error)
- func (b *Buffer) WriteString(s string) (n int, err error)
- type Pool
Constants ¶
This section is empty.
Variables ¶
var ErrTooLarge = errors.New("rebytes.Buffer: too large")
ErrTooLarge is passed to panic if memory cannot be allocated to store data in a buffer.
Functions ¶
This section is empty.
Types ¶
type Buffer ¶
type Buffer struct {
// contains filtered or unexported fields
}
Buffer is a dynamic buffer of bytes satisfying the Reader and Writer interfaces. Memory is managed by a rebytes.Pool: new memory is allocated from the pool as needed for writign and returned to the pool after reading.
func (*Buffer) Free ¶
func (b *Buffer) Free()
Free recycles byte slices back into the associated pool. The buffer is unusable afterwards.
func (*Buffer) Read ¶
Read reads the next len(p) unread bytes into p. It returns the number of bytes read (0 <= n <= len(p)) and any error encountered. When there is too-little data to fill p, the error will be io.EOF.
func (*Buffer) ReadAt ¶
ReadAt reads len(p) bytes into p starting at offset off in the buffer. It returns the number of bytes read (0 <= n <= len(p)) and any error encountered. When there is too-little data to fill p, the error will be io.EOF.
type Pool ¶
type Pool struct {
// contains filtered or unexported fields
}
Pool represents a goroutine-safe pool of []byte values of a configurable capacity. Because it contains a sync.Mutex, it must not be copied.
func NewPool ¶
NewPool constructs a new pool of []byte. The first argument is the capacity for []byte values retrieved from the pool. The second argument is the maximum number of unused []byte values that can be stored in the pool.
func (*Pool) Get ¶
Get returns a []byte with zero length and the pool's configured []byte capacity. The []byte value is removed from the pool if available or constructed on the spot otherwise.
func (*Pool) Put ¶
Put places a []byte into the pool. The pool takes ownership and the provided []byte MUST NOT be used again until returned from the pool with Get(). The []byte is resliced to zero length. NOTE: data is not zeroed out, only the length.
If the argument is nil or is a []byte with a capacity that isn't the same as the pool's configured []byte capacity, an error is returned and the argument is not added to the pool.