Repository navigation
Reduce temporary allocations in WebSocket accept hashing - #1439
Merged
Merged
Conversation
pjfanning
reviewed
Nov 17, 2026
pjfanning
left a comment
Member
There was a problem hiding this comment.
Thanks. The change in forKey is correct, and feeding the key and the GUID to SHA-1 separately gives the same digest as hashing the concatenation. A few points:
- CI failure on Scala next.
Compile and test (next, 17)fails while compilinghttp-bench-jmh:var keyText: String = _andprivate var key: ... = _inWebSocketAcceptBenchmarkhit the= _ has been deprecatedwarning, and-Werrorturns it into an error. An explicit initial value (= ""/= null) fixes it. - Cost vs benefit.
forKeyruns once per WebSocket handshake, and the saving is about 150 B per call. The new benchmark and the 100-key equivalence test add more code to maintain than the change saves. I'd keep the RFC 6455 known-answer test, which pins the result, and drop the benchmark and the generated-key comparison. String.valueOf(key.key). This keeps the old behaviour forSec-WebSocket-Key(null), but a null key isn't a meaningful input (a parsed or generated key is never null).key.key.asciiByteswould be simpler to read, and the null case could go from the test too.
Motivation: WebSocket accept hashing need not allocate a concatenated key and GUID string for each handshake. Modification: Feed the key bytes and precomputed GUID bytes separately to SHA-1. Retain the RFC 6455 known-answer test, and remove the maintenance-heavy benchmark and generated-key equivalence test following review. Keep String.valueOf so this follow-up does not introduce an additional runtime behavior change. Result: The original accept hash result is preserved with fewer temporary allocations and focused regression coverage. Tests: - git diff --check: passed. - HeaderSpec and scalafmt: not run; a working full JDK and formatter are unavailable in this environment. No build success claimed. References: Refs apache#1439 and PJ Fanning review 5938888815. RFC 6455 section 1.3.
He-Pin
force-pushed
the
perf/ws-accept-hash-allocations
branch
from
November 17, 2026 15:32
ba75b78 to
07caebc
Compare
pjfanning
added a commit
that referenced
this pull request
Nov 19, 2026
Motivation: Several recently merged PRs left review points unaddressed: docs that were asked to be dropped, comments and tests that were suggested, and small API and internal tidy-ups. Modification: - Add a Scala FilenameEncoding alias and values to Multipart.FormData and use them in the Scala toEntity signature and tests (#1494). - Render the form-data Content-Disposition header directly instead of rendering it twice and slicing off the prefix (#1494). - Drop the docs paragraph about unpaired URI surrogates (#1426) and the cookie -0000 sentence in request-level.md (#1493). - Fix the javadsl Multipart.FormData doc the same way #1483 fixed the scaladsl one. - Comment the cookie date rule copies so they stay in sync (#1458). - Test custom methods starting with Q next to the QUERY fast path (#1477). - Use one root package import and a dilated timeout in HeaderCompressionSpec (#1438). - Pass isTrailer to HttpMessageRendering.renderHeaders explicitly (#1463). - Remove the unused forwarding overloads in Handshake.Client and EnabledPingState (#1436, #1462). - Move the media type model checks from the routing spec to a new http-core MediaTypesSpec (#1437, #1454). - Hash the WebSocket key without String.valueOf (#1439). Result: The review points are addressed. The only public API change is the added Scala FilenameEncoding alias. Tests: - http-core/testOnly MultipartSpec MultipartsSpec MediaTypesSpec RequestParserCRLFSpec RequestParserLFSpec HeaderCompressionSpec WebSocketClientSpec HeaderSpec HttpHeaderSpec: 314 passed - http2-tests/testOnly HttpMessageRenderingSpec Http2PingSchedulingSpec: 38 passed - http-tests/testOnly FileAndResourceDirectivesSpec: 63 passed - http-core/mimaReportBinaryIssues: passed References: Refs #1426, #1436, #1437, #1438, #1439, #1454, #1458, #1462, #1463, #1477, #1483, #1493, #1494
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Reduce temporary allocations when computing
Sec-WebSocket-Accept:MessageDigestper invocation and the existingasciiBytesconversion;String.valueOf.Retain the RFC 6455 known-answer test. Remove the generated-key equivalence test and JMH benchmark source as requested in review; preserve compatible null-key behavior with String.valueOf.
Provenance
The allocation-reduction idea was prompted by Netty #16505. This implementation uses Pekko's existing helpers and ownership model; it does not adopt Netty's thread-local digest reuse. Existing source headers are retained. The independently written benchmark in the earlier revision carried an ASF header generated by
headerCreateAll; that benchmark source has now been removed.Validation
The following tests and benchmark measurements belong to the earlier revision. The latest trimmed revision was source-reviewed but not rerun; there is no new JMH run.
http-core/testOnly *HeaderSpec *WebSocketClientSpec *WebSocketServerSpec: 164 passed, 7 existing pending.headerCheckAll, Scala formatting, andgit diff --check: passed.-prof gc.This is a per-call allocation improvement, not a claim of improved end-to-end WebSocket throughput. Timing measurements come from a shared environment and must not be treated as a stable latency guarantee.
Historical old-implementation vs production measurements (
gc.alloc.rate.norm):dGhlIHNhbXBsZSBub25jZQ==YLQguzhR2dR6y5M9vnA5mw==This saves approximately 144–148 bytes per call in this run. Timing was noisy: baseline 255.883 ± 11.796 / 271.603 ± 26.017 ns/op; changed implementation 367.234 ± 547.823 / 304.853 ± 110.677 ns/op. These measurements do not establish a latency or throughput improvement.
Historical reproduction command for the earlier revision (the benchmark source is no longer present in this PR):
A follow-up timing-only run without the GC profiler, with longer warmup/measurement (
-wi 5 -i 5 -w 1s -r 2s -f 2 -t 1), remained inconclusive:The directions differ between keys and the intervals overlap. Allocation reduction is established in these runs; the latency tradeoff remains unresolved.