Documentation ¶
Overview ¶
Package buf manages allocation of temporary short-term buffers.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Buf ¶
type Buf struct { Data []byte // contains filtered or unexported fields }
Buf represents allocated slice of memory pool. At the end of using the buffer, Release() must be called to reclaim memory.
type Pool ¶
type Pool struct {
// contains filtered or unexported fields
}
Pool manages allocations of short-term data buffers from a pool.
Note that buffers managed by the pool are meant to be extremely short lived and are suitable for in-memory operations, such as encryption, compression, etc, but not for I/O buffers of any kind. It is EXTREMELY important to always release memory allocated from the Pool. Failure to do so will result in memory leaks.
The pool uses N segments, with each segment tracking its high water mark usage.
Allocation simply advances the high water mark within first segment that has capacity and increments per-segment refcount.
On Buf.Release() the refcount is decremented and when it hits zero, the entire segment becomes instantly freed.
As an extra optimization, when Buf.Release() is called in LIFO order, it will also lower the high water mark making its memory available for immediate reuse.
If no segment has available capacity, the pool waits a few times until memory becomes released and falls back to allocating from the heap.
func NewPool ¶
NewPool creates a buffer pool, composed of fixed-length segments of specified maximum size.
func (*Pool) SetSegmentSize ¶
SetSegmentSize sets the segment size for future segments that will be created.