Slightly improve performance for chunker

benchcmp output:

benchmark                      old ns/op     new ns/op     delta
BenchmarkChunkerWithSHA256     151752169     148072829     -2.42%
BenchmarkChunkerWithMD5        97166433      93357090      -3.92%
BenchmarkChunker               77265146      73642723      -4.69%

benchmark                      old MB/s     new MB/s     speedup
BenchmarkChunkerWithSHA256     69.10        70.81        1.02x
BenchmarkChunkerWithMD5        107.92       112.32       1.04x
BenchmarkChunker               135.71       142.39       1.05x

benchmark                      old allocs     new allocs     delta
BenchmarkChunkerWithSHA256     15             13             -13.33%
BenchmarkChunkerWithMD5        14             12             -14.29%
BenchmarkChunker               8              6              -25.00%

benchmark                      old bytes     new bytes     delta
BenchmarkChunkerWithSHA256     262924        26821         -89.80%
BenchmarkChunkerWithMD5        262774        13554         -94.84%
BenchmarkChunker               262678        13458         -94.88%
This commit is contained in:
Alexander Neumann 2015-02-13 20:03:40 +01:00
parent 0f36b19a4c
commit 59cfc4bd03
2 changed files with 12 additions and 4 deletions

View File

@ -206,7 +206,8 @@ func (c *Chunker) Next() (*Chunk, error) {
c.pre = 0
}
for i, b := range c.buf[c.bpos:c.bmax] {
add := c.count
for _, b := range c.buf[c.bpos:c.bmax] {
// inline c.slide(b) and append(b) to increase performance
out := c.window[c.wpos]
c.window[c.wpos] = b
@ -221,9 +222,15 @@ func (c *Chunker) Next() (*Chunk, error) {
c.digest ^= mod_table[index]
// end inline
if (c.count+uint(i)+1 >= MinSize && (c.digest&splitmask) == 0) || c.count+uint(i)+1 >= MaxSize {
add++
if add < MinSize {
continue
}
if (c.digest&splitmask) == 0 || add >= MaxSize {
i := add - c.count - 1
c.updateHash(c.buf[c.bpos : c.bpos+uint(i)+1])
c.count += uint(i) + 1
c.count = add
c.pos += uint(i) + 1
c.bpos += uint(i) + 1

View File

@ -256,13 +256,14 @@ func benchmarkChunker(b *testing.B, hash hash.Hash) {
b.ResetTimer()
b.SetBytes(int64(size))
ch := chunker.New(rd, *testBufSize, hash)
var chunks int
for i := 0; i < b.N; i++ {
chunks = 0
rd.Seek(0, 0)
ch := chunker.New(rd, *testBufSize, hash)
ch.Reset(rd)
for {
_, err := ch.Next()