mirror of
https://github.com/golang/go.git
synced 2025-11-05 19:20:58 +00:00
encoding/gob: reduce the amount of memory allocations.
Benchmark results: benchmark old ns/op new ns/op delta BenchmarkEndToEndPipe-4 7547 7294 -3.35% BenchmarkEndToEndByteBuffer-4 5146 5092 -1.05% BenchmarkEndToEndSliceByteBuffer-4 552779 439768 -20.44% BenchmarkEncodeComplex128Slice-4 266370 266184 -0.07% BenchmarkEncodeFloat64Slice-4 111891 110258 -1.46% BenchmarkEncodeInt32Slice-4 74482 74080 -0.54% BenchmarkEncodeStringSlice-4 84404 84279 -0.15% BenchmarkEncodeInterfaceSlice-4 3942925 3045995 -22.75% BenchmarkDecodeComplex128Slice-4 451837 415282 -8.09% BenchmarkDecodeFloat64Slice-4 283584 262558 -7.41% BenchmarkDecodeInt32Slice-4 246571 237383 -3.73% BenchmarkDecodeStringSlice-4 734210 479625 -34.67% BenchmarkDecodeInterfaceSlice-4 4778225 4160935 -12.92% benchmark old allocs new allocs delta BenchmarkEndToEndPipe-4 3 2 -33.33% BenchmarkEndToEndByteBuffer-4 3 2 -33.33% BenchmarkEndToEndSliceByteBuffer-4 1002 402 -59.88% BenchmarkEncodeComplex128Slice-4 1 1 +0.00% BenchmarkEncodeFloat64Slice-4 1 1 +0.00% BenchmarkEncodeInt32Slice-4 1 1 +0.00% BenchmarkEncodeStringSlice-4 1 1 +0.00% BenchmarkEncodeInterfaceSlice-4 3001 1 -99.97% BenchmarkDecodeComplex128Slice-4 188 185 -1.60% BenchmarkDecodeFloat64Slice-4 188 185 -1.60% BenchmarkDecodeInt32Slice-4 188 185 -1.60% BenchmarkDecodeStringSlice-4 2188 1185 -45.84% BenchmarkDecodeInterfaceSlice-4 6197 4194 -32.32% benchmark old bytes new bytes delta BenchmarkEndToEndPipe-4 64 48 -25.00% BenchmarkEndToEndByteBuffer-4 64 48 -25.00% BenchmarkEndToEndSliceByteBuffer-4 34551 10554 -69.45% BenchmarkEncodeComplex128Slice-4 55 55 +0.00% BenchmarkEncodeFloat64Slice-4 33 33 +0.00% BenchmarkEncodeInt32Slice-4 32 32 +0.00% BenchmarkEncodeStringSlice-4 36 36 +0.00% BenchmarkEncodeInterfaceSlice-4 144555 347 -99.76% BenchmarkDecodeComplex128Slice-4 28240 28097 -0.51% BenchmarkDecodeFloat64Slice-4 11840 11697 -1.21% BenchmarkDecodeInt32Slice-4 10817 10673 -1.33% BenchmarkDecodeStringSlice-4 56128 39985 -28.76% BenchmarkDecodeInterfaceSlice-4 132565 100421 -24.25% Change-Id: Ief7c7706b1f2916486ab7190b81aafbb16b70f1e Reviewed-on: https://go-review.googlesource.com/13660 Reviewed-by: Russ Cox <rsc@golang.org>
This commit is contained in:
parent
c4be790c0e
commit
a48de745b2
7 changed files with 105 additions and 43 deletions
|
|
@ -10,6 +10,7 @@ import (
|
|||
"encoding"
|
||||
"math"
|
||||
"reflect"
|
||||
"sync"
|
||||
)
|
||||
|
||||
const uint64Size = 8
|
||||
|
|
@ -36,6 +37,14 @@ type encBuffer struct {
|
|||
scratch [64]byte
|
||||
}
|
||||
|
||||
var encBufferPool = sync.Pool{
|
||||
New: func() interface{} {
|
||||
e := new(encBuffer)
|
||||
e.data = e.scratch[0:0]
|
||||
return e
|
||||
},
|
||||
}
|
||||
|
||||
func (e *encBuffer) WriteByte(c byte) {
|
||||
e.data = append(e.data, c)
|
||||
}
|
||||
|
|
@ -58,7 +67,11 @@ func (e *encBuffer) Bytes() []byte {
|
|||
}
|
||||
|
||||
func (e *encBuffer) Reset() {
|
||||
e.data = e.data[0:0]
|
||||
if len(e.data) >= tooBig {
|
||||
e.data = e.scratch[0:0]
|
||||
} else {
|
||||
e.data = e.data[0:0]
|
||||
}
|
||||
}
|
||||
|
||||
func (enc *Encoder) newEncoderState(b *encBuffer) *encoderState {
|
||||
|
|
@ -407,7 +420,7 @@ func (enc *Encoder) encodeInterface(b *encBuffer, iv reflect.Value) {
|
|||
// Encode the value into a new buffer. Any nested type definitions
|
||||
// should be written to b, before the encoded value.
|
||||
enc.pushWriter(b)
|
||||
data := new(encBuffer)
|
||||
data := encBufferPool.Get().(*encBuffer)
|
||||
data.Write(spaceForLength)
|
||||
enc.encode(data, elem, ut)
|
||||
if enc.err != nil {
|
||||
|
|
@ -415,6 +428,8 @@ func (enc *Encoder) encodeInterface(b *encBuffer, iv reflect.Value) {
|
|||
}
|
||||
enc.popWriter()
|
||||
enc.writeMessage(b, data)
|
||||
data.Reset()
|
||||
encBufferPool.Put(data)
|
||||
if enc.err != nil {
|
||||
error_(enc.err)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue