Repository navigation
Encode long Huffman values without growing-integer shifts - #304
Open
DicksonWu654 wants to merge 1 commit into
Open
DicksonWu654 wants to merge 1 commit into
DicksonWu654 wants to merge 1 commit into
Conversation
HuffmanEncoder.encode() appends every symbol to one Python integer with a shift and an or. Each step copies the whole integer, so encoding is quadratic in the value length and long values such as cookies are slow. For bytes values of 256 bytes or more, map each distinct symbol to its code as a string of '0'/'1' digits, join them, pad with EOS bits, and convert once with int(bits, 2).to_bytes(). The codes are read from the encoder's tables on every call. Shorter values, subclasses and code tables with unusual entries (non-int, zero or over-wide lengths) keep the existing loop, so output is 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.
HuffmanEncoder.encode()appends each symbol to a single Python integer (final_num <<= length; final_num |= code). Every step copies the whole integer, so encoding is quadratic in the value length, and long values such as cookies dominate encode time.For
bytesvalues of 256 bytes or more, this builds a small map from each distinct input byte to its code written as a string of0/1digits, joins those strings, pads with EOS (1) bits to a whole byte and converts once withint(bits, 2).to_bytes(...). The map is rebuilt on every call from the encoder's ownhuffman_code_list/huffman_code_list_lengths, so nothing is cached between calls and changes to those tables are still picked up.Everything else keeps the existing loop: values shorter than 256 bytes,
bytessubclasses,HuffmanEncodersubclasses, and code tables that are not plain lists of ints or contain a code with a length outside 1..30 or a value that does not fit its length (the loop's masking/overlap behaviour for such tables is not reproduced, so they fall back). The output is identical in all cases.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;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_roundtripOnly values of 256 bytes or more take the new path, so the other workloads are unchanged within noise.
Behaviour
int-subclass, list-subclass and short tables, with the exact bytes the existing loop produces.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.strinput, table size changes, Huffman on and off, plus directHuffmanEncodercalls. The encoded bytes, decoded headers, exceptions and, with DEBUG logging enabled, every log record were identical.🤖 Generated with Claude Code