Repository navigation
compress.deflate: build Huffman trees via the shared hash.huffman builder - #27394
Merged
JalonSolov merged 2 commits intoJun 9, 2026
Merged
Conversation
…lder Replace the two in-tree copies of the canonical Huffman code-assignment (RFC 1951 §3.2.2 bl_count / next_code) with calls to hash.huffman, the shared builder added for vlang#27358: - Decode: build_huff_tree() now fills its flat LSB-first lookup table via huffman.flat_table(). The table format (entry = (symbol << 5) | length, 0xFFFF_FFFF for invalid) and the decode loop are unchanged. flat_table() assigns each code inline while filling (no intermediate codes array) and skips the invalid pre-fill for complete codes, so it does strictly less work than the old hand-rolled loop on the common path. build_huff_tree() now returns `!HuffTree` so an over-subscribed (malformed) code surfaces as a clean error instead of a silently corrupt table; all call sites propagate with `!`. - Encode: fixed_litlen_encode() takes its reversed codes straight from huffman.build(..., bit_order: .lsb_first).codes. No behavior change for any valid DEFLATE stream (incomplete codes are still accepted). Benchmarked before/after with -prod: decode-dominated throughput (8 MiB fixed-Huffman text, 2 MiB dynamic-Huffman) is unchanged, and the build-dominated paths (thousands of small dynamic-block streams) are ~5-11% faster than the old code, since the fixed trees and most dynamic trees are complete and skip the invalid pre-fill. compress.deflate / gzip / zlib suites stay green. This is the second half of vlang#27358 (depends on the hash.huffman module PR). Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
|
Codex usage limits have been reached for code reviews. Please check with the admins of this repo to increase the limits by adding credits. |
JalonSolov
reviewed
Jun 8, 2026
JalonSolov
reviewed
Jun 8, 2026
…f panicking Address review on vlang#27394: fixed_litlen_encode() now returns `!([]u32, []int)` and deflate_compress_fixed() returns `![]u8`, propagating the (in practice unreachable) huffman.build error instead of panicking. This matches the decode-side build_huff_tree(), which already returns `!` in this PR, and keeps a clean error path end to end. The public compress* APIs are unchanged (already `![]u8`). Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
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.
What
Migrates
compress.deflateontohash.huffman(merged in #27392), removing its two in-tree copies of the canonical Huffman code-assignment (RFC 1951 §3.2.2bl_count/next_code).This is the second half of #27358 (the first half — the shared module + the HPACK migration — landed in #27392).
Changes
build_huff_tree()now fills its flat LSB-first lookup table viahuffman.flat_table(). The table format (entry = (symbol << 5) | length,0xFFFF_FFFFfor invalid) and thehuff_decodeloop are unchanged.build_huff_tree()now returns!HuffTreeso an over-subscribed (malformed) code surfaces as a clean error instead of a silently corrupt table; all call sites propagate with!.fixed_litlen_encode()takes its reversed codes straight fromhuffman.build(..., bit_order: .lsb_first).codes.No behavior change for any valid DEFLATE stream (incomplete codes are still accepted).
Performance
Benchmarked before/after with
v -prod(full method + corpora in the #27392 benchmark comment):Decode-dominated throughput is unchanged (the decode loop is byte-for-byte identical); build-dominated workloads (many small streams) are faster, because
flat_table()skips the invalid table pre-fill for complete codes — which V's fixed trees and most dynamic trees are. The build-dominated ranges are non-overlapping across runs, so the speedup is real, not jitter.Tests
compress.deflate/ gzip / zlib / snappy / zstd suites stay green;-cstrict -cc clangclean;v fmt -verifyclean.🤖 Generated with Claude Code
Closes #27358.