Skip to content

Coalesce pending HPACK table size updates - #1438

Merged
He-Pin merged 1 commit into
apache:mainfrom
He-Pin:optimize/hpack-pending-table-updates
Nov 17, 2026
Merged

He-Pin merged 1 commit into
apache:mainfrom
He-Pin:optimize/hpack-pending-table-updates

Conversation

@He-Pin

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

Copy link
Copy Markdown
Member

Motivation

HeaderCompression currently writes every received header-table size change into its output buffer, even when no header block is being sent. Only the smallest intervening size and the final size need to be emitted before the next header block (RFC 7541 section 4.2).

For example, ordinary size changes 4096 → 2048 → 1024 → 2048 currently buffer three updates. Retaining the minimum and final sizes avoids redundant update bytes and keeps this pending metadata constant-sized.

Modification

  • Record the minimum and final normalized table sizes in the compression stage instead of immediately appending update bytes.
  • Apply the minimum before the final size immediately before encoding the next actual HEADERS block, then clear the pending state.
  • Preserve every SETTINGS acknowledgement and retain pending updates when empty terminal headers are represented as DATA.
  • Add six focused stage tests covering acknowledgements, multiple settings in one acknowledgement, duplicate sizes, minimum/final updates, state reset, shrink/regrow eviction, and both empty-header paths.

The existing public Encoder.setMaxHeaderTableSize contract is unchanged. An explicit tradeoff is that an idle connection retains its existing dynamic table until the next actual header block (or connection teardown), instead of evicting immediately on each setting. Applying the minimum before encoding preserves the peer's required eviction state. This change reduces pending update bytes; it does not claim to reduce dynamic-table memory or fix a demonstrated security vulnerability.

Result

At most the minimum and final table-size changes are encoded between header blocks. Header decoding semantics and acknowledgement ordering are preserved.

Tests

  • Unmodified production code: the new stage suite ran 6 tests, with 4 failing on redundant update bytes and 2 passing.
  • Updated code: HeaderCompressionSpec, HpackEncoderSpec, and HpackDecoderSpec: 25 tests passed.
  • Targeted OrganizeImports and Scalafmt: passed.
  • http-core / Compile / headerCheck and http-core / Test / headerCheck: passed.
  • http-core / mimaReportBinaryIssues: passed.
  • git diff --check: passed.

References

  • RFC 7541 section 4.2.
  • Netty's pending table-size update optimization motivated the investigation. The stage implementation and tests were independently written for Pekko; no Netty source was copied.
  • The new test file received the standard ASF header through headerCreateAll; the production file's existing notices are unchanged.

Motivation:
Avoid accumulating redundant table size update bytes between header blocks.

Modification:
Record the minimum and final sizes in HeaderCompression and apply both before the next actual HEADERS block. Preserve ACK ordering and the existing Encoder API. Add six focused stage tests.

Result:
Constant-sized pending size metadata. Dynamic table eviction is deferred until the next header block; the intervening minimum still preserves peer state.

Tests:
Baseline: 4 of 6 new tests fail. Fixed: 25 tests pass across three suites. OrganizeImports, Scalafmt, header checks, http-core MiMa, and git diff --check pass.

References:
RFC 7541 section 4.2. Investigation inspired by Netty commit 9f496873a1132a89d55911a9af84615e97c01eac; implementation and tests independently written.
@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, this looks correct. Previously the update bytes waited in os until the next header block anyway, so deferring the setMaxHeaderTableSize calls doesn't change what the peer sees. It only changes when our encoder evicts locally, and nothing references the table in between. Emitting the minimum before the final size keeps RFC 7541 §4.2 behaviour, and the shrink/regrow test shows the evicted entry isn't brought back. A useful side effect: a run of SETTINGS frames with no header blocks in between no longer grows os.

Minor points on the new spec:

  • Pekko code usually imports the root package once and uses it as a prefix (import org.apache.pekko, then import pekko.http.impl.engine.http2.FrameEvent etc.), rather than spelling out org.apache.pekko on each import.
  • compress uses Await.result(..., 3.seconds). Using 3.seconds.dilated (or futureValue) would follow the time-factor setting like the rest of the test suite.

@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 84ae182 into apache:main Nov 17, 2026
6 checks passed
@He-Pin
He-Pin deleted the optimize/hpack-pending-table-updates branch November 17, 2026 14:29
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