Skip to content

Reduce allocations in WebSocketServerHandshaker13 - #16505

Merged
normanmaurer merged 1 commit into
netty:4.2from
doom369:avoid-allocation-in-websockets
Mar 18, 2026
Merged

normanmaurer merged 1 commit into
netty:4.2from
doom369:avoid-allocation-in-websockets

Conversation

@doom369

@doom369 doom369 commented Mar 17, 2026 •

Copy link
Copy Markdown
Contributor

Motivation:

image

Currently, WebSocketServerHandshaker13 does multiple different allocations in newHandshakeResponse() method, some of which could be avoided.

Modification:

Replaced the concat operation with multiple digest.update() calls.

Result:

No more String allocation on the concat operation. Also, as a bonus key.getBytes() now allocates ~2 times smaller byte array than a concatenated string would.

Benchmark                                                                              (key)  Mode  Cnt     Score     Error   Units
WebSocketServerHandshakeBenchmark.original                          dGhlIHNhbXBsZSBub25jZQ==  avgt   16   200.463 ±  66.739   ns/op
WebSocketServerHandshakeBenchmark.original:gc.alloc.rate            dGhlIHNhbXBsZSBub25jZQ==  avgt   16  1686.895 ± 565.204  MB/sec
WebSocketServerHandshakeBenchmark.original:gc.alloc.rate.norm       dGhlIHNhbXBsZSBub25jZQ==  avgt   16   320.001 ±   0.001    B/op
WebSocketServerHandshakeBenchmark.new                     dGhlIHNhbXBsZSBub25jZQ==  avgt   16   164.732 ±  43.825   ns/op
WebSocketServerHandshakeBenchmark.new:gc.alloc.rate       dGhlIHNhbXBsZSBub25jZQ==  avgt   16  1220.335 ± 269.482  MB/sec
WebSocketServerHandshakeBenchmark.new:gc.alloc.rate.norm  dGhlIHNhbXBsZSBub25jZQ==  avgt   16   200.001 ±   0.001    B/op

@normanmaurer

Copy link
Copy Markdown
Member

@doom369 nice one!

@normanmaurer normanmaurer added this to the 4.2.11.Final milestone Mar 17, 2026
@normanmaurer normanmaurer added the needs-cherry-pick-5.0 This PR should be cherry-picked to 5.0 once merged. label Mar 18, 2026
@normanmaurer
normanmaurer merged commit 4eb5ca3 into netty:4.2 Mar 18, 2026
34 of 35 checks passed
netty-project-bot pushed a commit that referenced this pull request Mar 18, 2026
Motivation:

<img width="695" height="207" alt="image"
src="https://github.com/user-attachments/assets/30a86363-3805-4414-a40b-ed3cfbea39cc"
/>

Currently, `WebSocketServerHandshaker13` does multiple different
allocations in `newHandshakeResponse()` method, some of which could be
avoided.

Modification:

Replaced the concat operation with multiple `digest.update()` calls.

Result:

No more String allocation on the concat operation. Also, as a bonus
key.getBytes() now allocates ~2 times smaller byte array than a
concatenated string would.

```
Benchmark                                                                              (key)  Mode  Cnt     Score     Error   Units
WebSocketServerHandshakeBenchmark.original                          dGhlIHNhbXBsZSBub25jZQ==  avgt   16   200.463 ±  66.739   ns/op
WebSocketServerHandshakeBenchmark.original:gc.alloc.rate            dGhlIHNhbXBsZSBub25jZQ==  avgt   16  1686.895 ± 565.204  MB/sec
WebSocketServerHandshakeBenchmark.original:gc.alloc.rate.norm       dGhlIHNhbXBsZSBub25jZQ==  avgt   16   320.001 ±   0.001    B/op
WebSocketServerHandshakeBenchmark.new                     dGhlIHNhbXBsZSBub25jZQ==  avgt   16   164.732 ±  43.825   ns/op
WebSocketServerHandshakeBenchmark.new:gc.alloc.rate       dGhlIHNhbXBsZSBub25jZQ==  avgt   16  1220.335 ± 269.482  MB/sec
WebSocketServerHandshakeBenchmark.new:gc.alloc.rate.norm  dGhlIHNhbXBsZSBub25jZQ==  avgt   16   200.001 ±   0.001    B/op
```

(cherry picked from commit 4eb5ca3)
@netty-project-bot

Copy link
Copy Markdown
Contributor

Auto-port PR for 5.0: #16519

@github-actions github-actions Bot removed the needs-cherry-pick-5.0 This PR should be cherry-picked to 5.0 once merged. label Mar 18, 2026
normanmaurer pushed a commit that referenced this pull request Mar 18, 2026
)

Auto-port of #16505 to 5.0
Cherry-picked commit: 4eb5ca3

---
Motivation:

<img width="695" height="207" alt="image"
src="https://github.com/user-attachments/assets/30a86363-3805-4414-a40b-ed3cfbea39cc"
/>


Currently, `WebSocketServerHandshaker13` does multiple different
allocations in `newHandshakeResponse()` method, some of which could be
avoided.

Modification:

Replaced the concat operation with multiple `digest.update()` calls.

Result:

No more String allocation on the concat operation. Also, as a bonus
key.getBytes() now allocates ~2 times smaller byte array than a
concatenated string would.


```
Benchmark                                                                              (key)  Mode  Cnt     Score     Error   Units
WebSocketServerHandshakeBenchmark.original                          dGhlIHNhbXBsZSBub25jZQ==  avgt   16   200.463 ±  66.739   ns/op
WebSocketServerHandshakeBenchmark.original:gc.alloc.rate            dGhlIHNhbXBsZSBub25jZQ==  avgt   16  1686.895 ± 565.204  MB/sec
WebSocketServerHandshakeBenchmark.original:gc.alloc.rate.norm       dGhlIHNhbXBsZSBub25jZQ==  avgt   16   320.001 ±   0.001    B/op
WebSocketServerHandshakeBenchmark.new                     dGhlIHNhbXBsZSBub25jZQ==  avgt   16   164.732 ±  43.825   ns/op
WebSocketServerHandshakeBenchmark.new:gc.alloc.rate       dGhlIHNhbXBsZSBub25jZQ==  avgt   16  1220.335 ± 269.482  MB/sec
WebSocketServerHandshakeBenchmark.new:gc.alloc.rate.norm  dGhlIHNhbXBsZSBub25jZQ==  avgt   16   200.001 ±   0.001    B/op
```

Co-authored-by: Dmytro Dumanskiy <[email protected]>
@normanmaurer normanmaurer mentioned this pull request Oct 1, 2026
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.

3 participants