]> Cypherpunks repositories - gostls13.git/commitdiff
hash: more efficient memory allocation
authorPascal S. de Kloe <pascal@quies.net>
Tue, 10 Apr 2012 19:15:39 +0000 (15:15 -0400)
committerRuss Cox <rsc@golang.org>
Tue, 10 Apr 2012 19:15:39 +0000 (15:15 -0400)
Feed append the complete content at once.

BenchmarkAdler32KB       1000000              2534 ns/op         404.05 MB/s
BenchmarkCrc32KB          500000              4757 ns/op         215.26 MB/s
BenchmarkCrc64KB          500000              4769 ns/op         214.70 MB/s
BenchmarkFnv32KB         1000000              2417 ns/op         423.64 MB/s
BenchmarkFnv32aKB        1000000              2408 ns/op         425.23 MB/s
BenchmarkFnv64KB          500000              4262 ns/op         240.21 MB/s
BenchmarkFnv64aKB         500000              4234 ns/op         241.83 MB/s

R=iant, rsc, r, minux.ma
CC=golang-dev
https://golang.org/cl/5937053

src/pkg/hash/adler32/adler32.go
src/pkg/hash/adler32/adler32_test.go
src/pkg/hash/crc32/crc32.go
src/pkg/hash/crc32/crc32_test.go
src/pkg/hash/crc64/crc64.go
src/pkg/hash/crc64/crc64_test.go
src/pkg/hash/fnv/fnv.go
src/pkg/hash/fnv/fnv_test.go

index 64fe68c443f027a2fab301e1a501a3ddeca76da1..7e483b3f768dabc41eff847171e6f01eb8222937 100644 (file)
@@ -75,11 +75,7 @@ func (d *digest) Sum32() uint32 { return finish(d.a, d.b) }
 
 func (d *digest) Sum(in []byte) []byte {
        s := d.Sum32()
-       in = append(in, byte(s>>24))
-       in = append(in, byte(s>>16))
-       in = append(in, byte(s>>8))
-       in = append(in, byte(s))
-       return in
+       return append(in, byte(s>>24), byte(s>>16), byte(s>>8), byte(s))
 }
 
 // Checksum returns the Adler-32 checksum of data.
index 01f931c6859850b309a2b2b5db90326e29ddd55f..31c602977488d18998751c5b7cec2f4b39b5c3bd 100644 (file)
@@ -5,7 +5,6 @@
 package adler32
 
 import (
-       "bytes"
        "io"
        "testing"
 )
@@ -63,15 +62,19 @@ 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))
+func BenchmarkAdler32KB(b *testing.B) {
+       b.SetBytes(1024)
+       data := make([]byte, 1024)
+       for i := range data {
+               data[i] = byte(i)
        }
-       b.StartTimer()
+       h := New()
+       in := make([]byte, 0, h.Size())
+
+       b.ResetTimer()
        for i := 0; i < b.N; i++ {
-               c.Write(buf.Bytes())
+               h.Reset()
+               h.Write(data)
+               h.Sum(in)
        }
 }
index 236d7787289c170234af166d571218cefad7a238..a2a21a06f950bf722f78adf70a46867b2c0d9042 100644 (file)
@@ -123,11 +123,7 @@ func (d *digest) Sum32() uint32 { return d.crc }
 
 func (d *digest) Sum(in []byte) []byte {
        s := d.Sum32()
-       in = append(in, byte(s>>24))
-       in = append(in, byte(s>>16))
-       in = append(in, byte(s>>8))
-       in = append(in, byte(s))
-       return in
+       return append(in, byte(s>>24), byte(s>>16), byte(s>>8), byte(s))
 }
 
 // Checksum returns the CRC-32 checksum of data
