Repository navigation
Return precomputed bytes for single-byte indexed fields - #305
Open
DicksonWu654 wants to merge 1 commit into
Open
DicksonWu654 wants to merge 1 commit into
DicksonWu654 wants to merge 1 commit into
Conversation
Encoder._encode_indexed() builds a bytearray through encode_integer(), sets the top bit and copies it to bytes for every indexed header, even though any index below 127 encodes to a single fixed byte. Look those up in a 127-entry tuple instead. Other values and types keep using encode_integer(), with the same validation and errors. When DEBUG logging is enabled the original path is used as well, so the "Encoding ..." debug records are unchanged. Co-Authored-By: Claude Opus 5.5 <[email protected]>
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.
Every header that is already in the static or dynamic table is encoded by
Encoder._encode_indexed(), which callsencode_integer()(building abytearray), sets the top bit and copies the result tobytes. Any index below 127 fits in the 7-bit prefix and always encodes to the single byte0x80 | index, so this looks those up in a 127-entry tuple of precomputedbytesinstead.bool, negative numbers, non-ints) still go throughencode_integer(), with the same output and errors.hpacklogger has DEBUG enabled, the existing path is used as well, so theEncoding %d with %d bitsrecords (including theirfuncName) are unchanged.The table adds 127 one-byte
bytesobjects at import time.Benchmarks
Each workload runs header blocks through
Encoder.encode()and, for*_roundtrip,Decoder.decode().browser_mixedalternates 16 browser-like requests and 16 responses;repeated_requestssends one request 16 times, so after the first block every field is indexed;long_cookieis 16 requests whose cookie is about 1.2 KB;no_huffman_encodeisbrowser_mixedwithhuffman=False;fixture_stories_roundtripreplays the first four stories of thehpack-test-casefixtures intests/.master and this branch were imported side by side in the same interpreter and timed in alternating ~20 ms CPU-time blocks (40 rounds in each of 6 processes, pinned to one core). The table shows the median per-round speedup with a 95% bootstrap interval. The machine was shared with other work, so the absolute times are only indicative.
browser_mixed_encodebrowser_mixed_roundtriprepeated_requests_encoderepeated_requests_roundtripresponses_decodelong_cookie_encodelong_cookie_roundtripno_huffman_encodefixture_stories_roundtripBehaviour
True), invalid values, and a DEBUG-logging round trip that checks the redaction of sensitive values and that the integer-encoding records still come fromencode_integer.pytestpasses with 100% statement and branch coverage on CPython 3.11, 3.12, 3.13 and 3.14;ruff check src/andmypy src/(thelinttox env) pass._encode_indexed()calls with indices from 0 to 200. The encoded bytes, decoded headers, exceptions and, with DEBUG logging enabled, every log record were identical.🤖 Generated with Claude Code