Reuse buffer in worker functions

This commit is contained in:
Alexander Neumann 2019-03-24 22:12:38 +01:00
parent d51e9d1b98
commit 66efa425bf
3 changed files with 20 additions and 14 deletions

View file

@ -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 // a worker receives an index ID from ch, loads the index, and sends it to indexCh
worker := func() error { worker := func() error {
var buf []byte
for fi := range ch { for fi := range ch {
debug.Log("worker got file %v", fi.ID.Str()) 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 { if errors.Cause(err) == repository.ErrOldIndexFormat {
debug.Log("index %v has old format", fi.ID.Str()) debug.Log("index %v has old format", fi.ID.Str())
hints = append(hints, ErrOldIndexFormat{fi.ID}) 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()) err = errors.Wrapf(err, "error loading index %v", fi.ID.Str())

View file

@ -549,21 +549,21 @@ func DecodeOldIndex(buf []byte) (idx *Index, err error) {
} }
// LoadIndexWithDecoder loads the index and decodes it with fn. // 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) 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 { if err != nil {
return nil, err return nil, buf[:0], err
} }
idx, err = fn(buf) idx, err := fn(buf)
if err != nil { if err != nil {
debug.Log("error while decoding index %v: %v", id, err) debug.Log("error while decoding index %v: %v", id, err)
return nil, err return nil, buf[:0], err
} }
idx.id = id idx.id = id
return idx, nil return idx, buf, nil
} }

View file

@ -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 // a worker receives an index ID from ch, loads the index, and sends it to indexCh
worker := func() error { worker := func() error {
var buf []byte
for fi := range ch { for fi := range ch {
idx, err := LoadIndex(ctx, r, fi.ID) var err error
if err != nil { var idx *Index
fmt.Fprintf(os.Stderr, "%v, ignoring\n", err) idx, buf, err = LoadIndexWithDecoder(ctx, r, buf[:0], fi.ID, DecodeIndex)
return nil if err != nil && errors.Cause(err) == ErrOldIndexFormat {
idx, buf, err = LoadIndexWithDecoder(ctx, r, buf[:0], fi.ID, DecodeOldIndex)
} }
select { select {
@ -548,14 +550,15 @@ func (r *Repository) PrepareCache(indexIDs restic.IDSet) error {
// LoadIndex loads the index id from backend and returns it. // LoadIndex loads the index id from backend and returns it.
func LoadIndex(ctx context.Context, repo restic.Repository, id restic.ID) (*Index, error) { 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 { if err == nil {
return idx, nil return idx, nil
} }
if errors.Cause(err) == ErrOldIndexFormat { if errors.Cause(err) == ErrOldIndexFormat {
fmt.Fprintf(os.Stderr, "index %v has old format\n", id.Str()) 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 return nil, err