index 7e82dd755e7978f50464b00c11d4458a612aca87..75dc26e7cc249aab1ed80b7b1c9184a2308cfb16 100644 (file)
@@ -82,16 +82,18 @@ func TestGolden(t *testing.T) {
 }
 
 func BenchmarkCrc32KB(b *testing.B) {
-       b.StopTimer()
-       data := make([]uint8, 1024)
-       for i := 0; i < 1024; i++ {
-               data[i] = uint8(i)
+       b.SetBytes(1024)
+       data := make([]byte, 1024)
+       for i := range data {
+               data[i] = byte(i)
        }
-       c := NewIEEE()
-       b.StartTimer()
-       b.SetBytes(int64(len(data)))
+       h := NewIEEE()
+       in := make([]byte, 0, h.Size())
 
+       b.ResetTimer()
        for i := 0; i < b.N; i++ {
-               c.Write(data)
+               h.Reset()
+               h.Write(data)
+               h.Sum(in)
        }
 }
index 5b64390f3d5b51d6e72ea3448c6e6983dc7b5e6b..692586798848cbfe6dad7cb386b3fad20ca238e8 100644 (file)
@@ -79,15 +79,7 @@ func (d *digest) Sum64() uint64 { return d.crc }
 
 func (d *digest) Sum(in []byte) []byte {
        s := d.Sum64()
-       in = append(in, byte(s>>56))
-       in = append(in, byte(s>>48))
-       in = append(in, byte(s>>40))
-       in = append(in, byte(s>>32))
-       in = append(in, byte(s>>24))
-       in = append(in, byte(s>>16))
-       in = append(in, byte(s>>8))
-       in = append(in, byte(s))
-       return in
+       return append(in, byte(s>>56), byte(s>>48), byte(s>>40), byte(s>>32), byte(s>>24), byte(s>>16), byte(s>>8), byte(s))
 }
 
 // Checksum returns the CRC-64 checksum of data
index e932524e0925026ddebb67c883c934a908a12145..81a87b56e355ab2060af2f7f4457d2136cf6d592 100644 (file)
@@ -64,15 +64,18 @@ func TestGolden(t *testing.T) {
 }
 
 func BenchmarkCrc64KB(b *testing.B) {
-       b.StopTimer()
-       data := make([]uint8, 1024)
-       for i := 0; i < 1024; i++ {
-               data[i] = uint8(i)
+       b.SetBytes(1024)
+       data := make([]byte, 1024)
+       for i := range data {
+               data[i] = byte(i)
        }
-       c := New(tab)
-       b.StartTimer()
+       h := New(tab)
+       in := make([]byte, 0, h.Size())
 
+       b.ResetTimer()
        for i := 0; i < b.N; i++ {
-               c.Write(data)
+               h.Reset()
+               h.Write(data)
+               h.Sum(in)
        }
 }
index ea50198180f54ad427e9c8c8b4447d8468144a2e..b5ecd4a7c6d194d580675098ab9fe4a1012ac8fb 100644 (file)
@@ -111,44 +111,20 @@ func (s *sum64a) BlockSize() int { return 1 }
 
 func (s *sum32) Sum(in []byte) []byte {
        v := uint32(*s)
-       in = append(in, byte(v>>24))
-       in = append(in, byte(v>>16))
-       in = append(in, byte(v>>8))
-       in = append(in, byte(v))
-       return in
+       return append(in, byte(v>>24), byte(v>>16), byte(v>>8), byte(v))
 }
 
 func (s *sum32a) Sum(in []byte) []byte {
        v := uint32(*s)
-       in = append(in, byte(v>>24))
-       in = append(in, byte(v>>16))
-       in = append(in, byte(v>>8))
-       in = append(in, byte(v))
-       return in
+       return append(in, byte(v>>24), byte(v>>16), byte(v>>8), byte(v))
 }
 
 func (s *sum64) Sum(in []byte) []byte {
        v := uint64(*s)
-       in = append(in, byte(v>>56))
-       in = append(in, byte(v>>48))
-       in = append(in, byte(v>>40))
-       in = append(in, byte(v>>32))
-       in = append(in, byte(v>>24))
-       in = append(in, byte(v>>16))
-       in = append(in, byte(v>>8))
-       in = append(in, byte(v))
-       return in
+       return append(in, byte(v>>56), byte(v>>48), byte(v>>40), byte(v>>32), byte(v>>24), byte(v>>16), byte(v>>8), byte(v))
 }
 
 func (s *sum64a) Sum(in []byte) []byte {
        v := uint64(*s)
-       in = append(in, byte(v>>56))
-       in = append(in, byte(v>>48))
-       in = append(in, byte(v>>40))
-       in = append(in, byte(v>>32))
-       in = append(in, byte(v>>24))
-       in = append(in, byte(v>>16))
-       in = append(in, byte(v>>8))
-       in = append(in, byte(v))
-       return in
+       return append(in, byte(v>>56), byte(v>>48), byte(v>>40), byte(v>>32), byte(v>>24), byte(v>>16), byte(v>>8), byte(v))
 }
index 17454deda904ff29c0f024c4dc537bd17e82f111..89d39b38ad3922403e03fb49b52fa174cc9a7e44 100644 (file)
@@ -11,8 +11,6 @@ import (
        "testing"
 )
 
-const testDataSize = 40
-
 type golden struct {
        sum  []byte
        text string
@@ -134,34 +132,34 @@ func testIntegrity(t *testing.T, h hash.Hash) {
        }
 }
 
-func Benchmark32(b *testing.B) {
-       benchmark(b, New32())
+func BenchmarkFnv32KB(b *testing.B) {
+       benchmarkKB(b, New32())
 }
 
-func Benchmark32a(b *testing.B) {
-       benchmark(b, New32a())
+func BenchmarkFnv32aKB(b *testing.B) {
+       benchmarkKB(b, New32a())
 }
 
-func Benchmark64(b *testing.B) {
-       benchmark(b, New64())
+func BenchmarkFnv64KB(b *testing.B) {
+       benchmarkKB(b, New64())
 }
 
-func Benchmark64a(b *testing.B) {
-       benchmark(b, New64a())
+func BenchmarkFnv64aKB(b *testing.B) {
+       benchmarkKB(b, New64a())
 }
 
-func benchmark(b *testing.B, h hash.Hash) {
-       b.ResetTimer()
-       b.SetBytes(testDataSize)
-       data := make([]byte, testDataSize)
+func benchmarkKB(b *testing.B, h hash.Hash) {
+       b.SetBytes(1024)
+       data := make([]byte, 1024)
        for i := range data {
-               data[i] = byte(i + 'a')
+               data[i] = byte(i)
        }
+       in := make([]byte, 0, h.Size())
 
-       b.StartTimer()
-       for todo := b.N; todo != 0; todo-- {
+       b.ResetTimer()
+       for i := 0; i < b.N; i++ {
                h.Reset()
                h.Write(data)
-               h.Sum(nil)
+               h.Sum(in)
        }
 }