Deflate compression overhead (source code)

= Deflate compression overhead
{c}

Short <inputs> can spend much of their size on <Deflate block framing>, the end-of-block symbol and, for dynamic blocks, <Deflate Huffman coding> descriptions. A repeated phrase is useful only if its <Deflate backreference> costs less than the <Deflate literal bytes> it replaces. <Huffman coding> adapts instruction costs; it does not make either references or code descriptions free.

For an exact expansion example, take the 256 <bytes> with values $0,1,\ldots,255$, each appearing once. A single fixed block containing only <Deflate literal bytes> costs $144\cdot8+112\cdot9+7+3=2170$ <bits>, or 272 <bytes> after final packing. One stored block starting at a <byte> boundary instead costs five overhead <bytes> plus the 256 data <bytes>, or 261 <bytes>. Both exceed the 256-<byte> <input>; these counts follow the specified layouts and are not measurements of an encoder's automatic choice. The example illustrates <universal lossless shortening is impossible>.<DEFLATE Compressed Data Format Specification version 1.3 by L. Peter Deutsch>[RFC 1951, sections 3.2.3, 3.2.4 and 3.2.6: header, stored layout and fixed literal lengths]{ref}

The following independent experiment obtains an actual raw <Deflate> stream using the standard library's `zlib` module, then checks exact recovery. Negative `wbits` selects raw framing; the three lines of compressed-data handling make no assumptions about which <Deflate backreferences> the library selects.<Python 3.14 documentation/zlib — Compression compatible with gzip (Python 3.14 documentation)>[Python 3.14 zlib documentation, compressobj, decompress and negative wbits]{ref}
``
import zlib
data = b"ABCABCABCABC"
encoder = zlib.compressobj(level=6, wbits=-15)
packed = encoder.compress(data) + encoder.flush()
assert zlib.decompress(packed, wbits=-15) == data
print(len(data), len(packed), packed.hex())
``
The printed <bytes> describe this library run. Exact roundtrip recovery is the check; a particular compressed length or instruction choice is not a guarantee for other <inputs> or library versions.