Every file you compress is a string of symbols — letters, bytes, pixel values — each appearing with some frequency. Information theory says the shortest possible encoding assigns each symbol a code of length − bits, where p is its probability. That limit is called Shannon entropy, and reaching it has always been the holy grail of lossless compression.
For decades the two ways to approach the limit were Huffman coding (fast but stuck to integer bit lengths, losing a little per symbol) and arithmetic coding (near-perfect but painfully slow on real hardware). In 2009 Jarek Duda published a radically different idea: instead of assigning bit patterns, fold every symbol directly into one giant integer called the state. The technique is Asymmetric Numeral Systems (ANS).
ANS encodes an entire message as a single number, then decodes that number back into the original symbols — in reverse order, like unwinding a stack. The encoder grows the state with each symbol; the decoder shrinks it. The key insight is that the state grows at a rate that precisely mirrors the symbol's probability, so the total bits used per symbol converges to the Shannon entropy without any of the bookkeeping overhead that makes arithmetic coding slow.
Two practical variants dominate modern software:
- rANS (range ANS) — arithmetically exact, works in streaming blocks, used inside zstd's entropy kernel.
- tANS (table ANS) — precomputes every transition into two lookup tables; a single table read encodes or decodes one symbol, making it exceptionally fast on CPUs.
Today ANS is the entropy coder inside zstd (Facebook/Meta), LZFSE (Apple), Brotli's entropy layer, and many GPU-accelerated codecs. It compresses at speeds previously associated only with simple byte-copy operations, while achieving ratios that once required heavy arithmetic coding.
Comments
Loading comments...