diff --git a/internal/restic/buffer.go b/internal/restic/buffer.go new file mode 100644 index 000000000..6178c9f1c --- /dev/null +++ b/internal/restic/buffer.go @@ -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} +} diff --git a/internal/restic/buffer_test.go b/internal/restic/buffer_test.go new file mode 100644 index 000000000..12682c5ec --- /dev/null +++ b/internal/restic/buffer_test.go @@ -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") + } +}