From 66efa425bf8b2d88b7a3b7ef1d1805e1cea0c023 Mon Sep 17 00:00:00 2001 From: Alexander Neumann Date: Sun, 24 Mar 2019 22:12:38 +0100 Subject: [PATCH] Reuse buffer in worker functions --- internal/checker/checker.go | 7 +++++-- internal/repository/index.go | 12 ++++++------ internal/repository/repository.go | 15 +++++++++------ 3 files changed, 20 insertions(+), 14 deletions(-) diff --git a/internal/checker/checker.go b/internal/checker/checker.go index 79b3cb4b9..c5bee4bef 100644 --- a/internal/checker/checker.go +++ b/internal/checker/checker.go @@ -108,14 +108,17 @@ func (c *Checker) LoadIndex(ctx context.Context) (hints []error, errs []error) { // a worker receives an index ID from ch, loads the index, and sends it to indexCh worker := func() error { + var buf []byte for fi := range ch { debug.Log("worker got file %v", fi.ID.Str()) - idx, err := repository.LoadIndexWithDecoder(ctx, c.repo, fi.ID, repository.DecodeIndex) + var err error + var idx *repository.Index + idx, buf, err = repository.LoadIndexWithDecoder(ctx, c.repo, buf[:0], fi.ID, repository.DecodeIndex) if errors.Cause(err) == repository.ErrOldIndexFormat { debug.Log("index %v has old format", fi.ID.Str()) hints = append(hints, ErrOldIndexFormat{fi.ID}) - idx, err = repository.LoadIndexWithDecoder(ctx, c.repo, fi.ID, repository.DecodeOldIndex) + idx, buf, err = repository.LoadIndexWithDecoder(ctx, c.repo, buf[:0], fi.ID, repository.DecodeOldIndex) } err = errors.Wrapf(err, "error loading index %v", fi.ID.Str()) diff --git a/internal/repository/index.go b/internal/repository/index.go index ef6661dfc..32d00882a 100644 --- a/internal/repository/index.go +++ b/internal/repository/index.go @@ -549,21 +549,21 @@ func DecodeOldIndex(buf []byte) (idx *Index, err error) { } // LoadIndexWithDecoder loads the index and decodes it with fn. -func LoadIndexWithDecoder(ctx context.Context, repo restic.Repository, id restic.ID, fn func([]byte) (*Index, error)) (idx *Index, err error) { +func LoadIndexWithDecoder(ctx context.Context, repo restic.Repository, buf []byte, id restic.ID, fn func([]byte) (*Index, error)) (*Index, []byte, error) { debug.Log("Loading index %v", id) - buf, err := repo.LoadAndDecrypt(ctx, nil, restic.IndexFile, id) + buf, err := repo.LoadAndDecrypt(ctx, buf[:0], restic.IndexFile, id) if err != nil { - return nil, err + return nil, buf[:0], err } - idx, err = fn(buf) + idx, err := fn(buf) if err != nil { debug.Log("error while decoding index %v: %v", id, err) - return nil, err + return nil, buf[:0], err } idx.id = id - return idx, nil + return idx, buf, nil } diff --git a/internal/repository/repository.go b/internal/repository/repository.go index 6ab8ca595..cb78c25c1 100644 --- a/internal/repository/repository.go +++ b/internal/repository/repository.go @@ -431,11 +431,13 @@ func (r *Repository) LoadIndex(ctx context.Context) error { // a worker receives an index ID from ch, loads the index, and sends it to indexCh worker := func() error { + var buf []byte for fi := range ch { - idx, err := LoadIndex(ctx, r, fi.ID) - if err != nil { - fmt.Fprintf(os.Stderr, "%v, ignoring\n", err) - return nil + var err error + var idx *Index + idx, buf, err = LoadIndexWithDecoder(ctx, r, buf[:0], fi.ID, DecodeIndex) + if err != nil && errors.Cause(err) == ErrOldIndexFormat { + idx, buf, err = LoadIndexWithDecoder(ctx, r, buf[:0], fi.ID, DecodeOldIndex) } select { @@ -548,14 +550,15 @@ func (r *Repository) PrepareCache(indexIDs restic.IDSet) error { // LoadIndex loads the index id from backend and returns it. func LoadIndex(ctx context.Context, repo restic.Repository, id restic.ID) (*Index, error) { - idx, err := LoadIndexWithDecoder(ctx, repo, id, DecodeIndex) + idx, _, err := LoadIndexWithDecoder(ctx, repo, nil, id, DecodeIndex) if err == nil { return idx, nil } if errors.Cause(err) == ErrOldIndexFormat { fmt.Fprintf(os.Stderr, "index %v has old format\n", id.Str()) - return LoadIndexWithDecoder(ctx, repo, id, DecodeOldIndex) + idx, _, err := LoadIndexWithDecoder(ctx, repo, nil, id, DecodeOldIndex) + return idx, err } return nil, err