Optimize h2 parsers - #15676
Closed
lorban wants to merge 5 commits into
Closed
Optimize h2 parsers#15676lorban wants to merge 5 commits into
lorban wants to merge 5 commits into
Conversation
HpackBenchmark measures HPACK alone, which left the obvious question unanswered: how much of handling a frame is HPACK, and how much is everything else. Nothing covered the parser and generator layers. Add generation and parsing of whole HEADERS and DATA frames, warmed the way a connection warms them, alongside a cold HEADERS parse. The answer for parsing is that HPACK is essentially all of it: a warmed HEADERS frame parses in about the same time HPACK alone takes to decode it, and a 1 KiB DATA frame parses in under 30ns because the body is sliced rather than copied.
lorban
force-pushed
the
enhancement/12.1/wendigo-h2-parsers
branch
from
August 31, 2026 09:18
d50e4c7 to
178c380
Compare
Signed-off-by: Ludovic Orban <lorban@bitronix.be>
HeaderParser stepped a 5-state machine once per octet to parse the 9 octets frame header, which is on the hot path of every frame. When the whole header is available in the buffer (the common case), read octets 0..7 with a single getLong() and extract length, type, flags and the high 3 octets of the stream id with shifts and masks, then read the last octet of the stream id. The octet-at-a-time state machine is kept as the fallback for headers split across buffers. This mirrors the existing 64-bit lookahead used by HttpParser for the HTTP/1 request line.
PrefaceParser compared the 24 preface octets one at a time against PREFACE_BYTES. Precompute the preface as overlapping 64-bit words so that, when all the remaining preface octets are in the buffer, they can be compared with 3 long comparisons instead of 24 byte ones. The words are indexed by preface offset, so the fast path also covers the h2c direct upgrade case, where the parser resumes at the offset after PREFACE_PREAMBLE_BYTES; there fewer than 8 octets remain and the comparison stays octet-wise. The octet-at-a-time loop remains for prefaces split across buffers. Failure handling is factored into invalidPreface() and is unchanged: the buffer is cleared and a PROTOCOL_ERROR is notified.
SettingsBodyParser split each 6 octets setting across the SETTING_ID and SETTING_VALUE states, so every setting cost two loop iterations plus the state transition between them. When all 6 octets are available, read the identifier and the value together and stay in SETTING_ID for the next setting. The split-buffer states are unchanged and still handle settings that straddle buffers.
Contributor
Author
|
I've reworked the benchmark to make it focus on all changes in this PR (and only changes made in this PR) and the perf improvements are not conclusive: 12.1.x vanilla with PR There is an improvement in the preface parser, but none in the settings parser and s noticeable degradation in the headers parser. |
lorban
force-pushed
the
enhancement/12.1/wendigo-h2-parsers
branch
from
August 31, 2026 10:01
178c380 to
7bf195b
Compare
Contributor
Author
|
Not conclusive perf improvements, closing without merging. |
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.
The H2 parsers part of #15498 made by @wendigo