]> Cypherpunks repositories - gostls13.git/commitdiff
encoding/gob: speedup floats encoding and decoding
authorAlberto Donizetti <alb.donizetti@gmail.com>
Sat, 18 Mar 2017 17:43:20 +0000 (18:43 +0100)
committerBrad Fitzpatrick <bradfitz@golang.org>
Sat, 18 Mar 2017 18:30:41 +0000 (18:30 +0000)
By replacing bytes-reversing routines with bits.ReverseBytes64 calls.

name                     old time/op  new time/op  delta
EncodeComplex128Slice-4  35.1µs ± 1%  23.2µs ± 2%  -33.94%  (p=0.000 n=20+20)
EncodeFloat64Slice-4     17.9µs ± 1%  11.0µs ± 1%  -38.36%  (p=0.000 n=17+18)

name                     old time/op  new time/op  delta
DecodeComplex128Slice-4  79.7µs ± 0%  69.9µs ± 1%  -12.31%  (p=0.000 n=20+20)
DecodeFloat64Slice-4     47.3µs ± 1%  42.2µs ± 1%  -10.65%  (p=0.000 n=17+17)

Change-Id: I91a6401c6009b5712fca6258dd1e57c6fe68ea64
Reviewed-on: https://go-review.googlesource.com/38352
Run-TryBot: Alberto Donizetti <alb.donizetti@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
src/encoding/gob/decode.go
src/encoding/gob/encode.go

index 792c159dedaa07a8c1e7a32a27dc1caf1413bb6f..92d9d3ef8761b9fe920dff63cf444ae68b1f2df1 100644 (file)
@@ -11,6 +11,7 @@ import (
        "errors"
        "io"
        "math"
+       "math/bits"
        "reflect"
 )
 
@@ -313,12 +314,7 @@ func decUint64(i *decInstr, state *decoderState, value reflect.Value) {
 // (for example) transmit more compactly. This routine does the
 // unswizzling.
 func float64FromBits(u uint64) float64 {
-       var v uint64
-       for i := 0; i < 8; i++ {
-               v <<= 8
-               v |= u & 0xFF
-               u >>= 8
-       }
+       v := bits.ReverseBytes64(u)
        return math.Float64frombits(v)
 }
 
index d67153da90242ffa0a70ada3edad6da1a5c5a035..edf204f47d2b31d39f01c183d56a8874f8ad0092 100644 (file)
@@ -209,13 +209,7 @@ func encUint(i *encInstr, state *encoderState, v reflect.Value) {
 // swizzling.
 func floatBits(f float64) uint64 {
        u := math.Float64bits(f)
-       var v uint64
-       for i := 0; i < 8; i++ {
-               v <<= 8
-               v |= u & 0xFF
-               u >>= 8
-       }
-       return v
+       return bits.ReverseBytes64(u)
 }
 
 // encFloat encodes the floating point value (float32 float64) referenced by v.