mirror of
https://github.com/restic/restic.git
synced 2026-11-23 13:52:39 +00:00
restic: add BlobBuffer pool types
Based on the buffer type used by the archiver. The main change is that `Get()` now always returns a slice with length zero.
This commit is contained in:
1 parent
b7e20f3aa2
commit
3446754c41
2 files changed
+118
No files matched your search
@@ -0,0 +1,54 @@
|
||||
package restic
|
||||
|
||||
import "sync"
|
||||
|
||||
// BlobBuffer is a reusable buffer. After the buffer has been used, Release should
|
||||
// be called so the underlying slice is put back into the pool.
|
||||
type BlobBuffer struct {
|
||||
Data []byte
|
||||
pool *BlobBufferPool
|
||||
}
|
||||
|
||||
// Release puts the buffer back into the pool it came from. For buffers created
|
||||
// with NewBuffer, Release is a no-op.
|
||||
func (b *BlobBuffer) Release() {
|
||||
pool := b.pool
|
||||
if pool == nil || cap(b.Data) > pool.defaultSize {
|
||||
return
|
||||
}
|
||||
|
||||
pool.pool.Put(b)
|
||||
}
|
||||
|
||||
// BlobBufferPool implements a limited set of reusable buffers.
|
||||
type BlobBufferPool struct {
|
||||
pool sync.Pool
|
||||
defaultSize int
|
||||
}
|
||||
|
||||
// NewBlobBufferPool initializes a new buffer pool. New buffers are created
|
||||
// with defaultSize. Buffers that have grown larger are not put back.
|
||||
func NewBlobBufferPool(defaultSize int) *BlobBufferPool {
|
||||
b := &BlobBufferPool{
|
||||
defaultSize: defaultSize,
|
||||
}
|
||||
b.pool = sync.Pool{New: func() any {
|
||||
return &BlobBuffer{
|
||||
Data: make([]byte, defaultSize),
|
||||
pool: b,
|
||||
}
|
||||
}}
|
||||
return b
|
||||
}
|
||||
|
||||
// Get returns a buffer from the pool. It is guaranteed that the buffer has zero length.
|
||||
func (pool *BlobBufferPool) Get() *BlobBuffer {
|
||||
buf := pool.pool.Get().(*BlobBuffer)
|
||||
buf.Data = buf.Data[:0]
|
||||
return buf
|
||||
}
|
||||
|
||||
// NewBuffer wraps an existing slice. Release is a no-op.
|
||||
func NewBuffer(data []byte) *BlobBuffer {
|
||||
return &BlobBuffer{Data: data}
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
package restic
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestBlobBufferPoolReuse(t *testing.T) {
|
||||
success := false
|
||||
// retries to avoid flakiness. The test can fail depending on the GC.
|
||||
for i := 0; i < 100; i++ {
|
||||
// Test that buffers are actually reused from the pool
|
||||
pool := NewBlobBufferPool(1024)
|
||||
|
||||
// Get a buffer and modify it
|
||||
buf1 := pool.Get()
|
||||
if cap(buf1.Data) < 1024 {
|
||||
t.Fatalf("buffer capacity %d, want at least 1024", cap(buf1.Data))
|
||||
}
|
||||
buf1.Data = append(buf1.Data, 0xFF)
|
||||
originalAddr := &buf1.Data[0]
|
||||
buf1.Release()
|
||||
|
||||
// Get another buffer and check if it's the same underlying slice
|
||||
buf2 := pool.Get()
|
||||
// extend buffer to allow getting the address of the first byte
|
||||
buf2.Data = append(buf2.Data, 0)
|
||||
if &buf2.Data[0] == originalAddr {
|
||||
success = true
|
||||
break
|
||||
}
|
||||
buf2.Release()
|
||||
}
|
||||
if !success {
|
||||
t.Error("buffer was not reused from pool")
|
||||
}
|
||||
}
|
||||
|
||||
func TestBlobBufferPoolLargeBuffers(t *testing.T) {
|
||||
success := false
|
||||
// retries to avoid flakiness. The test can fail depending on the GC.
|
||||
for i := 0; i < 100; i++ {
|
||||
// Test that buffers larger than defaultSize are not returned to pool
|
||||
pool := NewBlobBufferPool(1024)
|
||||
buf := pool.Get()
|
||||
|
||||
// Grow the buffer beyond default size
|
||||
buf.Data = append(buf.Data, make([]byte, 2048)...)
|
||||
originalCap := cap(buf.Data)
|
||||
|
||||
buf.Release()
|
||||
|
||||
// Get a new buffer - should not be the same slice
|
||||
newBuf := pool.Get()
|
||||
if cap(newBuf.Data) != originalCap {
|
||||
success = true
|
||||
break
|
||||
}
|
||||
newBuf.Release()
|
||||
}
|
||||
|
||||
if !success {
|
||||
t.Error("large buffer was incorrectly returned to pool")
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user