Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MM-54998: Optimize JSON marshalling in websocket broadcast #25286

Merged
merged 1 commit into from
Nov 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions server/channels/app/platform/web_conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,7 @@ func (wc *WebConn) writePump() {
var err error
if evtOk {
evt = evt.SetSequence(wc.Sequence)
err = evt.Encode(enc)
err = evt.Encode(enc, &buf)
wc.Sequence++
} else {
err = enc.Encode(msg)
Expand Down Expand Up @@ -530,7 +530,7 @@ func (wc *WebConn) writeMessage(msg *model.WebSocketEvent) error {
// We don't use the encoder from the write pump because it's unwieldy to pass encoders
// around, and this is only called during initialization of the webConn.
var buf bytes.Buffer
err := msg.Encode(json.NewEncoder(&buf))
err := msg.Encode(json.NewEncoder(&buf), &buf)
if err != nil {
mlog.Warn("Error in encoding websocket message", mlog.Err(err))
return nil
Expand Down
5 changes: 3 additions & 2 deletions server/public/model/websocket_message.go
Original file line number Diff line number Diff line change
Expand Up @@ -294,9 +294,10 @@ func (ev *WebSocketEvent) ToJSON() ([]byte, error) {
}

// Encode encodes the event to the given encoder.
func (ev *WebSocketEvent) Encode(enc *json.Encoder) error {
func (ev *WebSocketEvent) Encode(enc *json.Encoder, buf io.Writer) error {
if ev.precomputedJSON != nil {
return enc.Encode(json.RawMessage(ev.precomputedJSONBuf()))
_, err := buf.Write(ev.precomputedJSONBuf())
return err
}

return enc.Encode(webSocketEventJSON{
Expand Down
5 changes: 4 additions & 1 deletion server/public/model/websocket_message_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,8 +247,11 @@ func BenchmarkEncodeJSON(b *testing.B) {

ev := message.PrecomputeJSON()

var seq int64
enc := json.NewEncoder(io.Discard)
for i := 0; i < b.N; i++ {
err = ev.Encode(enc)
ev = ev.SetSequence(seq)
err = ev.Encode(enc, io.Discard)
seq++
}
Comment on lines +250 to 256
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need this here now? And why don't we use int64(i) directly?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It just makes things a bit more realistic as that's what happens here:

evt = evt.SetSequence(wc.Sequence)
err = evt.Encode(enc)
wc.Sequence++

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, good catch then :) Thanks for the clarification

}