Reference implementation of optimal static Huffman coding for 8-bit symbols, with matching C and Rust encoders/decoders.
- Optimal static Huffman coding of 8-bit symbols.
- No attempt is made to make the tree externalization as small as possible since that saves only a few bytes at the cost of increased complexity.
- Makes extensive use of data structure abstraction (as an example to students).
- Works for both Big and Little Endian architectures.
- Version: 1.0
Build C tools:
make
Build Rust tools:
cd rust && cargo build --release
Run Rust tests:
cd rust && cargo test
src/contains the C implementation (encode,decode, and shared modules).tests/contains fuzzing and benchmark helper scripts.rust/contains the bit-compatible Rust implementation.
Build with sanitizers, then run the Python fuzzer:
make clean && make CFLAGS='-Wall -Wextra -Wpedantic -Wshadow -Wparentheses -O1 -g -std=c17 -fsanitize=address,undefined' LDFLAGS='-fsanitize=address,undefined'python3 tests/fuzz_huffman.py --iterations 20000 --timeout 1.0
The fuzzer exercises both valid round-trip cases (encode -> decode) and
mutated malformed streams for decode. Any crash, timeout, sanitizer report,
or round-trip mismatch is saved under fuzz-crashes/.
Current output format is versioned and intended to be reproduced bit-for-bit:
- Header magic
MAGIC_V2(0xBEEFD00E) - 16-byte header
- 2-byte CRC16/CCITT-FALSE over exactly those 16 header bytes
- Serialized Huffman tree bytes
- Encoded payload bitstream
decode accepts both MAGIC_V1 and MAGIC_V2. A Rust implementation should
emit MAGIC_V2 with the same CRC16 placement and preserve this byte layout.
The following timing snapshot comes from:
pilot-bench: https://github.com/darrelllong/pilot-bench, commitf01eec4python3 tests/run_pilot_comparison.py --preset quick --session-limit 600 --cpu 9 --out-dir workloads/pilot_runs- helper scripts:
tests/pilot_run_local.sh(default local path; setCPUto pin),tests/pilot_run_remote.sh(generic remote runner; setREMOTE_HOST) - source data:
workloads/pilot_runs/comparison_summary.csv - kernel workload source:
workloads/kernel/README.md
Machine and method: knuth, NVIDIA GB10, Ubuntu 24.04.5 LTS (Linux
7.0.0-1019-nvidia, aarch64), performance governor. Pilot, the Python
case runner and the program under test were pinned together to logical
CPU 9, a Cortex-X925 core at up to 3.9 GHz. C built with GCC 13.3.0 and
the Makefile flags (-O3 -DNDEBUG -std=c17); Rust 1.95.0,
cargo build --release; Python 3.12.3. Run on September 28, 2026,
20:31–20:39 UTC, with no other benchmark or build running.
Each reading is the wall-clock time, measured by tests/run_huffman_case.py
with time.perf_counter(), of one invocation of encode or decode: process
start, reading the input file, coding, and writing the output to a
temporary file. Pilot's quick preset requires at least 30 subsession
samples, a 95% confidence interval no wider than 20% of the mean, and
autocorrelation within
Values are in seconds, reported as mean n), the number of readings Pilot took. Rust speedup
is C mean divided by Rust mean.
| Workload | Operation | C (s, mean |
C n | Rust (s, mean |
Rust n | Rust speedup |
|---|---|---|---|---|---|---|
| Shakespeare | encode | 53 |
64 |
1.29x |
||
| Shakespeare | decode | 33 |
41 |
1.23x |
||
| Kipling | encode | 30 |
30 |
1.27x |
||
| Kipling | decode | 84 |
30 |
1.18x |
||
| Linux kernel 6.19.6 tarball | encode | 90 |
30 |
1.64x |
||
| Linux kernel 6.19.6 tarball | decode | 30 |
124 |
1.77x |
The previous snapshot (March 2026, machine not recorded, Pilot before
f01eec4) reported the full width of the confidence interval after the
Stored in REFERENCES.bib:
@inproceedings{li2016pilot,
author = {Yan Li},
title = {Pilot: A Framework that Understands How to Do Performance Benchmarks The Right Way},
booktitle = {2016 IEEE 24th International Symposium on Modeling, Analysis and Simulation of Computer and Telecommunication Systems (MASCOTS)},
year = {2016},
month = sep,
address = {London, UK}
}- Do not try to be overly clever: simplicity and clarity are more important that exhibiting prowess in obscure C tricks.