1
0
buffer/pool.go

37 lines
484 B
Go
Raw Permalink Normal View History

2026-02-17 16:58:00 +01:00
package buffer
2026-02-22 18:45:57 +01:00
import "sync"
2026-02-22 16:30:37 +01:00
type noPool struct {
2026-02-17 16:58:00 +01:00
allocSize int
}
2026-02-22 18:45:57 +01:00
type pool struct {
sp *sync.Pool
}
func newPool(allocSize int) *pool {
sp := &sync.Pool{
New: func() any {
return make([]byte, allocSize)
},
}
2026-02-17 16:58:00 +01:00
2026-02-22 18:45:57 +01:00
return &pool{sp: sp}
2026-02-17 16:58:00 +01:00
}
func (p noPool) Get() ([]byte, error) {
return make([]byte, p.allocSize), nil
}
func (noPool) Put([]byte) {
}
func (p *pool) Get() ([]byte, error) {
2026-02-22 18:45:57 +01:00
return p.sp.Get().([]byte), nil
2026-02-17 16:58:00 +01:00
}
func (p *pool) Put(b []byte) {
2026-02-22 18:45:57 +01:00
p.sp.Put(b)
2026-02-17 16:58:00 +01:00
}