mirror of
https://github.com/golang/go.git
synced 2025-05-20 14:53:23 +00: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.
|
// Add p to the running checksum a, b.
|
||||||
func update(a, b uint32, p []byte) (aa, bb uint32) {
|
func update(a, b uint32, p []byte) (aa, bb uint32) {
|
||||||
for i := 0; i < len(p); i++ {
|
for _, pi := range p {
|
||||||
a += uint32(p[i])
|
a += uint32(pi)
|
||||||
b += a
|
b += a
|
||||||
// invariant: a <= b
|
// invariant: a <= b
|
||||||
if b > (0xffffffff-255)/2 {
|
if b > (0xffffffff-255)/2 {
|
||||||
|
@ -5,6 +5,7 @@
|
|||||||
package adler32
|
package adler32
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"io"
|
"io"
|
||||||
"testing"
|
"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…
x
Reference in New Issue
Block a user