membuf

package
v0.0.0-...-503c688 Latest Latest
Warning

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

Go to latest
Published: Nov 8, 2023 License: Apache-2.0, Apache-2.0 Imports: 0 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 the reuse buffer.

func (*Buffer) AddBytes

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

AddBytes adds the bytes into this Buffer.

func (*Buffer) AllocBytes

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

AllocBytes allocates bytes with the given length.

func (*Buffer) Destroy

func (b *Buffer) Destroy()

Destroy frees all buffers.

func (*Buffer) Reset

func (b *Buffer) Reset()

Reset resets the buffer.

func (*Buffer) TotalSize

func (b *Buffer) TotalSize() int64

TotalSize represents the total memory size of this Buffer.

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 WithBlockSize

func WithBlockSize(size int) Option

WithBlockSize configures the size of each block.

func WithLargeAllocThreshold

func WithLargeAllocThreshold(threshold int) Option

WithLargeAllocThreshold configures the threshold for large allocation of a Buffer. If allocate size is larger than this threshold, bytes will be allocated directly by the make built-in function and won't be tracked by the pool.

func WithPoolSize

func WithPoolSize(size int) Option

WithPoolSize configures how many blocks cached by this pool.

type Pool

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

Pool is like `sync.Pool`, which manages memory for all bytes buffers.

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() *Buffer

NewBuffer creates a new buffer in current pool.

func (*Pool) TotalSize

func (p *Pool) TotalSize() int64

TotalSize is the total memory size of this Pool.

Jump to

Keyboard shortcuts

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