Commit ce5bbfdd authored by Rob Pike's avatar Rob Pike

encoding/gob: simplify allocation in decode.

The old code's structure needed to track indirections because of the
use of unsafe. That is no longer necessary, so we can remove all
that tracking. The code cleans up considerably but is a little slower.
We may be able to recover that performance drop. I believe the
code quality improvement is worthwhile regardless.

BenchmarkEndToEndPipe           5610          5780          +3.03%
BenchmarkEndToEndByteBuffer     3156          3222          +2.09%

LGTM=rsc
R=rsc
CC=golang-codereviews
https://golang.org/cl/103700043
parent 6a228239
...@@ -323,7 +323,7 @@ func execDec(typ string, instr *decInstr, state *decoderState, t *testing.T, val ...@@ -323,7 +323,7 @@ func execDec(typ string, instr *decInstr, state *decoderState, t *testing.T, val
if v+state.fieldnum != 6 { if v+state.fieldnum != 6 {
t.Fatalf("decoding field number %d, got %d", 6, v+state.fieldnum) t.Fatalf("decoding field number %d, got %d", 6, v+state.fieldnum)
} }
instr.op(instr, state, decIndirect(value, instr.indir)) instr.op(instr, state, value)
state.fieldnum = 6 state.fieldnum = 6
} }
...@@ -342,7 +342,7 @@ func TestScalarDecInstructions(t *testing.T) { ...@@ -342,7 +342,7 @@ func TestScalarDecInstructions(t *testing.T) {
// bool // bool
{ {
var data bool var data bool
instr := &decInstr{decBool, 6, nil, 1, ovfl} instr := &decInstr{decBool, 6, nil, ovfl}
state := newDecodeStateFromData(boolResult) state := newDecodeStateFromData(boolResult)
execDec("bool", instr, state, t, reflect.ValueOf(&data)) execDec("bool", instr, state, t, reflect.ValueOf(&data))
if data != true { if data != true {
...@@ -352,7 +352,7 @@ func TestScalarDecInstructions(t *testing.T) { ...@@ -352,7 +352,7 @@ func TestScalarDecInstructions(t *testing.T) {
// int // int
{ {
var data int var data int
instr := &decInstr{decOpTable[reflect.Int], 6, nil, 1, ovfl} instr := &decInstr{decOpTable[reflect.Int], 6, nil, ovfl}
state := newDecodeStateFromData(signedResult) state := newDecodeStateFromData(signedResult)
execDec("int", instr, state, t, reflect.ValueOf(&data)) execDec("int", instr, state, t, reflect.ValueOf(&data))
if data != 17 { if data != 17 {
...@@ -363,7 +363,7 @@ func TestScalarDecInstructions(t *testing.T) { ...@@ -363,7 +363,7 @@ func TestScalarDecInstructions(t *testing.T) {
// uint // uint
{ {
var data uint var data uint
instr := &decInstr{decOpTable[reflect.Uint], 6, nil, 1, ovfl} instr := &decInstr{decOpTable[reflect.Uint], 6, nil, ovfl}
state := newDecodeStateFromData(unsignedResult) state := newDecodeStateFromData(unsignedResult)
execDec("uint", instr, state, t, reflect.ValueOf(&data)) execDec("uint", instr, state, t, reflect.ValueOf(&data))
if data != 17 { if data != 17 {
...@@ -374,7 +374,7 @@ func TestScalarDecInstructions(t *testing.T) { ...@@ -374,7 +374,7 @@ func TestScalarDecInstructions(t *testing.T) {
// int8 // int8
{ {
var data int8 var data int8
instr := &decInstr{decInt8, 6, nil, 1, ovfl} instr := &decInstr{decInt8, 6, nil, ovfl}
state := newDecodeStateFromData(signedResult) state := newDecodeStateFromData(signedResult)
execDec("int8", instr, state, t, reflect.ValueOf(&data)) execDec("int8", instr, state, t, reflect.ValueOf(&data))
if data != 17 { if data != 17 {
...@@ -385,7 +385,7 @@ func TestScalarDecInstructions(t *testing.T) { ...@@ -385,7 +385,7 @@ func TestScalarDecInstructions(t *testing.T) {
// uint8 // uint8
{ {
var data uint8 var data uint8
instr := &decInstr{decUint8, 6, nil, 1, ovfl} instr := &decInstr{decUint8, 6, nil, ovfl}
state := newDecodeStateFromData(unsignedResult) state := newDecodeStateFromData(unsignedResult)
execDec("uint8", instr, state, t, reflect.ValueOf(&data)) execDec("uint8", instr, state, t, reflect.ValueOf(&data))
if data != 17 { if data != 17 {
...@@ -396,7 +396,7 @@ func TestScalarDecInstructions(t *testing.T) { ...@@ -396,7 +396,7 @@ func TestScalarDecInstructions(t *testing.T) {
// int16 // int16
{ {
var data int16 var data int16
instr := &decInstr{decInt16, 6, nil, 1, ovfl} instr := &decInstr{decInt16, 6, nil, ovfl}
state := newDecodeStateFromData(signedResult) state := newDecodeStateFromData(signedResult)
execDec("int16", instr, state, t, reflect.ValueOf(&data)) execDec("int16", instr, state, t, reflect.ValueOf(&data))
if data != 17 { if data != 17 {
...@@ -407,7 +407,7 @@ func TestScalarDecInstructions(t *testing.T) { ...@@ -407,7 +407,7 @@ func TestScalarDecInstructions(t *testing.T) {
// uint16 // uint16
{ {
var data uint16 var data uint16
instr := &decInstr{decUint16, 6, nil, 1, ovfl} instr := &decInstr{decUint16, 6, nil, ovfl}
state := newDecodeStateFromData(unsignedResult) state := newDecodeStateFromData(unsignedResult)
execDec("uint16", instr, state, t, reflect.ValueOf(&data)) execDec("uint16", instr, state, t, reflect.ValueOf(&data))
if data != 17 { if data != 17 {
...@@ -418,7 +418,7 @@ func TestScalarDecInstructions(t *testing.T) { ...@@ -418,7 +418,7 @@ func TestScalarDecInstructions(t *testing.T) {
// int32 // int32
{ {
var data int32 var data int32
instr := &decInstr{decInt32, 6, nil, 1, ovfl} instr := &decInstr{decInt32, 6, nil, ovfl}
state := newDecodeStateFromData(signedResult) state := newDecodeStateFromData(signedResult)
execDec("int32", instr, state, t, reflect.ValueOf(&data)) execDec("int32", instr, state, t, reflect.ValueOf(&data))
if data != 17 { if data != 17 {
...@@ -429,7 +429,7 @@ func TestScalarDecInstructions(t *testing.T) { ...@@ -429,7 +429,7 @@ func TestScalarDecInstructions(t *testing.T) {
// uint32 // uint32
{ {
var data uint32 var data uint32
instr := &decInstr{decUint32, 6, nil, 1, ovfl} instr := &decInstr{decUint32, 6, nil, ovfl}
state := newDecodeStateFromData(unsignedResult) state := newDecodeStateFromData(unsignedResult)
execDec("uint32", instr, state, t, reflect.ValueOf(&data)) execDec("uint32", instr, state, t, reflect.ValueOf(&data))
if data != 17 { if data != 17 {
...@@ -440,7 +440,7 @@ func TestScalarDecInstructions(t *testing.T) { ...@@ -440,7 +440,7 @@ func TestScalarDecInstructions(t *testing.T) {
// uintptr // uintptr
{ {
var data uintptr var data uintptr
instr := &decInstr{decOpTable[reflect.Uintptr], 6, nil, 1, ovfl} instr := &decInstr{decOpTable[reflect.Uintptr], 6, nil, ovfl}
state := newDecodeStateFromData(unsignedResult) state := newDecodeStateFromData(unsignedResult)
execDec("uintptr", instr, state, t, reflect.ValueOf(&data)) execDec("uintptr", instr, state, t, reflect.ValueOf(&data))
if data != 17 { if data != 17 {
...@@ -451,7 +451,7 @@ func TestScalarDecInstructions(t *testing.T) { ...@@ -451,7 +451,7 @@ func TestScalarDecInstructions(t *testing.T) {
// int64 // int64
{ {
var data int64 var data int64
instr := &decInstr{decInt64, 6, nil, 1, ovfl} instr := &decInstr{decInt64, 6, nil, ovfl}
state := newDecodeStateFromData(signedResult) state := newDecodeStateFromData(signedResult)
execDec("int64", instr, state, t, reflect.ValueOf(&data)) execDec("int64", instr, state, t, reflect.ValueOf(&data))
if data != 17 { if data != 17 {
...@@ -462,7 +462,7 @@ func TestScalarDecInstructions(t *testing.T) { ...@@ -462,7 +462,7 @@ func TestScalarDecInstructions(t *testing.T) {
// uint64 // uint64
{ {
var data uint64 var data uint64
instr := &decInstr{decUint64, 6, nil, 1, ovfl} instr := &decInstr{decUint64, 6, nil, ovfl}
state := newDecodeStateFromData(unsignedResult) state := newDecodeStateFromData(unsignedResult)
execDec("uint64", instr, state, t, reflect.ValueOf(&data)) execDec("uint64", instr, state, t, reflect.ValueOf(&data))
if data != 17 { if data != 17 {
...@@ -473,7 +473,7 @@ func TestScalarDecInstructions(t *testing.T) { ...@@ -473,7 +473,7 @@ func TestScalarDecInstructions(t *testing.T) {
// float32 // float32
{ {
var data float32 var data float32
instr := &decInstr{decFloat32, 6, nil, 1, ovfl} instr := &decInstr{decFloat32, 6, nil, ovfl}
state := newDecodeStateFromData(floatResult) state := newDecodeStateFromData(floatResult)
execDec("float32", instr, state, t, reflect.ValueOf(&data)) execDec("float32", instr, state, t, reflect.ValueOf(&data))
if data != 17 { if data != 17 {
...@@ -484,7 +484,7 @@ func TestScalarDecInstructions(t *testing.T) { ...@@ -484,7 +484,7 @@ func TestScalarDecInstructions(t *testing.T) {
// float64 // float64
{ {
var data float64 var data float64
instr := &decInstr{decFloat64, 6, nil, 1, ovfl} instr := &decInstr{decFloat64, 6, nil, ovfl}
state := newDecodeStateFromData(floatResult) state := newDecodeStateFromData(floatResult)
execDec("float64", instr, state, t, reflect.ValueOf(&data)) execDec("float64", instr, state, t, reflect.ValueOf(&data))
if data != 17 { if data != 17 {
...@@ -495,7 +495,7 @@ func TestScalarDecInstructions(t *testing.T) { ...@@ -495,7 +495,7 @@ func TestScalarDecInstructions(t *testing.T) {
// complex64 // complex64
{ {
var data complex64 var data complex64
instr := &decInstr{decOpTable[reflect.Complex64], 6, nil, 1, ovfl} instr := &decInstr{decOpTable[reflect.Complex64], 6, nil, ovfl}
state := newDecodeStateFromData(complexResult) state := newDecodeStateFromData(complexResult)
execDec("complex", instr, state, t, reflect.ValueOf(&data)) execDec("complex", instr, state, t, reflect.ValueOf(&data))
if data != 17+19i { if data != 17+19i {
...@@ -506,7 +506,7 @@ func TestScalarDecInstructions(t *testing.T) { ...@@ -506,7 +506,7 @@ func TestScalarDecInstructions(t *testing.T) {
// complex128 // complex128
{ {
var data complex128 var data complex128
instr := &decInstr{decOpTable[reflect.Complex128], 6, nil, 1, ovfl} instr := &decInstr{decOpTable[reflect.Complex128], 6, nil, ovfl}
state := newDecodeStateFromData(complexResult) state := newDecodeStateFromData(complexResult)
execDec("complex", instr, state, t, reflect.ValueOf(&data)) execDec("complex", instr, state, t, reflect.ValueOf(&data))
if data != 17+19i { if data != 17+19i {
...@@ -517,7 +517,7 @@ func TestScalarDecInstructions(t *testing.T) { ...@@ -517,7 +517,7 @@ func TestScalarDecInstructions(t *testing.T) {
// bytes == []uint8 // bytes == []uint8
{ {
var data []byte var data []byte
instr := &decInstr{decUint8Slice, 6, nil, 1, ovfl} instr := &decInstr{decUint8Slice, 6, nil, ovfl}
state := newDecodeStateFromData(bytesResult) state := newDecodeStateFromData(bytesResult)
execDec("bytes", instr, state, t, reflect.ValueOf(&data)) execDec("bytes", instr, state, t, reflect.ValueOf(&data))
if string(data) != "hello" { if string(data) != "hello" {
...@@ -528,7 +528,7 @@ func TestScalarDecInstructions(t *testing.T) { ...@@ -528,7 +528,7 @@ func TestScalarDecInstructions(t *testing.T) {
// string // string
{ {
var data string var data string
instr := &decInstr{decString, 6, nil, 1, ovfl} instr := &decInstr{decString, 6, nil, ovfl}
state := newDecodeStateFromData(bytesResult) state := newDecodeStateFromData(bytesResult)
execDec("bytes", instr, state, t, reflect.ValueOf(&data)) execDec("bytes", instr, state, t, reflect.ValueOf(&data))
if data != "hello" { if data != "hello" {
......
This diff is collapsed.
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment