mempool

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Apr 2, 2025 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// Small4KPool 小型缓冲区池,适用于一般的小型JSON/Proto消息
	Small4KPool = NewAdvancedBufferPool(4096, 16384)

	// Medium64KPool 中型缓冲区池,适用于中等大小的消息
	Medium64KPool = NewAdvancedBufferPool(65536, 262144)

	// Large1MPool 大型缓冲区池,适用于大型消息
	Large1MPool = NewAdvancedBufferPool(1048576, 4194304)

	// TransferBufferPool 用于网络传输的4K缓冲区池
	TransferBufferPool = NewPreallocatedByteSlicePool(4096)
)

Common pools for frequent use cases

View Source
var DefaultAdvancedBufferPool = NewAdvancedBufferPool(4096, 1024*1024)

DefaultAdvancedBufferPool 创建默认大小的高级缓冲区池

Functions

func PutBuffer

func PutBuffer(buf *PooledBuffer)

PutBuffer 将缓冲区放回默认池

Types

type AdvancedBufferPool

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

AdvancedBufferPool 是一个增强的字节缓冲区池 与基本的BufferPool相比,添加了更多便捷方法和性能优化

func NewAdvancedBufferPool

func NewAdvancedBufferPool(initialSize, maxSize int) *AdvancedBufferPool

NewAdvancedBufferPool 创建一个新的高级缓冲区池

func (*AdvancedBufferPool) Get

func (p *AdvancedBufferPool) Get() *PooledBuffer

Get 从池中获取一个缓冲区

func (*AdvancedBufferPool) Put

func (p *AdvancedBufferPool) Put(buf *PooledBuffer)

Put 将缓冲区放回池中

type BufferPool

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

BufferPool 是一个字节缓冲区池,专门优化用于处理字节数组

func NewBufferPool

func NewBufferPool(capacity int) *BufferPool

NewBufferPool 创建一个新的字节缓冲区池 capacity 是缓冲区的初始容量

func (*BufferPool) Get

func (p *BufferPool) Get() []byte

Get 从池中获取一个字节缓冲区

func (*BufferPool) GetWithSize

func (p *BufferPool) GetWithSize(size int) []byte

GetWithSize 从池中获取一个指定大小的字节缓冲区

func (*BufferPool) Put

func (p *BufferPool) Put(buf []byte)

Put 将字节缓冲区放回池中

type MapPool

type MapPool[K comparable, V any] struct {
	// contains filtered or unexported fields
}

MapPool 是一个用于管理map的特殊对象池

func NewMapPool

func NewMapPool[K comparable, V any](capacity int) *MapPool[K, V]

NewMapPool 创建一个新的map池 capacity 是map的初始容量

func (*MapPool[K, V]) Get

func (p *MapPool[K, V]) Get() map[K]V

Get 从池中获取一个map

func (*MapPool[K, V]) Put

func (p *MapPool[K, V]) Put(m map[K]V)

Put 将map放回池中

type MessagePool

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

MessagePool 是一个泛型消息对象池,适用于Protocol Buffers消息等

func NewMessagePool

func NewMessagePool(factory func() interface{}, reset func(interface{}), options MessagePoolOptions) *MessagePool

NewMessagePool 创建一个新的消息对象池

func (*MessagePool) Close

func (p *MessagePool) Close()

Close 关闭池并释放资源

func (*MessagePool) Get

func (p *MessagePool) Get() interface{}

Get 从池中获取一个对象

func (*MessagePool) GetStats

func (p *MessagePool) GetStats() MessagePoolStats

GetStats 获取池统计信息

func (*MessagePool) Put

func (p *MessagePool) Put(obj interface{})

Put 将对象放回池中

type MessagePoolOptions

type MessagePoolOptions struct {
	// 最大池大小,超过此大小的对象会被回收
	MaxPoolSize int
	// 清理周期,多久清理一次过期对象
	CleanupInterval time.Duration
	// 对象过期时间
	TTL time.Duration
	// 预热大小,池初始化时预创建的对象数量
	PrewarmSize int
	// 采用引用计数机制
	UseRefCounting bool
	// 在获取对象后运行重置函数
	ResetOnGet bool
}

MessagePoolOptions 消息池配置选项

func DefaultMessagePoolOptions

func DefaultMessagePoolOptions() MessagePoolOptions

DefaultMessagePoolOptions 返回默认的消息池配置选项

type MessagePoolStats

type MessagePoolStats struct {
	Created         uint64
	Reused          uint64
	Destroyed       uint64
	ObjectsInPool   int
	MaxSize         int
	TotalAllocation uint64
}

MessagePoolStats 消息池统计信息

type Pool

type Pool[T any] struct {
	// contains filtered or unexported fields
}

Pool 是一个通用的对象池,用于减少频繁创建和销毁对象带来的GC压力

func New

func New[T any](new func() T) *Pool[T]

