From 79f17ac895285cb75941f33827a9767669829de6 Mon Sep 17 00:00:00 2001 From: Michael Eischer Date: Fri, 10 Jul 2026 17:48:10 +0200 Subject: [PATCH] archiver: switch fileSaver to repository BlobBufferPool --- internal/archiver/buffer.go | 47 ---------------------- internal/archiver/buffer_test.go | 58 ---------------------------- internal/archiver/file_saver.go | 16 ++++---- internal/archiver/file_saver_test.go | 3 +- internal/archiver/tree_saver_test.go | 7 ++-- 5 files changed, 13 insertions(+), 118 deletions(-) delete mode 100644 internal/archiver/buffer.go delete mode 100644 internal/archiver/buffer_test.go diff --git a/internal/archiver/buffer.go b/internal/archiver/buffer.go deleted file mode 100644 index 0a6ae6d8f..000000000 --- a/internal/archiver/buffer.go +++ /dev/null @@ -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) -} diff --git a/internal/archiver/buffer_test.go b/internal/archiver/buffer_test.go deleted file mode 100644 index 65263c5a2..000000000 --- a/internal/archiver/buffer_test.go +++ /dev/null @@ -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") - } -} diff --git a/internal/archiver/file_saver.go b/internal/archiver/file_saver.go index 358db8cbc..ab6a9b32f 100644 --- a/internal/archiver/file_saver.go +++ b/internal/archiver/file_saver.go @@ -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() diff --git a/internal/archiver/file_saver_test.go b/internal/archiver/file_saver_test.go index e71bf6cec..2f361c6e1 100644 --- a/internal/archiver/file_saver_test.go +++ b/internal/archiver/file_saver_test.go @@ -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) diff --git a/internal/archiver/tree_saver_test.go b/internal/archiver/tree_saver_test.go index 92231f4bc..fb6bde9a6 100644 --- a/internal/archiver/tree_saver_test.go +++ b/internal/archiver/tree_saver_test.go @@ -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)) {