Skip to content

Commit

Permalink
DecodeMessage: skip bytes Buffer, reducing alloc overhead
Browse files Browse the repository at this point in the history
this is the nsqd equivalent of a very similar change in go-nsq:
nsqio/go-nsq#175
  • Loading branch information
Dieterbe committed Apr 15, 2016
1 parent 1746cc6 commit 0c28816
Showing 1 changed file with 12 additions and 13 deletions.
25 changes: 12 additions & 13 deletions nsqd/message.go
Expand Up @@ -5,7 +5,6 @@ import (
"encoding/binary"
"fmt"
"io"
"io/ioutil"
"time"
)

Expand Down Expand Up @@ -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

Expand All @@ -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
}
Expand Down

0 comments on commit 0c28816

Please sign in to comment.