Add pool.Bytes for re-using byte slices
This adds a new pool type that allows byte slices to be re-used across clients. A sync.Pool can't be used in some cases because the the slices not in use end up getting garbage collected due to frequent GCs.pull/7348/head
parent
ac9a7d520b
commit
25508fa05f
|
@ -0,0 +1,40 @@
|
||||||
|
package pool
|
||||||
|
|
||||||
|
// Bytes is a pool of byte slices that can be re-used. Slices in
|
||||||
|
// this pool will not be garbage collected when not in use.
|
||||||
|
type Bytes struct {
|
||||||
|
pool chan []byte
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewBytes returns a Bytes pool with capacity for max byte slices
|
||||||
|
// to be pool.
|
||||||
|
func NewBytes(max int) *Bytes {
|
||||||
|
return &Bytes{
|
||||||
|
pool: make(chan []byte, max),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get returns a byte slice size with at least sz capacity.
|
||||||
|
func (p *Bytes) Get(sz int) []byte {
|
||||||
|
var c []byte
|
||||||
|
select {
|
||||||
|
case c = <-p.pool:
|
||||||
|
default:
|
||||||
|
return make([]byte, sz)
|
||||||
|
}
|
||||||
|
|
||||||
|
if cap(c) < sz {
|
||||||
|
return make([]byte, sz)
|
||||||
|
}
|
||||||
|
|
||||||
|
return c[:sz]
|
||||||
|
}
|
||||||
|
|
||||||
|
// Put returns a slice back to the pool. If the pool is full, the byte
|
||||||
|
// slice is discarded.
|
||||||
|
func (p *Bytes) Put(c []byte) {
|
||||||
|
select {
|
||||||
|
case p.pool <- c:
|
||||||
|
default:
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue