Replace binary.Read

It does memory allocs that can be avoided by passing a slice directly to
binary.BigEndian.*
scan-io
Jack Christensen 2014-06-27 08:20:15 -05:00
parent e8f367ede4
commit aeb6cea1cf
1 changed files with 10 additions and 5 deletions

15
conn.go
View File

@ -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) { func (c *Conn) rxDataRowValueTo(w io.Writer, bodySize int32) (err error) {
var columnCount int16 b := make([]byte, 2)
err = binary.Read(c.reader, binary.BigEndian, &columnCount) _, err = io.ReadFull(c.reader, b)
if err != nil { if err != nil {
c.die(err) c.die(err)
return return
} }
columnCount := int16(binary.BigEndian.Uint16(b))
if columnCount != 1 { if columnCount != 1 {
// Read the rest of the data row so it can be discarded // 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 return
} }
var valueSize int32 b = make([]byte, 4)
err = binary.Read(c.reader, binary.BigEndian, &valueSize) _, err = io.ReadFull(c.reader, b)
if err != nil { if err != nil {
c.die(err) c.die(err)
return return
} }
valueSize := int32(binary.BigEndian.Uint32(b))
if valueSize == -1 { if valueSize == -1 {
err = errors.New("SelectValueTo cannot handle null") 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 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 { if err != nil {
c.die(err) c.die(err)
return 0, 0, err return 0, 0, err
} }
bodySize = int32(binary.BigEndian.Uint32(b))
bodySize -= 4 // remove self from size bodySize -= 4 // remove self from size
return t, bodySize, err return t, bodySize, err
} }