Rework bufpool to be a little more efficient, I hope

This commit is contained in:
PhatPhuckDave
2024-07-23 14:55:33 +02:00
parent aff4ffe070
commit a9827f10ce
3 changed files with 77 additions and 68 deletions

View File

@@ -6,7 +6,7 @@ import (
type BufferPoolManager struct { type BufferPoolManager struct {
pools map[uint]*sync.Pool pools map[uint]*sync.Pool
mu sync.Mutex mu sync.RWMutex
} }
func NewBufferPoolManager() *BufferPoolManager { func NewBufferPoolManager() *BufferPoolManager {
@@ -15,9 +15,15 @@ func NewBufferPoolManager() *BufferPoolManager {
} }
} }
func (bpm *BufferPoolManager) GetBuffer(size uint) *([]uint8) { func (bpm *BufferPoolManager) GetBuffer(size uint) *[]uint8 {
bpm.mu.Lock() bpm.mu.RLock()
pool, exists := bpm.pools[size] pool, exists := bpm.pools[size]
bpm.mu.RUnlock()
if !exists {
bpm.mu.Lock()
// Double-check if another goroutine added the pool while we were waiting
pool, exists = bpm.pools[size]
if !exists { if !exists {
pool = &sync.Pool{ pool = &sync.Pool{
New: func() interface{} { New: func() interface{} {
@@ -28,21 +34,25 @@ func (bpm *BufferPoolManager) GetBuffer(size uint) *([]uint8) {
bpm.pools[size] = pool bpm.pools[size] = pool
} }
bpm.mu.Unlock() bpm.mu.Unlock()
}
return pool.Get().(*[]uint8) return pool.Get().(*[]uint8)
} }
func (bpm *BufferPoolManager) PutBuffer(buf *([]uint8)) { func (bpm *BufferPoolManager) PutBuffer(buf *[]uint8) {
size := uint(len(*buf)) size := uint(len(*buf))
bpm.mu.Lock() bpm.mu.RLock()
pool, exists := bpm.pools[size] pool, exists := bpm.pools[size]
bpm.mu.RUnlock()
if !exists { if !exists {
bpm.mu.Unlock()
return return
} }
bpm.mu.Unlock()
// Clear buffer
for i := range *buf { for i := range *buf {
(*buf)[i] = 0 (*buf)[i] = 0
} }
pool.Put(buf) pool.Put(buf)
} }

View File

@@ -6,10 +6,7 @@ import (
) )
func TestRetrieveBufferOfRequestedSize(t *testing.T) { func TestRetrieveBufferOfRequestedSize(t *testing.T) {
bpm := &BufferPoolManager{ bpm := NewBufferPoolManager()
pools: make(map[uint]*sync.Pool),
mu: sync.Mutex{},
}
size := 1024 size := 1024
buffer := bpm.GetBuffer(uint(size)) buffer := bpm.GetBuffer(uint(size))
@@ -24,10 +21,7 @@ func TestRetrieveBufferOfRequestedSize(t *testing.T) {
} }
func TestRequestBufferSizeZero(t *testing.T) { func TestRequestBufferSizeZero(t *testing.T) {
bpm := &BufferPoolManager{ bpm := NewBufferPoolManager()
pools: make(map[uint]*sync.Pool),
mu: sync.Mutex{},
}
size := 0 size := 0
buffer := bpm.GetBuffer(uint(size)) buffer := bpm.GetBuffer(uint(size))
@@ -42,10 +36,7 @@ func TestRequestBufferSizeZero(t *testing.T) {
} }
func TestConcurrentAccessToBufferPool(t *testing.T) { func TestConcurrentAccessToBufferPool(t *testing.T) {
bpm := &BufferPoolManager{ bpm := NewBufferPoolManager()
pools: make(map[uint]*sync.Pool),
mu: sync.Mutex{},
}
size := 1024 size := 1024
var wg sync.WaitGroup var wg sync.WaitGroup
@@ -69,10 +60,7 @@ func TestConcurrentAccessToBufferPool(t *testing.T) {
} }
func TestGetBufferLockUnlock(t *testing.T) { func TestGetBufferLockUnlock(t *testing.T) {
bpm := &BufferPoolManager{ bpm := NewBufferPoolManager()
pools: make(map[uint]*sync.Pool),
mu: sync.Mutex{},
}
size := 1024 size := 1024
buffer := bpm.GetBuffer(uint(size)) buffer := bpm.GetBuffer(uint(size))
@@ -87,10 +75,7 @@ func TestGetBufferLockUnlock(t *testing.T) {
} }
func TestVerifyPoolCreationForNewSizes(t *testing.T) { func TestVerifyPoolCreationForNewSizes(t *testing.T) {
bpm := &BufferPoolManager{ bpm := NewBufferPoolManager()
pools: make(map[uint]*sync.Pool),
mu: sync.Mutex{},
}
size := 512 size := 512
buffer := bpm.GetBuffer(uint(size)) buffer := bpm.GetBuffer(uint(size))
@@ -105,10 +90,7 @@ func TestVerifyPoolCreationForNewSizes(t *testing.T) {
} }
func TestBufferPoolManagerGetBuffer(t *testing.T) { func TestBufferPoolManagerGetBuffer(t *testing.T) {
bpm := &BufferPoolManager{ bpm := NewBufferPoolManager()
pools: make(map[uint]*sync.Pool),
mu: sync.Mutex{},
}
size := 1024 size := 1024
buffer := bpm.GetBuffer(uint(size)) buffer := bpm.GetBuffer(uint(size))
@@ -123,10 +105,7 @@ func TestBufferPoolManagerGetBuffer(t *testing.T) {
} }
func TestGetBufferWithMultipleSizes(t *testing.T) { func TestGetBufferWithMultipleSizes(t *testing.T) {
bpm := &BufferPoolManager{ bpm := NewBufferPoolManager()
pools: make(map[uint]*sync.Pool),
mu: sync.Mutex{},
}
sizes := []int{512, 1024, 2048} sizes := []int{512, 1024, 2048}
for _, size := range sizes { for _, size := range sizes {
@@ -141,3 +120,28 @@ func TestGetBufferWithMultipleSizes(t *testing.T) {
} }
} }
} }
func TestGetBufferIsAlwaysZero(t *testing.T) {
bpm := NewBufferPoolManager()
var size uint = 1024*64
for i := 0; i < 1000; i++ {
buffer := bpm.GetBuffer(size)
if buffer == nil {
t.Fatalf("Expected buffer for size %d, got nil", size)
}
if uint(len(*buffer)) != size {
t.Errorf("Expected buffer size %d, got %d", size, len(*buffer))
}
for _, b := range *buffer {
if b != 0 {
t.Errorf("Expected buffer to be zero, got %d", b)
}
}
bpm.PutBuffer(buffer)
}
}

View File

@@ -1,8 +1,3 @@
package pdu package pdu
import "sync" var ByteBufferPool = NewBufferPoolManager()
var ByteBufferPool = &BufferPoolManager{
pools: make(map[uint]*sync.Pool),
mu: sync.Mutex{},
}