mirror of
https://github.com/restic/restic.git
synced 2025-10-09 15:10:52 +00:00
Reuse buffer in worker functions
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
@@ -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
|
||||
|
Reference in New Issue
Block a user