summaryrefslogtreecommitdiff
path: root/src/pkg/gob/decoder.go
diff options
context:
space:
mode:
authorRob Pike <r@golang.org>2009-07-16 13:05:46 -0700
committerRob Pike <r@golang.org>2009-07-16 13:05:46 -0700
commit943d283e693285cd8fa28a5589699654373b66f4 (patch)
tree1aec49c2b73a5df6b4f6263bb6cce14a7e9a6f56 /src/pkg/gob/decoder.go
parentd72bc1cb6bf95a25937aba38a5f49c929d308f7a (diff)
downloadgolang-943d283e693285cd8fa28a5589699654373b66f4.tar.gz
clean up the decode loop and fix a couple of bad prints
R=rsc DELTA=15 (8 added, 2 deleted, 5 changed) OCL=31738 CL=31738
Diffstat (limited to 'src/pkg/gob/decoder.go')
-rw-r--r--src/pkg/gob/decoder.go14
1 files changed, 10 insertions, 4 deletions
diff --git a/src/pkg/gob/decoder.go b/src/pkg/gob/decoder.go
index e824ac754..9257f7c23 100644
--- a/src/pkg/gob/decoder.go
+++ b/src/pkg/gob/decoder.go
@@ -19,6 +19,7 @@ type Decoder struct {
seen map[TypeId] *wireType; // which types we've already seen described
state *decodeState; // reads data from in-memory buffer
countState *decodeState; // reads counts from wire
+ buf []byte;
oneByte []byte;
}
@@ -63,10 +64,15 @@ func (dec *Decoder) Decode(e interface{}) os.Error {
return err;
}
+ // Allocate the buffer.
+ if nbytes > uint64(len(dec.buf)) {
+ dec.buf = make([]byte, nbytes + 1000);
+ }
+ dec.state.b = bytes.NewBuffer(dec.buf[0:nbytes]);
+
// Read the data
- buf := make([]byte, nbytes); // TODO(r): avoid repeated allocation
var n int;
- n, err = dec.r.Read(buf);
+ n, err = dec.r.Read(dec.buf[0:nbytes]);
if err != nil {
return err;
}
@@ -74,13 +80,13 @@ func (dec *Decoder) Decode(e interface{}) os.Error {
return os.ErrorString("gob decode: short read");
}
- dec.state.b = bytes.NewBuffer(buf); // TODO(r): avoid repeated allocation
// Receive a type id.
id := TypeId(decodeInt(dec.state));
if dec.state.err != nil {
return dec.state.err
}
+ // Is it a type?
if id < 0 { // 0 is the error state, handled above
// If the id is negative, we have a type.
dec.recvType(-id);
@@ -90,7 +96,7 @@ func (dec *Decoder) Decode(e interface{}) os.Error {
continue;
}
- // we have a value
+ // No, it's a value.
info := getTypeInfo(rt);
// Check type compatibility.