bytes.go
936 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
// Package pool provides pool structures to help reduce garbage collector pressure.
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. Items
// returned may not be in the zero state and should be reset by the
// caller.
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:
}
}