Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
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
var DefaultAdvancedBufferPool = NewAdvancedBufferPool(4096, 1024*1024)
DefaultAdvancedBufferPool 创建默认大小的高级缓冲区池
Functions ¶
Types ¶
type AdvancedBufferPool ¶
type AdvancedBufferPool struct {
// contains filtered or unexported fields
}
AdvancedBufferPool 是一个增强的字节缓冲区池 与基本的BufferPool相比,添加了更多便捷方法和性能优化
func NewAdvancedBufferPool ¶
func NewAdvancedBufferPool(initialSize, maxSize int) *AdvancedBufferPool
NewAdvancedBufferPool 创建一个新的高级缓冲区池
type BufferPool ¶
type BufferPool struct {
// contains filtered or unexported fields
}
BufferPool 是一个字节缓冲区池,专门优化用于处理字节数组
func NewBufferPool ¶
func NewBufferPool(capacity int) *BufferPool
NewBufferPool 创建一个新的字节缓冲区池 capacity 是缓冲区的初始容量
func (*BufferPool) GetWithSize ¶
func (p *BufferPool) GetWithSize(size int) []byte
GetWithSize 从池中获取一个指定大小的字节缓冲区
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的初始容量
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 创建一个新的消息对象池
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压力
type PooledBuffer ¶
PooledBuffer 是一个来自缓冲区池的缓冲区,提供了buffer.Buffer的所有功能 优化了序列化/反序列化过程的内存分配
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缓冲区池
type SlicePool ¶
type SlicePool[T any] struct { // contains filtered or unexported fields }
SlicePool 是一个用于管理切片的特殊对象池 相比于直接使用Pool[[]T],SlicePool可以更好地管理切片的容量
func NewSlicePool ¶
NewSlicePool 创建一个新的切片池 capacity 是切片的初始容量
func (*SlicePool[T]) GetWithSize ¶
GetWithSize 从池中获取一个切片,并设置其长度为size 所有元素都被初始化为零值
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]) GetStats ¶
func (p *TypedMessagePool[T]) GetStats() MessagePoolStats
GetStats 获取池统计信息