|
|
@@ -4,39 +4,29 @@ import (
|
|
|
"sync"
|
|
|
)
|
|
|
|
|
|
-// Pool provides functionality to generate and recycle buffers on demand.
|
|
|
-type Pool struct {
|
|
|
- allocator *sync.Pool
|
|
|
-}
|
|
|
+const (
|
|
|
+ // Size of a regular buffer.
|
|
|
+ Size = 2 * 1024
|
|
|
+)
|
|
|
|
|
|
-// NewPool creates a SyncPool with given buffer size.
|
|
|
-func NewPool(bufferSize uint32) *Pool {
|
|
|
- pool := &Pool{
|
|
|
- allocator: &sync.Pool{
|
|
|
- New: func() interface{} { return make([]byte, bufferSize) },
|
|
|
- },
|
|
|
+func createAllocFunc(size uint32) func() interface{} {
|
|
|
+ return func() interface{} {
|
|
|
+ return make([]byte, size)
|
|
|
}
|
|
|
- return pool
|
|
|
}
|
|
|
|
|
|
-// Allocate either returns a unused buffer from the pool, or generates a new one from system.
|
|
|
-func (p *Pool) Allocate() *Buffer {
|
|
|
- return &Buffer{
|
|
|
- v: p.allocator.Get().([]byte),
|
|
|
- pool: p,
|
|
|
- }
|
|
|
+var pool2k = &sync.Pool{
|
|
|
+ New: createAllocFunc(2 * 1024),
|
|
|
}
|
|
|
|
|
|
-// // Free recycles the given buffer.
|
|
|
-func (p *Pool) Free(buffer *Buffer) {
|
|
|
- if buffer.v != nil {
|
|
|
- p.allocator.Put(buffer.v)
|
|
|
- }
|
|
|
+var pool8k = &sync.Pool{
|
|
|
+ New: createAllocFunc(8 * 1024),
|
|
|
}
|
|
|
|
|
|
-const (
|
|
|
- // Size of a regular buffer.
|
|
|
- Size = 2 * 1024
|
|
|
-)
|
|
|
+var pool64k = &sync.Pool{
|
|
|
+ New: createAllocFunc(64 * 1024),
|
|
|
+}
|
|
|
|
|
|
-var mediumPool = NewPool(Size)
|
|
|
+var pool128k = &sync.Pool{
|
|
|
+ New: createAllocFunc(128 * 1024),
|
|
|
+}
|