mirror of
https://github.com/golang/go
synced 2024-09-30 11:18:33 -06:00
encoding/gob: make integers encoding faster
name old time/op new time/op delta EncodeInt32Slice-4 14.6µs ± 2% 12.2µs ± 1% -16.65% (p=0.000 n=19+18) Change-Id: I078a171f1633ff81d7e3f981dc9a398309ecb2c0 Reviewed-on: https://go-review.googlesource.com/38269 Reviewed-by: Rob Pike <r@golang.org>
This commit is contained in:
parent
42e97468a1
commit
ed00cd94f2
@ -8,7 +8,9 @@ package gob
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding"
|
"encoding"
|
||||||
|
"encoding/binary"
|
||||||
"math"
|
"math"
|
||||||
|
"math/bits"
|
||||||
"reflect"
|
"reflect"
|
||||||
"sync"
|
"sync"
|
||||||
)
|
)
|
||||||
@ -107,14 +109,12 @@ func (state *encoderState) encodeUint(x uint64) {
|
|||||||
state.b.WriteByte(uint8(x))
|
state.b.WriteByte(uint8(x))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
i := uint64Size
|
|
||||||
for x > 0 {
|
binary.BigEndian.PutUint64(state.buf[1:], x)
|
||||||
state.buf[i] = uint8(x)
|
bc := bits.LeadingZeros64(x) >> 3 // 8 - bytelen(x)
|
||||||
x >>= 8
|
state.buf[bc] = uint8(bc - uint64Size) // and then we subtract 8 to get -bytelen(x)
|
||||||
i--
|
|
||||||
}
|
state.b.Write(state.buf[bc : uint64Size+1])
|
||||||
state.buf[i] = uint8(i - uint64Size) // = loop count, negated
|
|
||||||
state.b.Write(state.buf[i : uint64Size+1])
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// encodeInt writes an encoded signed integer to state.w.
|
// encodeInt writes an encoded signed integer to state.w.
|
||||||
|
@ -171,7 +171,7 @@ func BenchmarkEncodeInt32Slice(b *testing.B) {
|
|||||||
enc := NewEncoder(&buf)
|
enc := NewEncoder(&buf)
|
||||||
a := make([]int32, 1000)
|
a := make([]int32, 1000)
|
||||||
for i := range a {
|
for i := range a {
|
||||||
a[i] = 1234
|
a[i] = int32(i * 100)
|
||||||
}
|
}
|
||||||
b.ResetTimer()
|
b.ResetTimer()
|
||||||
for i := 0; i < b.N; i++ {
|
for i := 0; i < b.N; i++ {
|
||||||
|
Loading…
Reference in New Issue
Block a user