archiver: switch fileSaver to repository BlobBufferPool

This commit is contained in:
Michael Eischer committed 2026-11-22 17:54:09 +01:00
1 parent 30de2d9211
commit 79f17ac895
5 files changed
+13 -118

No files matched your search

-47
View File
@@ -1,47 +0,0 @@
package archiver
import "sync"
// buffer 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 buffer struct {
Data []byte
pool *bufferPool
}
// Release puts the buffer back into the pool it came from.
func (b *buffer) Release() {
pool := b.pool
if pool == nil || cap(b.Data) > pool.defaultSize {
return
}
pool.pool.Put(b)
}
// bufferPool implements a limited set of reusable buffers.
type bufferPool struct {
pool sync.Pool
defaultSize int
}
// newBufferPool initializes a new buffer pool. The pool stores at most max
// items. New buffers are created with defaultSize. Buffers that have grown
// larger are not put back.
func newBufferPool(defaultSize int) *bufferPool {
b := &bufferPool{
defaultSize: defaultSize,
}
b.pool = sync.Pool{New: func() any {
return &buffer{
Data: make([]byte, defaultSize),
pool: b,
}
}}
return b
}
// Get returns a new buffer, either from the pool or newly allocated.
func (pool *bufferPool) Get() *buffer {
return pool.pool.Get().(*buffer)
}
-58
View File
@@ -1,58 +0,0 @@
package archiver
import (
"testing"
)
func TestBufferPoolReuse(t *testing.T) {
success := false
// retries to avoid flakiness. The test can fail depending on the GC.
for range 100 {
// Test that buffers are actually reused from the pool
pool := newBufferPool(1024)
// Get a buffer and modify it
buf1 := pool.Get()
buf1.Data[0] = 0xFF
originalAddr := &buf1.Data[0]
buf1.Release()
// Get another buffer and check if it's the same underlying slice
buf2 := pool.Get()
if &buf2.Data[0] == originalAddr {
success = true
break
}
buf2.Release()
}
if !success {
t.Error("buffer was not reused from pool")
}
}
func TestBufferPoolLargeBuffers(t *testing.T) {
success := false
// retries to avoid flakiness. The test can fail depending on the GC.
for range 100 {
// Test that buffers larger than defaultSize are not returned to pool
pool := newBufferPool(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
}
}
if !success {
t.Error("large buffer was incorrectly returned to pool")
}
}
+7 -9
View File
@@ -18,8 +18,7 @@ const chunkReadBufSize = 512 * 1024 // matches chunker internal read buffer size
// fileSaver concurrently saves incoming files to the repo.
type fileSaver struct {
saveFilePool *bufferPool
uploader restic.BlobSaverAsync
uploader restic.BlobSaverAsync
chunkerFactory restic.ChunkerFactory
@@ -38,7 +37,6 @@ func newFileSaver(ctx context.Context, wg *errgroup.Group, uploader restic.BlobS
s := &fileSaver{
uploader: uploader,
saveFilePool: newBufferPool(chunkerFactory.MaxChunkSize()),
chunkerFactory: chunkerFactory,
ch: ch,
@@ -224,7 +222,7 @@ func (s *fileSaver) saveFile(ctx context.Context, chnker restic.Chunker, chunkSt
node.Size = 0
var idx int
for {
buf := s.saveFilePool.Get()
buf := s.uploader.BlobBufferPool().Get()
chunkData, err := chunkState.readNextChunk(f, chnker, buf.Data)
if err == io.EOF {
buf.Release()
@@ -239,7 +237,8 @@ func (s *fileSaver) saveFile(ctx context.Context, chnker restic.Chunker, chunkSt
// put result buffer back for later reuse
buf.Data = chunkData
node.Size += uint64(len(chunkData))
chunkSize := len(buf.Data)
node.Size += uint64(chunkSize)
// test if the context has been cancelled, return the error
if ctx.Err() != nil {
@@ -256,8 +255,7 @@ func (s *fileSaver) saveFile(ctx context.Context, chnker restic.Chunker, chunkSt
node.Content = append(node.Content, restic.ID{})
lock.Unlock()
s.uploader.SaveBlobAsync(ctx, restic.DataBlob, restic.NewBuffer(chunkData), restic.ID{}, false, func(newID restic.ID, known bool, sizeInRepo int, err error) {
defer buf.Release()
s.uploader.SaveBlobAsync(ctx, restic.DataBlob, buf, restic.ID{}, false, func(newID restic.ID, known bool, sizeInRepo int, err error) {
if err != nil {
completeError(err)
return
@@ -266,7 +264,7 @@ func (s *fileSaver) saveFile(ctx context.Context, chnker restic.Chunker, chunkSt
lock.Lock()
if !known {
fnr.stats.DataBlobs++
fnr.stats.DataSize += uint64(len(chunkData))
fnr.stats.DataSize += uint64(chunkSize)
fnr.stats.DataSizeInRepo += uint64(sizeInRepo)
}
node.Content[pos] = newID
@@ -283,7 +281,7 @@ func (s *fileSaver) saveFile(ctx context.Context, chnker restic.Chunker, chunkSt
return
}
s.CompleteBlob(uint64(len(chunkData)))
s.CompleteBlob(uint64(chunkSize))
}
err = f.Close()
+2 -1
View File
@@ -11,6 +11,7 @@ import (
"github.com/restic/restic/internal/data"
"github.com/restic/restic/internal/fs"
"github.com/restic/restic/internal/repository"
"github.com/restic/restic/internal/restic"
"github.com/restic/restic/internal/test"
"golang.org/x/sync/errgroup"
)
@@ -36,7 +37,7 @@ func startFileSaver(ctx context.Context, t testing.TB, _ fs.FS) (*fileSaver, *mo
workers := uint(runtime.NumCPU())
chunkerFactory := repository.TestRepository(t).ChunkerFactory()
saver := &mockSaver{saved: make(map[string]int)}
saver := &mockSaver{saved: make(map[string]int), buffers: restic.NewBlobBufferPool(chunkerFactory.MaxChunkSize())}
s := newFileSaver(ctx, wg, saver, chunkerFactory, workers)
s.NodeFromFileInfo = func(snPath, filename string, meta toNoder, ignoreXattrListError bool) (*data.Node, error) {
return meta.ToNode(ignoreXattrListError, t.Logf)
+4 -3
View File
@@ -15,12 +15,13 @@ import (
)
type mockSaver struct {
saved map[string]int
mutex sync.Mutex
saved map[string]int
mutex sync.Mutex
buffers *restic.BlobBufferPool
}
func (m *mockSaver) BlobBufferPool() *restic.BlobBufferPool {
return nil
return m.buffers
}
func (m *mockSaver) SaveBlobAsync(_ context.Context, _ restic.BlobType, buf *restic.BlobBuffer, id restic.ID, storeDuplicate bool, cb func(newID restic.ID, known bool, sizeInRepo int, err error)) {