Documentation ¶
Index ¶
- type Allocator
- type Buffer
- func (b *Buffer) AddBytes(bytes []byte) []byte
- func (b *Buffer) AllocBytes(n int) []byte
- func (b *Buffer) AllocBytesWithSliceLocation(n int) ([]byte, SliceLocation)
- func (b *Buffer) Destroy()
- func (b *Buffer) GetSlice(loc SliceLocation) []byte
- func (b *Buffer) Reset()
- func (b *Buffer) TotalSize() int64
- type BufferOption
- type Limiter
- type Option
- type Pool
- type SliceLocation
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Buffer ¶
type Buffer struct {
// contains filtered or unexported fields
}
Buffer represents a buffer that can allocate []byte from its memory.
func (*Buffer) AllocBytes ¶
AllocBytes allocates bytes with the given length.
func (*Buffer) AllocBytesWithSliceLocation ¶
func (b *Buffer) AllocBytesWithSliceLocation(n int) ([]byte, SliceLocation)
AllocBytesWithSliceLocation is like AllocBytes, but it must allocate the buffer in the pool rather from go's runtime. The expected usage is after writing data into returned slice **we do not store the slice**, but only the SliceLocation. Later we can use the SliceLocation to get the slice again. When we have a large number of slices in memory this can reduce memory occupation. nil returned slice means allocation failed.
func (*Buffer) Destroy ¶
func (b *Buffer) Destroy()
Destroy releases all buffers to the pool. Caller must release the reference to the returned []byte or SliceLocation before calling Destroy.
func (*Buffer) GetSlice ¶
func (b *Buffer) GetSlice(loc SliceLocation) []byte
type BufferOption ¶
type BufferOption func(*Buffer)
BufferOption configures a buffer.
func WithBufferMemoryLimit ¶
func WithBufferMemoryLimit(limit uint64) BufferOption
WithBufferMemoryLimit approximately limits the maximum memory size of this Buffer. Due to it use blocks to allocate memory, the actual memory size is blockSize*ceil(limit/blockSize).
type Limiter ¶
type Limiter struct {
// contains filtered or unexported fields
}
Limiter will block on Acquire if the number it has acquired and not released exceeds the limit.
func NewLimiter ¶
NewLimiter creates a new Limiter with the given limit.
type Option ¶
type Option func(p *Pool)
Option configures a pool.
func WithAllocator ¶
WithAllocator specifies the allocator used by pool to allocate and free memory.
func WithBlockNum ¶
WithBlockNum configures how many blocks cached by this pool.
func WithBlockSize ¶
WithBlockSize configures the size of each block.
func WithPoolMemoryLimiter ¶
WithPoolMemoryLimiter controls the maximum memory returned to buffer. Note that when call AllocBytes with size larger than blockSize, the memory is not controlled by this limiter.
type Pool ¶
type Pool struct {
// contains filtered or unexported fields
}
Pool is like `sync.Pool`, which manages memory for all bytes buffers. You can use Pool.NewBuffer to create a new buffer, and use Buffer.Destroy to release its memory to the pool. Pool can provide fixed size []byte blocks to Buffer.
NOTE: we don't used a `sync.Pool` because when will sync.Pool release is depending on the garbage collector which always release the memory so late. Use a fixed size chan to reuse can decrease the memory usage to 1/3 compare with sync.Pool.
func (*Pool) NewBuffer ¶
func (p *Pool) NewBuffer(opts ...BufferOption) *Buffer
NewBuffer creates a new buffer in current pool. The buffer can gradually acquire memory from the pool and release all memory once it's not used.
type SliceLocation ¶
type SliceLocation struct {
// contains filtered or unexported fields
}
SliceLocation is like a reflect.SliceHeader, but it's associated with a Buffer. The advantage is that it's smaller than a slice, and it doesn't contain a pointer thus more GC-friendly.