// Package pbufio contains tools for pooling bufio.Reader and bufio.Writers.
package pbufioimport ()var (DefaultWriterPool = NewWriterPool(256, 65536)DefaultReaderPool = NewReaderPool(256, 65536))// GetWriter returns bufio.Writer whose buffer has at least size bytes.// Note that size could be ceiled to the next power of two.// GetWriter is a wrapper around DefaultWriterPool.Get().func ( io.Writer, int) *bufio.Writer { returnDefaultWriterPool.Get(, ) }// PutWriter takes bufio.Writer for future reuse.// It does not reuse bufio.Writer which underlying buffer size is not power of// PutWriter is a wrapper around DefaultWriterPool.Put().func ( *bufio.Writer) { DefaultWriterPool.Put() }// GetReader returns bufio.Reader whose buffer has at least size bytes. It returns// its capacity for further pass to Put().// Note that size could be ceiled to the next power of two.// GetReader is a wrapper around DefaultReaderPool.Get().func ( io.Reader, int) *bufio.Reader { returnDefaultReaderPool.Get(, ) }// PutReader takes bufio.Reader and its size for future reuse.// It does not reuse bufio.Reader if size is not power of two or is out of pool// min/max range.// PutReader is a wrapper around DefaultReaderPool.Put().func ( *bufio.Reader) { DefaultReaderPool.Put() }// WriterPool contains logic of *bufio.Writer reuse with various size.typeWriterPoolstruct { pool *pool.Pool}// NewWriterPool creates new WriterPool that reuses writers which size is in// logarithmic range [min, max].func (, int) *WriterPool {return &WriterPool{pool.New(, )}}// CustomWriterPool creates new WriterPool with given options.func ( ...pool.Option) *WriterPool {return &WriterPool{pool.Custom(...)}}// Get returns bufio.Writer whose buffer has at least size bytes.func ( *WriterPool) ( io.Writer, int) *bufio.Writer { , := .pool.Get()if != nil { := .(*bufio.Writer) .Reset()return }returnbufio.NewWriterSize(, )}// Put takes ownership of bufio.Writer for further reuse.func ( *WriterPool) ( *bufio.Writer) {// Should reset even if we do Reset() inside Get(). // This is done to prevent locking underlying io.Writer from GC. .Reset(nil) .pool.Put(, writerSize())}// ReaderPool contains logic of *bufio.Reader reuse with various size.typeReaderPoolstruct { pool *pool.Pool}// NewReaderPool creates new ReaderPool that reuses writers which size is in// logarithmic range [min, max].func (, int) *ReaderPool {return &ReaderPool{pool.New(, )}}// CustomReaderPool creates new ReaderPool with given options.func ( ...pool.Option) *ReaderPool {return &ReaderPool{pool.Custom(...)}}// Get returns bufio.Reader whose buffer has at least size bytes.func ( *ReaderPool) ( io.Reader, int) *bufio.Reader { , := .pool.Get()if != nil { := .(*bufio.Reader) .Reset()return }returnbufio.NewReaderSize(, )}// Put takes ownership of bufio.Reader for further reuse.func ( *ReaderPool) ( *bufio.Reader) {// Should reset even if we do Reset() inside Get(). // This is done to prevent locking underlying io.Reader from GC. .Reset(nil) .pool.Put(, readerSize())}
The pages are generated with Goldsv0.8.2. (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu.
PR and bug reports are welcome and can be submitted to the issue list.
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds.