mirror of
https://github.com/restic/restic.git
synced 2026-10-08 11:27:12 +00:00
Remove chunker.Reset() and chunker pool
This commit is contained in:
1 parent
c57c4f0b8f
commit
282c4379db
5 files changed
+32
-93
No files matched your search
+17
-26
@@ -17,6 +17,9 @@ const (
|
||||
// aim to create chunks of 20 bits or about 1MiB on average.
|
||||
averageBits = 20
|
||||
|
||||
// default buffer size
|
||||
bufSize = 512 * KiB
|
||||
|
||||
// MinSize is the minimal size of a chunk.
|
||||
MinSize = 512 * KiB
|
||||
// MaxSize is the maximal size of a chunk.
|
||||
@@ -81,36 +84,33 @@ type Chunker struct {
|
||||
|
||||
// New returns a new Chunker based on polynomial p that reads from data from rd
|
||||
// with bufsize and pass all data to hash along the way.
|
||||
func New(rd io.Reader, p Pol, bufsize int, hash hash.Hash) *Chunker {
|
||||
func New(rd io.Reader, pol Pol, h hash.Hash) *Chunker {
|
||||
c := &Chunker{
|
||||
buf: make([]byte, bufsize),
|
||||
h: hash,
|
||||
buf: make([]byte, bufSize),
|
||||
h: h,
|
||||
pol: pol,
|
||||
rd: rd,
|
||||
}
|
||||
c.Reset(rd, p)
|
||||
|
||||
c.reset()
|
||||
|
||||
return c
|
||||
}
|
||||
|
||||
// Reset restarts a chunker so that it can be reused with a different
|
||||
// polynomial and reader.
|
||||
func (c *Chunker) Reset(rd io.Reader, p Pol) {
|
||||
c.pol = p
|
||||
c.polShift = uint(p.Deg() - 8)
|
||||
func (c *Chunker) reset() {
|
||||
c.polShift = uint(c.pol.Deg() - 8)
|
||||
c.fillTables()
|
||||
c.rd = rd
|
||||
|
||||
for i := 0; i < windowSize; i++ {
|
||||
c.window[i] = 0
|
||||
}
|
||||
|
||||
c.closed = false
|
||||
c.digest = 0
|
||||
c.wpos = 0
|
||||
c.pos = 0
|
||||
c.start = 0
|
||||
c.count = 0
|
||||
|
||||
if p != 0 {
|
||||
c.slide(1)
|
||||
}
|
||||
c.slide(1)
|
||||
c.start = c.pos
|
||||
|
||||
if c.h != nil {
|
||||
c.h.Reset()
|
||||
@@ -276,16 +276,7 @@ func (c *Chunker) Next() (*Chunk, error) {
|
||||
Digest: c.hashDigest(),
|
||||
}
|
||||
|
||||
if c.h != nil {
|
||||
c.h.Reset()
|
||||
}
|
||||
|
||||
// reset chunker, but keep position
|
||||
pos := c.pos
|
||||
c.Reset(c.rd, c.pol)
|
||||
c.pos = pos
|
||||
c.start = pos
|
||||
c.pre = MinSize - windowSize
|
||||
c.reset()
|
||||
|
||||
return chunk, nil
|
||||
}
|
||||
|
||||
+7
-23
@@ -19,7 +19,6 @@ import (
|
||||
)
|
||||
|
||||
var benchmarkFile = flag.String("bench.file", "", "read from this file for benchmark")
|
||||
var testBufSize = flag.Int("test.bufsize", 256*1024, "use this buffer size for benchmark")
|
||||
|
||||
func parseDigest(s string) []byte {
|
||||
d, err := hex.DecodeString(s)
|
||||
@@ -151,7 +150,7 @@ func getRandom(seed, count int) []byte {
|
||||
func TestChunker(t *testing.T) {
|
||||
// setup data source
|
||||
buf := getRandom(23, 32*1024*1024)
|
||||
ch := chunker.New(bytes.NewReader(buf), testPol, *testBufSize, sha256.New())
|
||||
ch := chunker.New(bytes.NewReader(buf), testPol, sha256.New())
|
||||
chunks := testWithData(t, ch, chunks1)
|
||||
|
||||
// test reader
|
||||
@@ -178,7 +177,7 @@ func TestChunker(t *testing.T) {
|
||||
|
||||
// setup nullbyte data source
|
||||
buf = bytes.Repeat([]byte{0}, len(chunks2)*chunker.MinSize)
|
||||
ch = chunker.New(bytes.NewReader(buf), testPol, *testBufSize, sha256.New())
|
||||
ch = chunker.New(bytes.NewReader(buf), testPol, sha256.New())
|
||||
|
||||
testWithData(t, ch, chunks2)
|
||||
}
|
||||
@@ -194,7 +193,7 @@ func TestChunkerWithRandomPolynomial(t *testing.T) {
|
||||
t.Logf("generating random polynomial took %v", time.Since(start))
|
||||
|
||||
start = time.Now()
|
||||
ch := chunker.New(bytes.NewReader(buf), p, *testBufSize, sha256.New())
|
||||
ch := chunker.New(bytes.NewReader(buf), p, sha256.New())
|
||||
t.Logf("creating chunker took %v", time.Since(start))
|
||||
|
||||
// make sure that first chunk is different
|
||||
@@ -211,7 +210,7 @@ func TestChunkerWithRandomPolynomial(t *testing.T) {
|
||||
func TestChunkerWithoutHash(t *testing.T) {
|
||||
// setup data source
|
||||
buf := getRandom(23, 32*1024*1024)
|
||||
ch := chunker.New(bytes.NewReader(buf), testPol, *testBufSize, nil)
|
||||
ch := chunker.New(bytes.NewReader(buf), testPol, nil)
|
||||
chunks := testWithData(t, ch, chunks1)
|
||||
|
||||
// test reader
|
||||
@@ -241,30 +240,17 @@ func TestChunkerWithoutHash(t *testing.T) {
|
||||
|
||||
// setup nullbyte data source
|
||||
buf = bytes.Repeat([]byte{0}, len(chunks2)*chunker.MinSize)
|
||||
ch = chunker.New(bytes.NewReader(buf), testPol, *testBufSize, sha256.New())
|
||||
ch = chunker.New(bytes.NewReader(buf), testPol, sha256.New())
|
||||
|
||||
testWithData(t, ch, chunks2)
|
||||
}
|
||||
|
||||
func TestChunkerReuse(t *testing.T) {
|
||||
// test multiple uses of the same chunker
|
||||
ch := chunker.New(nil, testPol, *testBufSize, sha256.New())
|
||||
buf := getRandom(23, 32*1024*1024)
|
||||
|
||||
for i := 0; i < 4; i++ {
|
||||
ch.Reset(bytes.NewReader(buf), testPol)
|
||||
testWithData(t, ch, chunks1)
|
||||
}
|
||||
}
|
||||
|
||||
func benchmarkChunker(b *testing.B, hash hash.Hash) {
|
||||
var (
|
||||
rd io.ReadSeeker
|
||||
size int
|
||||
)
|
||||
|
||||
b.Logf("using bufsize %v", *testBufSize)
|
||||
|
||||
if *benchmarkFile != "" {
|
||||
b.Logf("using file %q for benchmark", *benchmarkFile)
|
||||
f, err := os.Open(*benchmarkFile)
|
||||
@@ -284,8 +270,6 @@ func benchmarkChunker(b *testing.B, hash hash.Hash) {
|
||||
rd = bytes.NewReader(getRandom(23, size))
|
||||
}
|
||||
|
||||
ch := chunker.New(rd, testPol, *testBufSize, hash)
|
||||
|
||||
b.ResetTimer()
|
||||
b.SetBytes(int64(size))
|
||||
|
||||
@@ -294,7 +278,7 @@ func benchmarkChunker(b *testing.B, hash hash.Hash) {
|
||||
chunks = 0
|
||||
|
||||
rd.Seek(0, 0)
|
||||
ch.Reset(rd, testPol)
|
||||
ch := chunker.New(rd, testPol, hash)
|
||||
|
||||
for {
|
||||
_, err := ch.Next()
|
||||
@@ -333,6 +317,6 @@ func BenchmarkNewChunker(b *testing.B) {
|
||||
b.ResetTimer()
|
||||
|
||||
for i := 0; i < b.N; i++ {
|
||||
chunker.New(bytes.NewBuffer(nil), p, *testBufSize, nil)
|
||||
chunker.New(bytes.NewBuffer(nil), p, nil)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user