From 9d1a33facc265c46aba3dd246add7cd0f6916da6 Mon Sep 17 00:00:00 2001 From: Matt Joiner Date: Wed, 15 Jun 2016 14:51:58 +1000 Subject: [PATCH] bencode: Unembed the Reader type, and minimize the interface surface Want to remove some unnecessary allocations. --- bencode/api.go | 4 ++-- bencode/decode.go | 22 ++++++++++++---------- 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/bencode/api.go b/bencode/api.go index bf368cf9..9a77583e 100644 --- a/bencode/api.go +++ b/bencode/api.go @@ -128,7 +128,7 @@ func Marshal(v interface{}) ([]byte, error) { // Unmarshal the bencode value in the 'data' to a value pointed by the 'v' // pointer, return a non-nil error if any. func Unmarshal(data []byte, v interface{}) error { - e := decoder{Reader: bufio.NewReader(bytes.NewBuffer(data))} + e := decoder{r: bufio.NewReader(bytes.NewBuffer(data))} return e.decode(v) } @@ -141,7 +141,7 @@ type Decoder struct { } func NewDecoder(r io.Reader) *Decoder { - return &Decoder{decoder{Reader: bufio.NewReader(r)}} + return &Decoder{decoder{r: bufio.NewReader(r)}} } func (d *Decoder) Decode(v interface{}) error { diff --git a/bencode/decode.go b/bencode/decode.go index 9cb2a0d6..ebd4684e 100644 --- a/bencode/decode.go +++ b/bencode/decode.go @@ -1,7 +1,6 @@ package bencode import ( - "bufio" "bytes" "errors" "fmt" @@ -14,7 +13,10 @@ import ( ) type decoder struct { - *bufio.Reader + r interface { + io.ByteScanner + io.Reader + } offset int64 buf bytes.Buffer key string @@ -51,7 +53,7 @@ func check_for_unexpected_eof(err error, offset int64) { } func (d *decoder) read_byte() byte { - b, err := d.ReadByte() + b, err := d.r.ReadByte() if err != nil { check_for_unexpected_eof(err, d.offset) panic(err) @@ -145,7 +147,7 @@ func (d *decoder) parse_string(v reflect.Value) { check_for_int_parse_error(err, start) d.buf.Reset() - n, err := io.CopyN(&d.buf, d, length) + n, err := io.CopyN(&d.buf, d.r, length) d.offset += n if err != nil { check_for_unexpected_eof(err, d.offset) @@ -329,12 +331,12 @@ func (d *decoder) parse_list(v reflect.Value) { } func (d *decoder) read_one_value() bool { - b, err := d.ReadByte() + b, err := d.r.ReadByte() if err != nil { panic(err) } if b == 'e' { - d.UnreadByte() + d.r.UnreadByte() return false } else { d.offset++ @@ -360,7 +362,7 @@ func (d *decoder) read_one_value() bool { check_for_int_parse_error(err, d.offset-1) d.buf.WriteString(":") - n, err := io.CopyN(&d.buf, d, length) + n, err := io.CopyN(&d.buf, d.r, length) d.offset += n if err != nil { check_for_unexpected_eof(err, d.offset) @@ -429,7 +431,7 @@ func (d *decoder) parse_value(v reflect.Value) bool { return true } - b, err := d.ReadByte() + b, err := d.r.ReadByte() if err != nil { panic(err) } @@ -468,7 +470,7 @@ func (d *decoder) raiseUnknownValueType(b byte, offset int64) { } func (d *decoder) parse_value_interface() (interface{}, bool) { - b, err := d.ReadByte() + b, err := d.r.ReadByte() if err != nil { panic(err) } @@ -535,7 +537,7 @@ func (d *decoder) parse_string_interface() interface{} { check_for_int_parse_error(err, start) d.buf.Reset() - n, err := io.CopyN(&d.buf, d, length) + n, err := io.CopyN(&d.buf, d.r, length) d.offset += n if err != nil { check_for_unexpected_eof(err, d.offset)