From 0c28816d1a77f30fd9855751a04a332b00c8486b Mon Sep 17 00:00:00 2001 From: Dieter Plaetinck Date: Fri, 15 Apr 2016 16:41:56 -0400 Subject: [PATCH] DecodeMessage: skip bytes Buffer, reducing alloc overhead this is the nsqd equivalent of a very similar change in go-nsq: https://github.com/nsqio/go-nsq/pull/175 --- nsqd/message.go | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/nsqd/message.go b/nsqd/message.go index 7dda4e36d..77ee4c79d 100644 --- a/nsqd/message.go +++ b/nsqd/message.go @@ -5,7 +5,6 @@ import ( "encoding/binary" "fmt" "io" - "io/ioutil" "time" ) @@ -66,6 +65,16 @@ func (m *Message) WriteTo(w io.Writer) (int64, error) { return total, nil } +// decodeMessage deserializes data (as []byte) and creates a new Message +// message format: +// [x][x][x][x][x][x][x][x][x][x][x][x][x][x][x][x][x][x][x][x][x][x][x][x][x][x][x][x][x][x]... +// | (int64) || || (hex string encoded in ASCII) || (binary) +// | 8-byte || || 16-byte || N-byte +// ------------------------------------------------------------------------------------------... +// nanosecond timestamp ^^ message ID message body +// (uint16) +// 2-byte +// attempts func decodeMessage(b []byte) (*Message, error) { var msg Message @@ -75,18 +84,8 @@ func decodeMessage(b []byte) (*Message, error) { msg.Timestamp = int64(binary.BigEndian.Uint64(b[:8])) msg.Attempts = binary.BigEndian.Uint16(b[8:10]) - - buf := bytes.NewBuffer(b[10:]) - - _, err := io.ReadFull(buf, msg.ID[:]) - if err != nil { - return nil, err - } - - msg.Body, err = ioutil.ReadAll(buf) - if err != nil { - return nil, err - } + copy(msg.ID[:], b[10:10+MsgIDLength]) + msg.Body = b[10+MsgIDLength:] return &msg, nil }