From 7714dcacbca1961543fbad0c8bc2a2afc7baaaee Mon Sep 17 00:00:00 2001 From: Martin Garton Date: Mon, 30 Sep 2019 09:27:38 +0000 Subject: encoding/binary: add float support to fast path MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This adds float type support to the main switch blocks in Read and Write, instead of falling back to reflection. This gives a considerable speedup for the float types: ReadFloats-8 129ns ± 9% 70ns ± 8% -46.02% (p=0.001 n=7+7) WriteFloats-8 131ns ± 6% 86ns ±11% -34.59% (p=0.001 n=7+7) ReadSlice1000Float32s-8 14.6µs ±14% 4.8µs ±12% -67.29% (p=0.001 n=7+7) WriteSlice1000Float32s-8 16.4µs ±20% 4.7µs ± 8% -71.01% (p=0.001 n=7+7) Change-Id: I0be99d068b07d10dd6eb1137b45eff6f7c216b87 GitHub-Last-Rev: 4ff326e99ca35977d819f0ba29c10d9efc7e811c GitHub-Pull-Request: golang/go#31803 Reviewed-on: https://go-review.googlesource.com/c/go/+/174959 Reviewed-by: Brad Fitzpatrick --- src/encoding/binary/binary.go | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) (limited to 'src/encoding/binary/binary.go') diff --git a/src/encoding/binary/binary.go b/src/encoding/binary/binary.go index 33066fc77a..a99ca01994 100644 --- a/src/encoding/binary/binary.go +++ b/src/encoding/binary/binary.go @@ -185,6 +185,10 @@ func Read(r io.Reader, order ByteOrder, data interface{}) error { *data = int64(order.Uint64(bs)) case *uint64: *data = order.Uint64(bs) + case *float32: + *data = math.Float32frombits(order.Uint32(bs)) + case *float64: + *data = math.Float64frombits(order.Uint64(bs)) case []bool: for i, x := range bs { // Easier to loop over the input for 8-bit values. data[i] = x != 0 @@ -219,6 +223,14 @@ func Read(r io.Reader, order ByteOrder, data interface{}) error { for i := range data { data[i] = order.Uint64(bs[8*i:]) } + case []float32: + for i := range data { + data[i] = math.Float32frombits(order.Uint32(bs[4*i:])) + } + case []float64: + for i := range data { + data[i] = math.Float64frombits(order.Uint64(bs[8*i:])) + } default: n = 0 // fast path doesn't apply } @@ -343,6 +355,22 @@ func Write(w io.Writer, order ByteOrder, data interface{}) error { for i, x := range v { order.PutUint64(bs[8*i:], x) } + case *float32: + order.PutUint32(bs, math.Float32bits(*v)) + case float32: + order.PutUint32(bs, math.Float32bits(v)) + case []float32: + for i, x := range v { + order.PutUint32(bs[4*i:], math.Float32bits(x)) + } + case *float64: + order.PutUint64(bs, math.Float64bits(*v)) + case float64: + order.PutUint64(bs, math.Float64bits(v)) + case []float64: + for i, x := range v { + order.PutUint64(bs[8*i:], math.Float64bits(x)) + } } _, err := w.Write(bs) return err @@ -696,6 +724,14 @@ func intDataSize(data interface{}) int { return 8 * len(data) case []uint64: return 8 * len(data) + case float32, *float32: + return 4 + case float64, *float64: + return 8 + case []float32: + return 4 * len(data) + case []float64: + return 8 * len(data) } return 0 } -- cgit v1.3