Reuse 32KB buffers in copyBufferLog with sync.Pool (#1572)

* Bolt: Reuse 32KB buffers in copyBufferLog with sync.Pool

Delete .jules directory

* Add benchmark test for copyBufferLog function
This commit is contained in:
prudhvi 2026-05-16 10:49:36 +05:30 committed by GitHub
parent 6dc0f3f792
commit 0e2b37ad6c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 35 additions and 1 deletions

View file

@ -3,13 +3,24 @@ package server
import (
"errors"
"io"
"sync"
"time"
)
var errDisconnect = errors.New("traffic logger requested disconnect")
var copyBufPool = sync.Pool{
New: func() any {
b := make([]byte, 32*1024)
return &b
},
}
func copyBufferLog(dst io.Writer, src io.Reader, log func(n uint64) bool) error {
buf := make([]byte, 32*1024)
bufp := copyBufPool.Get().(*[]byte)
buf := *bufp
defer copyBufPool.Put(bufp)
for {
nr, er := src.Read(buf)
if nr > 0 {

View file

@ -0,0 +1,23 @@
package server
import (
"bytes"
"io"
"testing"
)
func BenchmarkCopyBufferLog(b *testing.B) {
srcData := make([]byte, 1024*1024) // 1MB
for i := range srcData {
srcData[i] = byte(i)
}
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
src := bytes.NewReader(srcData)
dst := io.Discard
copyBufferLog(dst, src, func(n uint64) bool { return true })
}
}