From aeb6cea1cf71860bdcdee25c8ce8d68bdd8f2e48 Mon Sep 17 00:00:00 2001 From: Jack Christensen Date: Fri, 27 Jun 2014 08:20:15 -0500 Subject: [PATCH] Replace binary.Read It does memory allocs that can be avoided by passing a slice directly to binary.BigEndian.* --- conn.go | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/conn.go b/conn.go index 1ca60cdc..836818d9 100644 --- a/conn.go +++ b/conn.go @@ -522,12 +522,13 @@ func (c *Conn) SelectValueTo(w io.Writer, sql string, arguments ...interface{}) } func (c *Conn) rxDataRowValueTo(w io.Writer, bodySize int32) (err error) { - var columnCount int16 - err = binary.Read(c.reader, binary.BigEndian, &columnCount) + b := make([]byte, 2) + _, err = io.ReadFull(c.reader, b) if err != nil { c.die(err) return } + columnCount := int16(binary.BigEndian.Uint16(b)) if columnCount != 1 { // Read the rest of the data row so it can be discarded @@ -539,12 +540,13 @@ func (c *Conn) rxDataRowValueTo(w io.Writer, bodySize int32) (err error) { return } - var valueSize int32 - err = binary.Read(c.reader, binary.BigEndian, &valueSize) + b = make([]byte, 4) + _, err = io.ReadFull(c.reader, b) if err != nil { c.die(err) return } + valueSize := int32(binary.BigEndian.Uint32(b)) if valueSize == -1 { err = errors.New("SelectValueTo cannot handle null") @@ -972,12 +974,15 @@ func (c *Conn) rxMsgHeader() (t byte, bodySize int32, err error) { return 0, 0, err } - err = binary.Read(c.reader, binary.BigEndian, &bodySize) + b := make([]byte, 4) + _, err = io.ReadFull(c.reader, b) if err != nil { c.die(err) return 0, 0, err } + bodySize = int32(binary.BigEndian.Uint32(b)) + bodySize -= 4 // remove self from size return t, bodySize, err }