Commit 457dfd75 authored by Rob Pike's avatar Rob Pike

gob: when possible, allow sequential decoders on the same input stream.

This can work only if there is no type info required to initialize the decoder,
but it's easy and gains a few percent in the basic benchmarks by avoiding
bufio when it's a bytes.Buffer - a testing-only scenario, I admit.
Add a comment about what Decode expects from the input.

R=rsc
CC=golang-dev
https://golang.org/cl/5165048
parent 305f167b
......@@ -29,9 +29,15 @@ type Decoder struct {
}
// NewDecoder returns a new decoder that reads from the io.Reader.
// If r does not also implement io.ByteReader, it will be wrapped in a
// bufio.Reader.
func NewDecoder(r io.Reader) *Decoder {
dec := new(Decoder)
dec.r = bufio.NewReader(r)
// We use the ability to read bytes as a plausible surrogate for buffering.
if _, ok := r.(io.ByteReader); !ok {
r = bufio.NewReader(r)
}
dec.r = r
dec.wireType = make(map[typeId]*wireType)
dec.decoderCache = make(map[reflect.Type]map[typeId]**decEngine)
dec.ignorerCache = make(map[typeId]**decEngine)
......
......@@ -638,3 +638,28 @@ func TestBadCount(t *testing.T) {
t.Error("expected bad count error; got", err)
}
}
// Verify that sequential Decoders built on a single input will
// succeed if the input implements ReadByte and there is no
// type information in the stream.
func TestSequentialDecoder(t *testing.T) {
b := new(bytes.Buffer)
enc := NewEncoder(b)
const count = 10
for i := 0; i < count; i++ {
s := fmt.Sprintf("%d", i)
if err := enc.Encode(s); err != nil {
t.Error("encoder fail:", err)
}
}
for i := 0; i < count; i++ {
dec := NewDecoder(b)
var s string
if err := dec.Decode(&s); err != nil {
t.Fatal("decoder fail:", err)
}
if s != fmt.Sprintf("%d", i) {
t.Fatalf("decode expected %d got %s", i, s)
}
}
}
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