Skip to content

Commit

Permalink
Merge pull request #788 from Endeavourken/master
Browse files Browse the repository at this point in the history
[CHANGED] avoid unnecessary data copy in processMsg
  • Loading branch information
kozlovic committed Aug 9, 2021
2 parents 91bdffe + 9b7c017 commit 6856f4a
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 12 deletions.
7 changes: 5 additions & 2 deletions nats.go
Expand Up @@ -2629,8 +2629,11 @@ func (nc *Conn) processMsg(data []byte) {
// It's possible that we end-up not using the message, but that's ok.

// FIXME(dlc): Need to copy, should/can do COW?
msgPayload := make([]byte, len(data))
copy(msgPayload, data)
var msgPayload = data
if !nc.ps.msgCopied {
msgPayload = make([]byte, len(data))
copy(msgPayload, data)
}

// Check if we have headers encoded here.
var h Header
Expand Down
22 changes: 12 additions & 10 deletions parser.go
Expand Up @@ -28,14 +28,15 @@ type msgArg struct {
const MAX_CONTROL_LINE_SIZE = 4096

type parseState struct {
state int
as int
drop int
hdr int
ma msgArg
argBuf []byte
msgBuf []byte
scratch [MAX_CONTROL_LINE_SIZE]byte
state int
as int
drop int
hdr int
ma msgArg
argBuf []byte
msgBuf []byte
msgCopied bool
scratch [MAX_CONTROL_LINE_SIZE]byte
}

const (
Expand Down Expand Up @@ -167,7 +168,7 @@ func (nc *Conn) parse(buf []byte) error {
if nc.ps.msgBuf != nil {
if len(nc.ps.msgBuf) >= nc.ps.ma.size {
nc.processMsg(nc.ps.msgBuf)
nc.ps.argBuf, nc.ps.msgBuf, nc.ps.state = nil, nil, MSG_END
nc.ps.argBuf, nc.ps.msgBuf, nc.ps.msgCopied, nc.ps.state = nil, nil, false, MSG_END
} else {
// copy as much as we can to the buffer and skip ahead.
toCopy := nc.ps.ma.size - len(nc.ps.msgBuf)
Expand All @@ -190,7 +191,7 @@ func (nc *Conn) parse(buf []byte) error {
}
} else if i-nc.ps.as >= nc.ps.ma.size {
nc.processMsg(buf[nc.ps.as:i])
nc.ps.argBuf, nc.ps.msgBuf, nc.ps.state = nil, nil, MSG_END
nc.ps.argBuf, nc.ps.msgBuf, nc.ps.msgCopied, nc.ps.state = nil, nil, false, MSG_END
}
case MSG_END:
switch b {
Expand Down Expand Up @@ -403,6 +404,7 @@ func (nc *Conn) parse(buf []byte) error {

nc.ps.msgBuf = make([]byte, lrem, nc.ps.ma.size)
copy(nc.ps.msgBuf, buf[nc.ps.as:])
nc.ps.msgCopied = true
} else {
nc.ps.msgBuf = nc.ps.scratch[len(nc.ps.argBuf):len(nc.ps.argBuf)]
nc.ps.msgBuf = append(nc.ps.msgBuf, (buf[nc.ps.as:])...)
Expand Down

0 comments on commit 6856f4a

Please sign in to comment.