mirror of
https://github.com/FrankerFaceZ/FrankerFaceZ.git
synced 2025-08-07 06:40:54 +00:00
Optimize MarshalClientMessage to avoid copies
This commit is contained in:
parent
e0bad1dc10
commit
edb728f7bb
1 changed files with 12 additions and 7 deletions
|
@ -1,6 +1,7 @@
|
||||||
package server // import "github.com/FrankerFaceZ/FrankerFaceZ/socketserver/server"
|
package server // import "github.com/FrankerFaceZ/FrankerFaceZ/socketserver/server"
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
@ -619,7 +620,6 @@ func MarshalClientMessage(clientMessage interface{}) (int, []byte, error) {
|
||||||
}
|
}
|
||||||
msg = *pMsg
|
msg = *pMsg
|
||||||
}
|
}
|
||||||
var dataStr string
|
|
||||||
|
|
||||||
if msg.Command == "" && msg.MessageID == 0 {
|
if msg.Command == "" && msg.MessageID == 0 {
|
||||||
panic("MarshalClientMessage: attempt to send an empty ClientMessage")
|
panic("MarshalClientMessage: attempt to send an empty ClientMessage")
|
||||||
|
@ -632,20 +632,25 @@ func MarshalClientMessage(clientMessage interface{}) (int, []byte, error) {
|
||||||
msg.MessageID = -1
|
msg.MessageID = -1
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// optimized from fmt.Sprintf("%d %s %s", msg.MessageID, msg.Command, ...)
|
||||||
|
var buf bytes.Buffer
|
||||||
|
fmt.Fprint(&buf, msg.MessageID)
|
||||||
|
buf.WriteByte(' ')
|
||||||
|
buf.WriteString(string(msg.Command))
|
||||||
|
|
||||||
if msg.origArguments != "" {
|
if msg.origArguments != "" {
|
||||||
dataStr = fmt.Sprintf("%d %s %s", msg.MessageID, msg.Command, msg.origArguments)
|
buf.WriteByte(' ')
|
||||||
|
buf.WriteString(msg.origArguments)
|
||||||
} else if msg.Arguments != nil {
|
} else if msg.Arguments != nil {
|
||||||
argBytes, err := json.Marshal(msg.Arguments)
|
argBytes, err := json.Marshal(msg.Arguments)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, nil, err
|
return 0, nil, err
|
||||||
}
|
}
|
||||||
|
buf.WriteByte(' ')
|
||||||
dataStr = fmt.Sprintf("%d %s %s", msg.MessageID, msg.Command, string(argBytes))
|
buf.Write(argBytes)
|
||||||
} else {
|
|
||||||
dataStr = fmt.Sprintf("%d %s", msg.MessageID, msg.Command)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return websocket.TextMessage, []byte(dataStr), nil
|
return websocket.TextMessage, buf.Bytes(), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// ArgumentsAsString parses the arguments of the ClientMessage as a single string.
|
// ArgumentsAsString parses the arguments of the ClientMessage as a single string.
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue