]> Cypherpunks repositories - gostls13.git/commitdiff
encoding/binary: remove TODO in Write and add benchmarks
authorzhouzhongyuan <zhouzhongyuan96@gmail.com>
Wed, 5 Aug 2020 07:08:54 +0000 (15:08 +0800)
committerIan Lance Taylor <iant@golang.org>
Wed, 23 Sep 2020 03:14:03 +0000 (03:14 +0000)
Benchmarks:
goos: linux
goarch: amd64
BenchmarkReadSlice1000Uint8s-8           4097088               296 ns/op        3381.06 MB/s
BenchmarkWriteSlice1000Uint8s-8          4372588               271 ns/op        3694.96 MB/s

Change-Id: I5b6ef0da5052e3381ee9c714bbff541c11ed0259
Reviewed-on: https://go-review.googlesource.com/c/go/+/246837
Run-TryBot: Alberto Donizetti <alb.donizetti@gmail.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Alberto Donizetti <alb.donizetti@gmail.com>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Trust: Alberto Donizetti <alb.donizetti@gmail.com>

src/encoding/binary/binary.go
src/encoding/binary/binary_test.go

index a99ca0199497734a2316f9f53f2996e73f2d1860..a31149979da62951f254c871a1f5e411aec4029f 100644 (file)
@@ -306,7 +306,7 @@ func Write(w io.Writer, order ByteOrder, data interface{}) error {
                case uint8:
                        bs[0] = v
                case []uint8:
-                       bs = v // TODO(josharian): avoid allocating bs in this case?
+                       bs = v
                case *int16:
                        order.PutUint16(bs, uint16(*v))
                case int16:
index aeb4212ec2e032d08f694d7720a1ee35262bf6be..5971e0966adeefb475cbef721834e65a9acd4bcb 100644 (file)
@@ -706,3 +706,27 @@ func BenchmarkWriteSlice1000Float32s(b *testing.B) {
        }
        b.StopTimer()
 }
+
+func BenchmarkReadSlice1000Uint8s(b *testing.B) {
+       bsr := &byteSliceReader{}
+       slice := make([]uint8, 1000)
+       buf := make([]byte, len(slice))
+       b.SetBytes(int64(len(buf)))
+       b.ResetTimer()
+       for i := 0; i < b.N; i++ {
+               bsr.remain = buf
+               Read(bsr, BigEndian, slice)
+       }
+}
+
+func BenchmarkWriteSlice1000Uint8s(b *testing.B) {
+       slice := make([]uint8, 1000)
+       buf := new(bytes.Buffer)
+       var w io.Writer = buf
+       b.SetBytes(1000)
+       b.ResetTimer()
+       for i := 0; i < b.N; i++ {
+               buf.Reset()
+               Write(w, BigEndian, slice)
+       }
+}