Skip to content

Reduce temporary allocations in WebSocket accept hashing - #1439

Merged
He-Pin merged 1 commit into
apache:mainfrom
He-Pin:perf/ws-accept-hash-allocations
Nov 17, 2026
Merged

He-Pin merged 1 commit into
apache:mainfrom
He-Pin:perf/ws-accept-hash-allocations

Conversation

@He-Pin

@He-Pin He-Pin commented Nov 13, 2026 •

Copy link
Copy Markdown
Member

Summary

Reduce temporary allocations when computing Sec-WebSocket-Accept:

  • cache the constant GUID bytes privately;
  • feed the key bytes and GUID bytes separately into SHA-1 instead of allocating their concatenated String and byte array;
  • retain a new MessageDigest per invocation and the existing asciiBytes conversion;
  • preserve directly constructed null-key behavior with 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.

  • Independent adversarial source/test/benchmark review completed before publication.
  • http-core/testOnly *HeaderSpec *WebSocketClientSpec *WebSocketServerSpec: 164 passed, 7 existing pending.
  • headerCheckAll, Scala formatting, and git diff --check: passed.
  • JMH on JDK 17: two key parameters, two forks, one thread, 3 x 1-second warmup and 5 x 1-second measurement iterations, -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):

Key Previous implementation Production change
dGhlIHNhbXBsZSBub25jZQ== 640.002 ± 0.001 B/op 492.002 ± 6.376 B/op
YLQguzhR2dR6y5M9vnA5mw== 640.002 ± 0.001 B/op 496.002 ± 0.001 B/op

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):

http-bench-jmh/Jmh/run -wi 3 -i 5 -w 1s -r 1s -f 2 -t 1 -prof gc .*WebSocketAcceptBenchmark.*

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:

Key Previous implementation Production change
RFC example 289.352 ± 29.332 ns/op 323.458 ± 124.919 ns/op
Second key 305.929 ± 74.529 ns/op 291.205 ± 81.760 ns/op

The directions differ between keys and the intervals overlap. Allocation reduction is established in these runs; the latency tradeoff remains unresolved.

@pjfanning pjfanning added this to the 2.1.0 milestone Nov 13, 2026

@pjfanning pjfanning left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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:

  1. CI failure on Scala next. Compile and test (next, 17) fails while compiling http-bench-jmh: var keyText: String = _ and private var key: ... = _ in WebSocketAcceptBenchmark hit the = _ has been deprecated warning, and -Werror turns it into an error. An explicit initial value (= "" / = null) fixes it.
  2. Cost vs benefit. forKey runs 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.
  3. String.valueOf(key.key). This keeps the old behaviour for Sec-WebSocket-Key(null), but a null key isn't a meaningful input (a parsed or generated key is never null). key.key.asciiBytes would 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
He-Pin force-pushed the perf/ws-accept-hash-allocations branch from ba75b78 to 07caebc Compare November 17, 2026 15:32
@He-Pin
He-Pin requested a review from pjfanning November 17, 2026 15:33

@pjfanning pjfanning left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

lgtm

@He-Pin
He-Pin merged commit bb23808 into apache:main Nov 17, 2026
6 checks passed
@He-Pin
He-Pin deleted the perf/ws-accept-hash-allocations branch November 17, 2026 20:06
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
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.

2 participants