New 创建一个新的对象池 new 是一个创建新对象的函数

func (*Pool[T]) BatchGet

func (p *Pool[T]) BatchGet(count int) []T

BatchGet 从池中批量获取指定数量的对象

func (*Pool[T]) BatchPut

func (p *Pool[T]) BatchPut(objs []T)

BatchPut 批量将对象放回池中

func (*Pool[T]) Get

func (p *Pool[T]) Get() T

Get 从池中获取一个对象,如果池中没有可用对象,则创建一个新的

func (*Pool[T]) Put

func (p *Pool[T]) Put(obj T)

Put 将对象放回池中,以便后续重用

type PooledBuffer

type PooledBuffer struct {
	*bytes.Buffer
	// contains filtered or unexported fields
}

PooledBuffer 是一个来自缓冲区池的缓冲区,提供了buffer.Buffer的所有功能 优化了序列化/反序列化过程的内存分配

func GetBuffer

func GetBuffer() *PooledBuffer

GetBuffer 从默认池获取缓冲区

func (*PooledBuffer) ReadFrom

func (b *PooledBuffer) ReadFrom(r io.Reader) (n int64, err error)

ReadFrom 实现io.ReaderFrom接口,优化从Reader复制数据到缓冲区

func (*PooledBuffer) Release

func (b *PooledBuffer) Release()

Release 从PooledBuffer内部释放缓冲区

func (*PooledBuffer) WriteTo

func (b *PooledBuffer) WriteTo(w io.Writer) (n int64, err error)

WriteTo 实现io.WriterTo接口,优化从缓冲区复制数据到Writer

type PreallocatedByteSlicePool

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

PreallocatedByteSlicePool 预分配固定大小的字节切片池 用于避免频繁分配临时切片

func NewPreallocatedByteSlicePool

func NewPreallocatedByteSlicePool(sliceSize int) *PreallocatedByteSlicePool

NewPreallocatedByteSlicePool 创建一个新的预分配字节切片池

func (*PreallocatedByteSlicePool) Get

func (p *PreallocatedByteSlicePool) Get() []byte

Get 从池中获取一个预分配的字节切片

func (*PreallocatedByteSlicePool) Put

func (p *PreallocatedByteSlicePool) Put(b []byte)

Put 将字节切片放回池中

type ProtoBufferPool

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

ProtoBufferPool 是专门为协议缓冲区优化的池 用于高效处理Protocol Buffers序列化/反序列化

func NewProtoBufferPool

func NewProtoBufferPool(size int) *ProtoBufferPool

NewProtoBufferPool 创建一个新的Protocol Buffers缓冲区池

func (*ProtoBufferPool) Get

func (p *ProtoBufferPool) Get() *PooledBuffer

Get 从池中获取一个Proto缓冲区

func (*ProtoBufferPool) Put

func (p *ProtoBufferPool) Put(buf *PooledBuffer)

Put 将Proto缓冲区放回池中

type SlicePool

type SlicePool[T any] struct {
	// contains filtered or unexported fields
}

SlicePool 是一个用于管理切片的特殊对象池 相比于直接使用Pool[[]T],SlicePool可以更好地管理切片的容量

func NewSlicePool

func NewSlicePool[T any](capacity int) *SlicePool[T]

NewSlicePool 创建一个新的切片池 capacity 是切片的初始容量

func (*SlicePool[T]) Get

func (p *SlicePool[T]) Get() []T

Get 从池中获取一个切片,切片长度为0但容量不小于初始设置

func (*SlicePool[T]) GetWithSize

func (p *SlicePool[T]) GetWithSize(size int) []T

GetWithSize 从池中获取一个切片,并设置其长度为size 所有元素都被初始化为零值

func (*SlicePool[T]) Put

func (p *SlicePool[T]) Put(slice []T)

Put 将切片放回池中 注意:调用者应该确保不再持有对该切片的引用,因为切片可能被后续的Get操作获取并修改

type TypedMessagePool

type TypedMessagePool[T any] struct {
	// contains filtered or unexported fields
}

TypedMessagePool 提供类型安全的消息对象池

func NewTypedMessagePool

func NewTypedMessagePool[T any](options MessagePoolOptions) *TypedMessagePool[T]

NewTypedMessagePool 创建一个类型安全的消息对象池

func (*TypedMessagePool[T]) Close

func (p *TypedMessagePool[T]) Close()

Close 关闭池

func (*TypedMessagePool[T]) Get

func (p *TypedMessagePool[T]) Get() T

Get 获取指定类型的对象

func (*TypedMessagePool[T]) GetStats

func (p *TypedMessagePool[T]) GetStats() MessagePoolStats

GetStats 获取池统计信息

func (*TypedMessagePool[T]) Put

func (p *TypedMessagePool[T]) Put(obj T)

Put 归还对象到池中

Jump to

Keyboard shortcuts

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