// Package pbufio contains tools for pooling bufio.Reader and bufio.Writers.
package pbufio import ( ) 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 { return DefaultWriterPool.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 { return DefaultReaderPool.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. type WriterPool struct { 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 } return bufio.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. type ReaderPool struct { 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 } return bufio.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()) }