Documentation ¶
Overview ¶
Package pool provides a sync.Pool equivalent that buckets incoming requests to one of 32 sub-pools, one for each power of 2, 0-32.
import ("github.com/bpfs/dep2p/util/pool") var p pool.BufferPool small := make([]byte, 1024) large := make([]byte, 4194304) p.Put(small) p.Put(large) small2 := p.Get(1024) large2 := p.Get(4194304) fmt.Println("small2 len:", len(small2)) fmt.Println("large2 len:", len(large2)) // Output: // small2 len: 1024 // large2 len: 4194304
Index ¶
- Constants
- Variables
- func Get(length int) []byte
- func Put(slice []byte)
- type Buffer
- func (b *Buffer) Bytes() []byte
- func (b *Buffer) Cap() int
- func (b *Buffer) Grow(n int)
- func (b *Buffer) Len() int
- func (b *Buffer) Next(n int) []byte
- func (b *Buffer) Read(buf []byte) (int, error)
- func (b *Buffer) ReadByte() (byte, error)
- func (b *Buffer) ReadFrom(r io.Reader) (int64, error)
- func (b *Buffer) Reset()
- func (b *Buffer) String() string
- func (b *Buffer) Truncate(n int)
- func (b *Buffer) Write(buf []byte) (int, error)
- func (b *Buffer) WriteByte(c byte) error
- func (b *Buffer) WriteString(buf string) (int, error)
- func (b *Buffer) WriteTo(w io.Writer) (int64, error)
- type BufferPool
- type Writer
- func (w *Writer) Available() int
- func (w *Writer) Buffered() int
- func (w *Writer) Close() error
- func (w *Writer) Flush() error
- func (w *Writer) Size() int
- func (w *Writer) Write(b []byte) (int, error)
- func (w *Writer) WriteByte(b byte) error
- func (w *Writer) WriteRune(r rune) (int, error)
- func (w *Writer) WriteString(s string) (int, error)
Constants ¶
const MaxLength = math.MaxInt32
MaxLength is the maximum length of an element that can be added to the Pool.
const MinRead = 512
MinRead is the minimum slice size passed to a Read call by Buffer.ReadFrom. As long as the Buffer has at least MinRead bytes beyond what is required to hold the contents of r, ReadFrom will not grow the underlying buffer.
const WriterBufferSize = 4096
Variables ¶
var GlobalPool = new(BufferPool)
GlobalPool is a static Pool for reusing byteslices of various sizes.
Functions ¶
Types ¶
type Buffer ¶
type Buffer struct { // Pool is the buffer pool to use. If nil, this Buffer will use the // global buffer pool. Pool *BufferPool // contains filtered or unexported fields }
Buffer is a buffer like bytes.Buffer that:
1. Uses a buffer pool. 2. Frees memory on read.
If you only have a few buffers and read/write at a steady rate, *don't* use this package, it'll be slower.
However:
- If you frequently create/destroy buffers, this implementation will be significantly nicer to the allocator.
- If you have many buffers with bursty traffic, this implementation will use significantly less memory.
func NewBuffer ¶
NewBuffer constructs a new buffer initialized to `buf`. Unlike `bytes.Buffer`, we *copy* the buffer but don't reuse it (to ensure that we *only* use buffers from the pool).
func NewBufferString ¶
NewBufferString is identical to NewBuffer *except* that it allows one to initialize the buffer from a string (without having to allocate an intermediate bytes slice).
func (*Buffer) Bytes ¶
Bytes returns the slice of bytes currently buffered in the Buffer.
The buffer returned by Bytes is valid until the next call grow, truncate, read, or write. Really, just don't touch the Buffer until you're done with the return value of this function.
func (*Buffer) Cap ¶
Cap returns the current capacity of the buffer.
Note: Buffer *may* re-allocate when writing (or growing by) `n` bytes even if `Cap() < Len() + n` to avoid excessive copying.
func (*Buffer) Grow ¶
Grow grows the internal buffer such that `n` bytes can be written without reallocating.
func (*Buffer) Next ¶
Next is an alternative to `Read` that returns a byte slice instead of taking one.
The returned byte slice is valid until the next read, write, grow, or truncate.
func (*Buffer) Read ¶
Read reads at most `len(buf)` bytes from the internal buffer into the given buffer.
func (*Buffer) String ¶
String returns the string representation of the buffer.
It returns `<nil>` the buffer is a nil pointer.
func (*Buffer) Truncate ¶
Truncate truncates the Buffer.
Panics if `n > b.Len()`.
This function may free memory by shrinking the internal buffer.
func (*Buffer) WriteString ¶
WriteString writes a string to the buffer.
This function is identical to Write except that it allows one to write a string directly without allocating an intermediate byte slice.
type BufferPool ¶
type BufferPool struct {
// contains filtered or unexported fields
}
BufferPool is a pool to handle cases of reusing elements of varying sizes. It maintains 32 internal pools, for each power of 2 in 0-32.
You should generally just call the package level Get and Put methods or use the GlobalPool BufferPool instead of constructing your own.
You MUST NOT copy Pool after using.
func (*BufferPool) Get ¶
func (p *BufferPool) Get(length int) []byte
Get retrieves a buffer of the appropriate length from the buffer pool or allocates a new one. Get may choose to ignore the pool and treat it as empty. Callers should not assume any relation between values passed to Put and the values returned by Get.
If no suitable buffer exists in the pool, Get creates one.
type Writer ¶
Writer is a buffered writer that returns its internal buffer in a pool when not in use.
func (*Writer) Close ¶
Close flushes the underlying writer and closes it if it implements the io.Closer interface.
Note: Close() closes the writer even if Flush() fails to avoid leaking system resources. If you want to make sure Flush() succeeds, call it first.
func (*Writer) Write ¶
Write writes the given byte slice to the underlying connection.
Note: Write won't return the write buffer to the pool even if it ends up being empty after the write. You must call Flush() to do that.