From 59cfc4bd035a3b92582e8b8e340dfb125bfa62db Mon Sep 17 00:00:00 2001 From: Alexander Neumann Date: Fri, 13 Feb 2015 20:03:40 +0100 Subject: [PATCH] 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% --- chunker/chunker.go | 13 ++++++++++--- chunker/chunker_test.go | 3 ++- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/chunker/chunker.go b/chunker/chunker.go index b06dec69a..d69579724 100644 --- a/chunker/chunker.go +++ b/chunker/chunker.go @@ -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 diff --git a/chunker/chunker_test.go b/chunker/chunker_test.go index 710d49588..e84e96c0c 100644 --- a/chunker/chunker_test.go +++ b/chunker/chunker_test.go @@ -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()