]> Cypherpunks repositories - gostls13.git/commitdiff
adler32: speed up ~40% by avoiding bounds checks
authorBrad Fitzpatrick <bradfitz@golang.org>
Thu, 28 Apr 2011 04:36:11 +0000 (21:36 -0700)
committerBrad Fitzpatrick <bradfitz@golang.org>
Thu, 28 Apr 2011 04:36:11 +0000 (21:36 -0700)
before & after:
adler32.BenchmarkGolden   100000      14747 ns/op
adler32.BenchmarkGolden   200000       8761 ns/op

Found by profiling PNG encoding.

R=rsc, bradfitzwork, eds
CC=golang-dev
https://golang.org/cl/4441073

src/pkg/hash/adler32/adler32.go
src/pkg/hash/adler32/adler32_test.go

index f13a9379338a3112b11825139746da2e4d0994be..84943d9ae4c2ebf0941a2c204bb2ad2e59168667 100644 (file)
@@ -43,8 +43,8 @@ func (d *digest) Size() int { return Size }
 
 // Add p to the running checksum a, b.
 func update(a, b uint32, p []byte) (aa, bb uint32) {
-       for i := 0; i < len(p); i++ {
-               a += uint32(p[i])
+       for _, pi := range p {
+               a += uint32(pi)
                b += a
                // invariant: a <= b
                if b > (0xffffffff-255)/2 {
index ffa5569bcdd47df19a5f3959b4360951b1d9dca6..01f931c6859850b309a2b2b5db90326e29ddd55f 100644 (file)
@@ -5,6 +5,7 @@
 package adler32
 
 import (
+       "bytes"
        "io"
        "testing"
 )
@@ -61,3 +62,16 @@ func TestGolden(t *testing.T) {
                }
        }
 }
+
+func BenchmarkGolden(b *testing.B) {
+       b.StopTimer()
+       c := New()
+       var buf bytes.Buffer
+       for _, g := range golden {
+               buf.Write([]byte(g.in))
+       }
+       b.StartTimer()
+       for i := 0; i < b.N; i++ {
+               c.Write(buf.Bytes())
+       }
+}