Skip to content

perf: reduce HTTP/2 pipeline allocations and response handoffs - #1081

Open
He-Pin wants to merge 1 commit into
apache:mainfrom
He-Pin:optimize/h2-eliminate-double-dispatch
Open

He-Pin wants to merge 1 commit into
apache:mainfrom
He-Pin:optimize/h2-eliminate-double-dispatch

Conversation

@He-Pin

@He-Pin He-Pin commented Jun 21, 2026 •

Copy link
Copy Markdown
Member

Motivation

Reduce avoidable per-request and per-frame work without changing where application handlers run or how HTTP/2 connections drain.

Modification

  • Keep handler invocation and pending-response callbacks on the existing handler execution context. Attach the stream ID immediately when a successful response is already available.
  • Allocate handler recovery once, rather than per request. Keep the same synchronous and asynchronous error handling.
  • Handle each frame with a single demux match, reuse the existing response pass-through handler, and remove closures around stream-state transitions without changing their bookkeeping.
  • Drain header continuation fragments from the compressor's existing stage logic instead of allocating a replacement handler, preserving order and HPACK table updates.

Result

The HTTP/2 dispatcher contract, wire ordering and header parsing stay unchanged. The proposed secondary header-parse cache and DATA/trailer coalescing are out of scope for this revision: they need separate evidence for admission, retention and graceful draining respectively. The earlier end-to-end benchmark numbers do not apply to this revision; no numerical throughput claim is made.

Tests

  • Exact current-main rebase: 19 focused settings, dispatcher, valid client/server and graceful-close cases passed on each of Scala 2.13.18 and 3.3.9 on JDK 17. An independent exact-source run passed 8 ordinary cases on each version.
  • Scoped formatting and git diff --check passed. The bytecode check confirms that the recovery callback is allocated once per wrapper, not per request; this is not a throughput benchmark.
  • The current head passed all six GitHub checks, including Validate and test, formatting, and license headers. The CI matrix exercised the dispatcher, handler error recovery, and valid continuation-order coverage. No full local CI suite or throughput run is claimed.

References

@He-Pin
He-Pin marked this pull request as draft June 21, 2026 16:00
@He-Pin
He-Pin marked this pull request as ready for review June 22, 2026 09:00
@pjfanning
pjfanning requested review from jrudolph and raboof June 23, 2026 12:52
@He-Pin

He-Pin commented Jun 23, 2026

Copy link
Copy Markdown
Member Author

The AffinityPool gives me better number than the ForkJoinPool on my local pc

response.map(_.addAttribute(Http2.streamId, streamIdHeader))
}
case None => response
}

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

@pjfanning Should I extract these commits one by one by one as seperated PR?

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.

I'm ok with leaving this as is. I'm hoping that other people will review this but if not, I'll ok this in a few days.

@He-Pin
He-Pin force-pushed the optimize/h2-eliminate-double-dispatch branch from 820263f to 8abbce3 Compare June 27, 2026 12:56
@pjfanning

Copy link
Copy Markdown
Member

@He-Pin could you fix the scalafmt and look into the test failure in CI?

@jrudolph

jrudolph commented Jul 1, 2026

Copy link
Copy Markdown
Contributor

Nice investigation!

I haven't looked at all points but "Double ExecutionContext dispatch: handleWithStreamIdHeader wrapped the user handler call in Future { }, adding an unnecessary EC dispatch hop on top of mapAsyncUnordered's own scheduling" was an explicit feature. The question is how mapAsyncUnordered schedules the Future-returning thunks these days. It used to be the case, that the thunks themselves were executed synchronously and could block the stream engine. In particular, the optimizations in FastFuture could mean that a whole routing structure and request processing could run synchronously. Without the existing wrapping Future, we might de-facto serialize request processing on a single http2 connection.

@He-Pin

He-Pin commented Jul 1, 2026

Copy link
Copy Markdown
Member Author

I’ve considered that, too; it would help avoid heavier renewal overhead for users. However, it’s not very friendly to the grpcbench benchmarks, so perhaps we could look into making it configurable.

@He-Pin
He-Pin force-pushed the optimize/h2-eliminate-double-dispatch branch from 8abbce3 to 8c39889 Compare November 25, 2026 11:29
@He-Pin He-Pin changed the title Optimize HTTP/2 request/response processing: eliminate double dispatch, reduce allocations, and streamline stream pipeline perf: reduce HTTP/2 pipeline allocations and response handoffs Nov 25, 2026
He-Pin added a commit to He-Pin/incubator-pekko-http that referenced this pull request Nov 27, 2026
Motivation: Reduce redundant work in the HTTP/2 request and response pipeline without changing dispatcher isolation or wire-level scheduling.

Modification: Keep application code on its dispatcher and avoid re-dispatching completed responses. Share handler recovery, consolidate frame and stream-state transitions, and drain continuation fragments without replacing the outlet handler. Preserve the current main branch's settings acknowledgment, stream cleanup, and flow-control ordering.

Result: Preserve the handler contract and normal request/response behavior with less per-message allocation. Do not change header parse caching or DATA/trailer batching and make no end-to-end throughput claim.

References: Apache Pekko HTTP apache#1081
@He-Pin
He-Pin force-pushed the optimize/h2-eliminate-double-dispatch branch from 8c39889 to 01ae156 Compare November 27, 2026 02:25
@He-Pin
He-Pin requested a review from pjfanning November 27, 2026 02:26
Motivation: Reduce redundant work in the HTTP/2 request and response pipeline without changing dispatcher isolation or wire-level scheduling.

Modification: Keep application code on its dispatcher and avoid re-dispatching completed responses. Share handler recovery, consolidate frame and stream-state transitions, and drain continuation fragments without replacing the outlet handler. Preserve the current main branch's settings acknowledgment, stream cleanup, and flow-control ordering.

Result: Preserve the handler contract and normal request/response behavior with less per-message allocation. Do not change header parse caching or DATA/trailer batching and make no end-to-end throughput claim.

References: Apache Pekko HTTP apache#1081
@He-Pin
He-Pin force-pushed the optimize/h2-eliminate-double-dispatch branch from 01ae156 to 06c6d2c Compare November 29, 2026 14:14
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