]> Cypherpunks repositories - gostls13.git/commitdiff
gob: remove another allocation.
authorRob Pike <r@golang.org>
Thu, 24 Mar 2011 04:49:19 +0000 (21:49 -0700)
committerRob Pike <r@golang.org>
Thu, 24 Mar 2011 04:49:19 +0000 (21:49 -0700)
The top level bytes.Buffer is always there and can be re-used.
Rpc goes from 83 to 79 mallocs per round trip.

R=rsc
CC=golang-dev
https://golang.org/cl/4271062

src/pkg/gob/encoder.go

index 55481a98854f5c0610606f612d03695eaea479eb..e52a4de29f7117c9f9b3d274a96835daaeb0de38 100644 (file)
@@ -21,6 +21,7 @@ type Encoder struct {
        countState *encoderState           // stage for writing counts
        freeList   *encoderState           // list of free encoderStates; avoids reallocation
        buf        []byte                  // for collecting the output.
+       byteBuf    bytes.Buffer            // buffer for top-level encoderState
        err        os.Error
 }
 
@@ -219,7 +220,8 @@ func (enc *Encoder) EncodeValue(value reflect.Value) os.Error {
        }
 
        enc.err = nil
-       state := enc.newEncoderState(new(bytes.Buffer))
+       enc.byteBuf.Reset()
+       state := enc.newEncoderState(&enc.byteBuf)
 
        enc.sendTypeDescriptor(enc.writer(), state, ut)
        enc.sendTypeId(state, ut)