Rework every uint8 to byte and rework every []byte to bytes.Buffer
Some checks failed
Benchmark BufferPool / RunBenchmarks (push) Failing after 17s
Run Tests / Test (push) Failing after 15s

I just learnt that uint8 and byte are the fucking same
And that bytes.Buffer is the thing to use
Idiot idiot idiot...
Live and learn
This commit is contained in:
2024-07-24 18:43:18 +02:00
parent bed69fbfd3
commit f30680c26f
9 changed files with 461 additions and 418 deletions

View File

@@ -1,21 +1,22 @@
package pdu
import (
"bytes"
"sync"
)
type BufferPoolManager struct {
pools map[uint]*sync.Pool
pools map[int]*sync.Pool
mu sync.RWMutex
}
func NewBufferPoolManager() *BufferPoolManager {
return &BufferPoolManager{
pools: make(map[uint]*sync.Pool),
pools: make(map[int]*sync.Pool),
}
}
func (bpm *BufferPoolManager) Get(size uint) *[]uint8 {
func (bpm *BufferPoolManager) Get(size int) *bytes.Buffer {
bpm.mu.RLock()
pool, exists := bpm.pools[size]
bpm.mu.RUnlock()
@@ -27,8 +28,7 @@ func (bpm *BufferPoolManager) Get(size uint) *[]uint8 {
if !exists {
pool = &sync.Pool{
New: func() interface{} {
buf := make([]uint8, size)
return &buf
return bytes.NewBuffer(make([]byte, size))
},
}
bpm.pools[size] = pool
@@ -36,11 +36,11 @@ func (bpm *BufferPoolManager) Get(size uint) *[]uint8 {
bpm.mu.Unlock()
}
return pool.Get().(*[]uint8)
return pool.Get().(*bytes.Buffer);
}
func (bpm *BufferPoolManager) Put(buf *[]uint8) {
size := uint(len(*buf))
func (bpm *BufferPoolManager) Put(buf *bytes.Buffer) {
size := buf.Len()
bpm.mu.RLock()
pool, exists := bpm.pools[size]
bpm.mu.RUnlock()
@@ -49,10 +49,6 @@ func (bpm *BufferPoolManager) Put(buf *[]uint8) {
return
}
// Clear buffer
for i := range *buf {
(*buf)[i] = 0
}
buf.Reset()
pool.Put(buf)
}