gophi/core/pool.go
kim (grufwub) 529d28e62e refactor sync.Pool usages, improve file reading perf, trim leading '/' in request
Signed-off-by: kim (grufwub) <grufwub@gmail.com>
2020-10-23 20:59:09 +01:00

109 lines
1.9 KiB
Go

package core
import (
"bufio"
"io"
"sync"
)
var (
connBufferedReaderPool *bufferedReaderPool
connBufferedWriterPool *bufferedWriterPool
fileBufferedReaderPool *bufferedReaderPool
fileBufferPool *bufferPool
)
type bufferPool struct {
pool sync.Pool
}
func newBufferPool(size int) *bufferPool {
return &bufferPool{
pool: sync.Pool{
New: func() interface{} {
return make([]byte, size)
},
},
}
}
func (bp *bufferPool) Get() []byte {
// Just return and cast a buffer
return bp.pool.Get().([]byte)
}
func (bp *bufferPool) Put(b []byte) {
// Just put back in pool
bp.pool.Put(b)
}
type bufferedReaderPool struct {
pool sync.Pool
}
func newBufferedReaderPool(size int) *bufferedReaderPool {
return &bufferedReaderPool{
pool: sync.Pool{
New: func() interface{} {
return bufio.NewReaderSize(nil, size)
},
},
}
}
func (bp *bufferedReaderPool) Get(r io.Reader) *bufio.Reader {
// Get a buffered reader from the pool
br := bp.pool.Get().(*bufio.Reader)
// Reset to use our new reader!
br.Reset(r)
// Return
return br
}
func (bp *bufferedReaderPool) Put(br *bufio.Reader) {
// We must reset again here to ensure
// we don't mess with GC with unused underlying
// reader.
br.Reset(nil)
// Put back in the pool
bp.pool.Put(br)
}
type bufferedWriterPool struct {
pool sync.Pool
}
func newBufferedWriterPool(size int) *bufferedWriterPool {
return &bufferedWriterPool{
pool: sync.Pool{
New: func() interface{} {
return bufio.NewWriterSize(nil, size)
},
},
}
}
func (bp *bufferedWriterPool) Get(w io.Writer) *bufio.Writer {
// Get a buffered writer from the pool
bw := bp.pool.Get().(*bufio.Writer)
// Reset to user our new writer
bw.Reset(w)
// Return
return bw
}
func (bp *bufferedWriterPool) Put(bw *bufio.Writer) {
// We must reset again here to ensure
// we don't mess with GC with unused underlying
// writer.
bw.Reset(nil)
// Put back in the pool
bp.pool.Put(bw)
}