-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuffer_pool.go
47 lines (42 loc) · 1.26 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
package xun
import (
"bytes"
)
// BufferPool is a pool of *bytes.Buffer for reuse to reduce memory alloc.
type BufferPool struct {
c chan *bytes.Buffer
}
// NewBufferPool returns a new BufferPool with the given size.
//
// The size determines how many buffers can be stored in the pool. If the
// pool is full and a new buffer is requested, a new buffer will be created.
func NewBufferPool(size int) (bp *BufferPool) {
return &BufferPool{
c: make(chan *bytes.Buffer, size),
}
}
// Get retrieves a buffer from the pool or creates a new one if the pool is empty.
//
// If a buffer is available in the pool, it is returned for reuse, reducing memory
// allocations. If the pool is empty, a new buffer is created and returned.
func (bp *BufferPool) Get() (b *bytes.Buffer) {
select {
case b = <-bp.c:
// reuse existing buffer
default:
// create new buffer
b = bytes.NewBuffer([]byte{})
}
return
}
// Put returns a buffer to the pool for reuse or discards if the pool is full.
//
// This function resets the buffer to clear any existing data before
// returning it to the pool. If the pool is already full, the buffer is discarded.
func (bp *BufferPool) Put(b *bytes.Buffer) {
b.Reset()
select {
case bp.c <- b:
default: // Discard the buffer if the pool is full.
}
}