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

Reduce allocations in WebSockets #4623

Merged
merged 3 commits into from Oct 4, 2023
Merged

Reduce allocations in WebSockets #4623

merged 3 commits into from Oct 4, 2023

Conversation

neilalexander
Copy link
Member

@neilalexander neilalexander commented Oct 4, 2023

This PR primarily does two things:

  1. Updates the buffer pool to allow reclaiming buffers that have been grown beyond their original capacity by slicing them up into capacities we can make use of (reverted for now);
  2. Updates the WebSocket code to remove extra copies, instead drawing straight from the compression buffer, which is reset on each call.

With nats bench ... --pub=1 --sub=3 --msgs=100000 --size=1024 over non-TLS WebSockets, we have gone from:

  • 174,688 msgs/sec ~ 170.59 MB/sec up to 834,240 msgs/sec ~ 814.69 MB/sec
  • 40GB of wsCollapsePtoNB allocations down to 10MB
  • 13GB of flate.NewWriter allocations down to 3.63MB

Signed-off-by: Neil Twigg neil@nats.io

Signed-off-by: Neil Twigg <neil@nats.io>
@neilalexander neilalexander requested a review from a team as a code owner October 4, 2023 12:01
@neilalexander neilalexander changed the title Reduce allocations in WebSocket compression Reduce allocations in WebSockets Oct 4, 2023
server/client.go Outdated Show resolved Hide resolved
server/websocket.go Outdated Show resolved Hide resolved
@derekcollison derekcollison self-requested a review October 4, 2023 16:45
Copy link
Member

@derekcollison derekcollison left a comment

Choose a reason for hiding this comment

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

LGTM

@neilalexander neilalexander merged commit 4c791d6 into main Oct 4, 2023
2 checks passed
@neilalexander neilalexander deleted the neil/ws branch October 4, 2023 19:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants