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 ¶
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 ¶
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 ¶
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.
Types ¶
type Pool ¶
type Pool[T any] struct { // contains filtered or unexported fields }
Pool reuses objects of type T by size class.
func New ¶
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 ¶
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.