pool

package
v1.5.0 Latest Latest
Warning

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

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

Documentation

Overview

Package pool reuses objects that are distinguishable by size: byte slices, bufio readers and writers and the like. Every power of two in a range has its own sync.Pool, and a request is served from the class of the least power of two not less than the requested size.

It is the part of github.com/gobwas/pool that ws uses, with generics instead of interface{}.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetBytes

func GetBytes(n int) *[]byte

GetBytes returns a pointer to a byte slice of length n, reused if possible.

The pointer, not the slice, is what the pool holds: a slice stored in a sync.Pool is boxed into an interface, which allocates its header on every Put. With a pointer the warm path does not allocate at all.

func GetReader

func GetReader(size int) *bufio.Reader

GetReader returns a bufio.Reader whose buffer has at least size bytes, reused if possible. It reads from nothing until the caller calls Reset().

func GetWriter

func GetWriter(size int) *bufio.Writer

GetWriter returns a bufio.Writer whose buffer has at least size bytes, reused if possible. It writes to nothing until the caller calls Reset().

func PutBytes

func PutBytes(bp *[]byte)

PutBytes stores bp for reuse by GetBytes(). Slices whose capacity is not a power of two in the pool's range are dropped.

func PutReader

func PutReader(br *bufio.Reader)

PutReader stores br for reuse by GetReader(). It resets br so that the pooled reader does not keep its source alive.

func PutWriter

func PutWriter(bw *bufio.Writer)

PutWriter stores bw for reuse by GetWriter(). It resets bw so that the pooled writer does not keep its destination alive.

Types

type Pool

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

Pool reuses objects of type T by size class.

func New

func New[T any](lo, hi int) *Pool[T]

New creates a Pool with a size class for every power of two in [lo, hi]. lo is rounded up to a power of two first; a lo of zero means one.

func (*Pool[T]) Get

func (p *Pool[T]) Get(size int) (T, int)

Get returns a pooled object whose size class is the least power of two not less than size, and that class size, to be passed to Put() later and to be used as the allocation size when there is nothing to reuse.

The object is the zero value of T when the class is empty or when size is out of the pool's range; the returned size is then size itself.

func (*Pool[T]) Put

func (p *Pool[T]) Put(x T, size int)

Put stores x for reuse under the size class of size. It drops x if size is not one of the pool's classes.

Jump to

Keyboard shortcuts

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