Commit a063235
compress.deflate: build Huffman trees via the shared hash.huffman builder (#27394)
* compress.deflate: build Huffman trees via the shared hash.huffman builder
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 #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 #27358 (depends on the hash.huffman module PR).
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
* compress.deflate: propagate errors from fixed_litlen_encode instead of panicking
Address review on #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>
---------
Co-authored-by: Richard Wheeler <quaesitor.scientiam@gmail.com>
Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>1 parent 164b303 commit a063235
3 files changed
Lines changed: 32 additions & 76 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
129 | 129 | | |
130 | 130 | | |
131 | 131 | | |
132 | | - | |
| 132 | + | |
133 | 133 | | |
134 | 134 | | |
135 | 135 | | |
136 | 136 | | |
137 | | - | |
| 137 | + | |
138 | 138 | | |
139 | 139 | | |
140 | 140 | | |
| |||
146 | 146 | | |
147 | 147 | | |
148 | 148 | | |
149 | | - | |
| 149 | + | |
150 | 150 | | |
151 | 151 | | |
152 | 152 | | |
| |||
158 | 158 | | |
159 | 159 | | |
160 | 160 | | |
161 | | - | |
| 161 | + | |
162 | 162 | | |
163 | 163 | | |
164 | 164 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
| 3 | + | |
| 4 | + | |
3 | 5 | | |
4 | 6 | | |
5 | 7 | | |
| |||
8 | 10 | | |
9 | 11 | | |
10 | 12 | | |
11 | | - | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
12 | 16 | | |
13 | | - | |
14 | | - | |
15 | | - | |
16 | | - | |
17 | | - | |
18 | | - | |
19 | | - | |
20 | | - | |
21 | | - | |
22 | | - | |
23 | | - | |
24 | | - | |
25 | | - | |
26 | | - | |
27 | | - | |
28 | | - | |
29 | | - | |
30 | | - | |
31 | | - | |
32 | | - | |
33 | | - | |
34 | | - | |
35 | | - | |
36 | | - | |
37 | | - | |
38 | | - | |
39 | | - | |
40 | | - | |
| 17 | + | |
| 18 | + | |
41 | 19 | | |
42 | 20 | | |
43 | 21 | | |
| |||
135 | 113 | | |
136 | 114 | | |
137 | 115 | | |
138 | | - | |
139 | | - | |
| 116 | + | |
| 117 | + | |
140 | 118 | | |
141 | 119 | | |
142 | 120 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
| 3 | + | |
| 4 | + | |
3 | 5 | | |
4 | 6 | | |
5 | 7 | | |
| |||
39 | 41 | | |
40 | 42 | | |
41 | 43 | | |
42 | | - | |
| 44 | + | |
43 | 45 | | |
44 | 46 | | |
45 | 47 | | |
| |||
52 | 54 | | |
53 | 55 | | |
54 | 56 | | |
55 | | - | |
56 | | - | |
57 | | - | |
58 | | - | |
59 | | - | |
60 | | - | |
61 | | - | |
62 | | - | |
63 | | - | |
64 | | - | |
65 | | - | |
66 | | - | |
67 | | - | |
68 | | - | |
69 | | - | |
70 | | - | |
71 | | - | |
72 | | - | |
73 | | - | |
74 | | - | |
75 | | - | |
76 | | - | |
77 | | - | |
78 | | - | |
79 | | - | |
80 | | - | |
81 | | - | |
82 | | - | |
83 | | - | |
84 | | - | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
85 | 63 | | |
86 | 64 | | |
87 | 65 | | |
| |||
192 | 170 | | |
193 | 171 | | |
194 | 172 | | |
195 | | - | |
196 | | - | |
| 173 | + | |
| 174 | + | |
197 | 175 | | |
198 | 176 | | |
199 | 177 | | |
| |||
220 | 198 | | |
221 | 199 | | |
222 | 200 | | |
223 | | - | |
| 201 | + | |
224 | 202 | | |
225 | 203 | | |
226 | 204 | | |
| |||
249 | 227 | | |
250 | 228 | | |
251 | 229 | | |
252 | | - | |
253 | | - | |
| 230 | + | |
| 231 | + | |
254 | 232 | | |
255 | 233 | | |
256 | 234 | | |
| |||
284 | 262 | | |
285 | 263 | | |
286 | 264 | | |
287 | | - | |
288 | | - | |
| 265 | + | |
| 266 | + | |
289 | 267 | | |
290 | 268 | | |
291 | 269 | | |
| |||
318 | 296 | | |
319 | 297 | | |
320 | 298 | | |
321 | | - | |
| 299 | + | |
322 | 300 | | |
323 | 301 | | |
324 | 302 | | |
| |||
347 | 325 | | |
348 | 326 | | |
349 | 327 | | |
350 | | - | |
351 | | - | |
| 328 | + | |
| 329 | + | |
352 | 330 | | |
353 | 331 | | |
354 | 332 | | |
| |||
0 commit comments