From 7a992fc794f2cac9c063b8af31caf4d5fcc5532e Mon Sep 17 00:00:00 2001 From: Michael Eischer Date: Fri, 19 Aug 2022 21:11:02 +0200 Subject: [PATCH] repository: Reduce buffer reallocations in ForAllIndexes Previously the buffer was grown incrementally inside `repo.LoadUnpacked`. But we can do better as we already know how large the index will be. Allocate a bit more memory to increase the chance that the buffer can be reused in the future. --- internal/repository/index_parallel.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/internal/repository/index_parallel.go b/internal/repository/index_parallel.go index dcf33113e..4fe0797af 100644 --- a/internal/repository/index_parallel.go +++ b/internal/repository/index_parallel.go @@ -52,6 +52,10 @@ func ForAllIndexes(ctx context.Context, repo restic.Repository, var idx *Index oldFormat := false + if cap(buf) < int(fi.Size) { + // overallocate a bit + buf = make([]byte, fi.Size+128*1024) + } buf, err = repo.LoadUnpacked(ctx, restic.IndexFile, fi.ID, buf[:0]) if err == nil { idx, oldFormat, err = DecodeIndex(buf, fi.ID)