mirror of
https://github.com/golang/go
synced 2024-11-20 01:14:40 -07:00
adler32: speed up ~40% by avoiding bounds checks
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
This commit is contained in:
parent
37b3494026
commit
ba43be30c4
@ -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 {
|
||||
|
@ -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())
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user