membuf

package
v1.1.0-beta.0...-9b70321 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 26, 2024 License: Apache-2.0, Apache-2.0 Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Allocator

type Allocator interface {
	Alloc(n int) []byte
	Free([]byte)
}

Allocator is the abstract interface for allocating and freeing memory.

type Buffer

type Buffer struct {
	// contains filtered or unexported fields
}

Buffer represents a buffer that can allocate []byte from its memory.

func (*Buffer) AddBytes

func (b *Buffer) AddBytes(bytes []byte) []byte

AddBytes adds the bytes into this Buffer's managed memory and return it.

func (*Buffer) AllocBytes

func (b *Buffer) AllocBytes(n int) []byte

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

func (*Buffer) Reset

func (b *Buffer) Reset()

Reset resets the buffer, the memory is still retained in this buffer. Caller must release the reference to the returned []byte or SliceLocation before calling Reset.

func (*Buffer) TotalSize

func (b *Buffer) TotalSize() int64

TotalSize represents the total memory size of this Buffer.

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

func NewLimiter(limit int) *Limiter

NewLimiter creates a new Limiter with the given limit.

func (*Limiter) Acquire

func (l *Limiter) Acquire(n int)

Acquire acquires n tokens from the limiter. If the number of tokens acquired and not released exceeds the limit, it will block until enough tokens are released.

func (*Limiter) Release

func (l *Limiter) Release(n int)

Release releases tokens to the limiter. If there are goroutines waiting for tokens, it will wake them up.

type Option

type Option func(p *Pool)

Option configures a pool.

func WithAllocator

func WithAllocator(allocator Allocator) Option

WithAllocator specifies the allocator used by pool to allocate and free memory.

func WithBlockNum

func WithBlockNum(num int) Option

WithBlockNum configures how many blocks cached by this pool.

func WithBlockSize

func WithBlockSize(bytes int) Option

WithBlockSize configures the size of each block.

func WithPoolMemoryLimiter

func WithPoolMemoryLimiter(limiter *Limiter) Option

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 NewPool

func NewPool(opts ...Option) *Pool

NewPool creates a new pool.

func (*Pool) Destroy

func (p *Pool) Destroy()

Destroy frees all buffers.

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.

func (*Pool) TotalSize

func (p *Pool) TotalSize() int64

TotalSize is the total memory size of this Pool, not considering its Buffer.

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL