// 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{}.
package pool import ( ) // Pool reuses objects of type T by size class. type Pool[ any] struct { pools map[int]*sync.Pool } // 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 [ any](, int) *Pool[] { := &Pool[]{ pools: make(map[int]*sync.Pool), } for := ceilToPowerOfTwo(max(, 1)); <= ; <<= 1 { .pools[] = new(sync.Pool) } return } // 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[]) ( int) (, int) { var := ceilToPowerOfTwo() := .pools[] if == nil { return , } , := .Get().() return , } // Put stores x for reuse under the size class of size. It drops x if size is // not one of the pool's classes. func ( *Pool[]) ( , int) { if := .pools[]; != nil { .Put() } } var bytesPool = New[*[]byte](128, 65536) // 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 ( int) *[]byte { , := bytesPool.Get() if == nil { := make([]byte, , ) return & } * = (*)[:] return } // PutBytes stores bp for reuse by GetBytes(). Slices whose capacity is not a // power of two in the pool's range are dropped. func ( *[]byte) { bytesPool.Put(, cap(*)) } var ( readers = New[*bufio.Reader](256, 65536) writers = New[*bufio.Writer](256, 65536) ) // 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 ( int) *bufio.Reader { , := readers.Get() if == nil { return bufio.NewReaderSize(nil, ) } return } // PutReader stores br for reuse by GetReader(). It resets br so that the // pooled reader does not keep its source alive. func ( *bufio.Reader) { .Reset(nil) readers.Put(, .Size()) } // 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 ( int) *bufio.Writer { , := writers.Get() if == nil { return bufio.NewWriterSize(nil, ) } return } // PutWriter stores bw for reuse by GetWriter(). It resets bw so that the // pooled writer does not keep its destination alive. func ( *bufio.Writer) { .Reset(nil) writers.Put(, .Size()) } // ceilToPowerOfTwo returns the least power of two not less than n. For n of // two or less it returns n itself. func ceilToPowerOfTwo( int) int { if <= 2 { return } return 1 << bits.Len(uint(-1)) }