-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuffer_pool.go
62 lines (54 loc) · 1.44 KB
/
buffer_pool.go
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package main
import (
"sync"
)
// BufferPool specifies an interface to fetch and return resources from a cache
// pool.
type BufferPool interface {
Get(int64) []byte
Put([]byte)
}
// bufferPool implements a simple unbounded cache for reusing []byte of a
// specified size.
type bufferPool struct {
size int64
pool *sync.Pool
}
// NewBufferPool initializes a new BufferPool which will return []byte slice of
// the specified size.
func NewBufferPool(size int64) BufferPool {
return &bufferPool{
size: size,
pool: &sync.Pool{
New: func() any {
return make([]byte, size)
},
},
}
}
// Get returns a []byte slice of the specified length, resizing it (shrinking
// or reallocating) if necessary. The slice should be returned via Put when
// the caller has finished with it.
func (p *bufferPool) Get(size int64) []byte {
buf := p.pool.Get().([]byte)
if int64(len(buf)) < size {
if n := size - int64(cap(buf)); n > 0 {
// old buf capacity was too small, discard the old buf
// and create a new one to eventually return to the
// pool
buf = make([]byte, size)
} else {
// buf had capacity, resize in place
buf = buf[0:size]
}
} else if int64(len(buf)) > size {
// buf was too large, shrink it to the desired size
buf = buf[0:size]
}
return buf
}
// Put returns a []byte slice to be added back to the cache pool to become
// available from another call to Get.
func (p *bufferPool) Put(b []byte) {
p.pool.Put(b)
}