bufferpool

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jan 14, 2026 License: MIT Imports: 3 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Max

func Max(a, b int) int

Types

type BufferPool

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

func NewBufferPool

func NewBufferPool(left, right uint32) *BufferPool

NewBufferPool 创建一个内存池,复制自 `https://github.com/lxzan/gws/blob/main/internal/pool.go`

e.g. NewBufferPool(16, 200): &{16 65536 map[16:0xc000ad5bc0 32:0xc000ad5bf0 64:0xc000ad5c20 128:0xc000ad5c50 256:0xc000ad5c80]}

creates a memory pool left 和 right 表示内存池的区间范围,它们将被转换为 2 的 n 次幂 left and right indicate the interval range of the memory pool, they will be transformed into pow(2, n) 小于 left 的情况下,Get 方法将返回至少 left 字节的缓冲区;大于 right 的情况下,Put 方法不会回收缓冲区 Below left, the Get method will return at least left bytes; above right, the Put method will not reclaim the buffer

func (*BufferPool) Get

func (p *BufferPool) Get(n int) *bytes.Buffer

Get 从内存池中获取一个至少 n 字节的缓冲区 fetches a buffer from the memory pool, of at least n bytes

func (*BufferPool) Put

func (p *BufferPool) Put(b *bytes.Buffer)

Put 将缓冲区放回到内存池 returns the buffer to the memory pool

type Pool

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

Pool 是一个泛型内存池,使用 sync.Pool 实现

func NewPool

func NewPool[T any](newFn func() T, resetFn func(T)) *Pool[T]

NewPool 创建一个新的泛型内存池

func (*Pool[T]) Get

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

Get 从内存池中获取一个对象

func (*Pool[T]) Put

func (c *Pool[T]) Put(v T)

Put 将对象放回内存池

Jump to

Keyboard shortcuts

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