mirror of
https://github.com/gogrlx/nats-server.git
synced 2026-04-02 03:38:42 -07:00
Optimize to not allocate converting strings to []byte
Signed-off-by: Derek Collison <derek@nats.io>
This commit is contained in:
@@ -51,9 +51,12 @@ func (sq *sendq) internalLoop() {
|
|||||||
defer c.closeConnection(ClientClosed)
|
defer c.closeConnection(ClientClosed)
|
||||||
|
|
||||||
// To optimize for not converting a string to a []byte slice.
|
// To optimize for not converting a string to a []byte slice.
|
||||||
var subj [256]byte
|
var (
|
||||||
var rply [256]byte
|
subj [256]byte
|
||||||
var szb [10]byte
|
rply [256]byte
|
||||||
|
szb [10]byte
|
||||||
|
hdb [10]byte
|
||||||
|
)
|
||||||
|
|
||||||
for s.isRunning() {
|
for s.isRunning() {
|
||||||
select {
|
select {
|
||||||
@@ -73,7 +76,7 @@ func (sq *sendq) internalLoop() {
|
|||||||
var msg []byte
|
var msg []byte
|
||||||
if len(pm.hdr) > 0 {
|
if len(pm.hdr) > 0 {
|
||||||
c.pa.hdr = len(pm.hdr)
|
c.pa.hdr = len(pm.hdr)
|
||||||
c.pa.hdb = []byte(strconv.Itoa(c.pa.hdr))
|
c.pa.hdb = append(hdb[:0], strconv.Itoa(c.pa.hdr)...)
|
||||||
msg = append(pm.hdr, pm.msg...)
|
msg = append(pm.hdr, pm.msg...)
|
||||||
msg = append(msg, _CRLF_...)
|
msg = append(msg, _CRLF_...)
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@@ -4353,16 +4353,29 @@ func (mset *stream) internalLoop() {
|
|||||||
// This should be rarely used now so can be smaller.
|
// This should be rarely used now so can be smaller.
|
||||||
var _r [1024]byte
|
var _r [1024]byte
|
||||||
|
|
||||||
|
// To optimize for not converting a string to a []byte slice.
|
||||||
|
var (
|
||||||
|
subj [256]byte
|
||||||
|
dsubj [256]byte
|
||||||
|
rply [256]byte
|
||||||
|
szb [10]byte
|
||||||
|
hdb [10]byte
|
||||||
|
)
|
||||||
|
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case <-outq.ch:
|
case <-outq.ch:
|
||||||
pms := outq.pop()
|
pms := outq.pop()
|
||||||
for _, pm := range pms {
|
for _, pm := range pms {
|
||||||
c.pa.subject = []byte(pm.dsubj)
|
c.pa.subject = append(dsubj[:0], pm.dsubj...)
|
||||||
c.pa.deliver = []byte(pm.subj)
|
c.pa.deliver = append(subj[:0], pm.subj...)
|
||||||
c.pa.size = len(pm.msg) + len(pm.hdr)
|
c.pa.size = len(pm.msg) + len(pm.hdr)
|
||||||
c.pa.szb = []byte(strconv.Itoa(c.pa.size))
|
c.pa.szb = append(szb[:0], strconv.Itoa(c.pa.size)...)
|
||||||
c.pa.reply = []byte(pm.reply)
|
if len(pm.reply) > 0 {
|
||||||
|
c.pa.reply = append(rply[:0], pm.reply...)
|
||||||
|
} else {
|
||||||
|
c.pa.reply = nil
|
||||||
|
}
|
||||||
|
|
||||||
// If we have an underlying buf that is the wire contents for hdr + msg, else construct on the fly.
|
// If we have an underlying buf that is the wire contents for hdr + msg, else construct on the fly.
|
||||||
var msg []byte
|
var msg []byte
|
||||||
@@ -4385,6 +4398,7 @@ func (mset *stream) internalLoop() {
|
|||||||
if len(pm.hdr) > 0 {
|
if len(pm.hdr) > 0 {
|
||||||
c.pa.hdr = len(pm.hdr)
|
c.pa.hdr = len(pm.hdr)
|
||||||
c.pa.hdb = []byte(strconv.Itoa(c.pa.hdr))
|
c.pa.hdb = []byte(strconv.Itoa(c.pa.hdr))
|
||||||
|
c.pa.hdb = append(hdb[:0], strconv.Itoa(c.pa.hdr)...)
|
||||||
} else {
|
} else {
|
||||||
c.pa.hdr = -1
|
c.pa.hdr = -1
|
||||||
c.pa.hdb = nil
|
c.pa.hdb = nil
|
||||||
|
|||||||
Reference in New Issue
Block a user