Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1574 from ifedorenko/1567_optimize-pack-readHeader
Optimize pack readHeader() implementation
- Loading branch information
Showing
3 changed files
with
89 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
Enhancement: Reduce number of remote requests reading pack header | ||
|
||
This change eliminates extra remote repository calls for most pack | ||
files and improves repository reindex and purge time. | ||
|
||
https://github.com/restic/restic/issues/1567 | ||
https://github.com/restic/restic/pull/1574 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package pack | ||
|
||
import ( | ||
"bytes" | ||
"encoding/binary" | ||
"io" | ||
"testing" | ||
|
||
"github.com/restic/restic/internal/crypto" | ||
rtest "github.com/restic/restic/internal/test" | ||
) | ||
|
||
type countingReaderAt struct { | ||
delegate io.ReaderAt | ||
invocationCount int | ||
} | ||
|
||
func (rd *countingReaderAt) ReadAt(p []byte, off int64) (n int, err error) { | ||
rd.invocationCount++ | ||
return rd.delegate.ReadAt(p, off) | ||
} | ||
|
||
func TestReadHeaderEagerLoad(t *testing.T) { | ||
|
||
testReadHeader := func(entryCount uint, expectedReadInvocationCount int) { | ||
expectedHeader := rtest.Random(0, int(entryCount*entrySize)+crypto.Extension) | ||
|
||
buf := &bytes.Buffer{} | ||
buf.Write(rtest.Random(0, 100)) // pack blobs data | ||
buf.Write(expectedHeader) // pack header | ||
binary.Write(buf, binary.LittleEndian, uint32(len(expectedHeader))) // pack header length | ||
|
||
rd := &countingReaderAt{delegate: bytes.NewReader(buf.Bytes())} | ||
|
||
header, err := readHeader(rd, int64(buf.Len())) | ||
rtest.OK(t, err) | ||
|
||
rtest.Equals(t, expectedHeader, header) | ||
rtest.Equals(t, expectedReadInvocationCount, rd.invocationCount) | ||
} | ||
|
||
testReadHeader(1, 1) | ||
testReadHeader(eagerEntries-1, 1) | ||
testReadHeader(eagerEntries, 1) | ||
testReadHeader(eagerEntries+1, 2) | ||
} |