You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
perf(brotli,zstd): faster encode on low-redundancy input
Audit of the codecs against their official CLIs found two encoders that
were dramatically slower than the reference on incompressible/low-match
data — both linear but with pathological constants. (Interop and the
streaming contract were otherwise clean across every codec with an
official tool; lh1/lh2's Unsupported-without-length is documented, not a
bug.)
brotli: the literal-context histogram clustering was O(contexts^3 * 256)
— it rescanned all cluster pairs and recomputed each cluster's cost from
scratch on every merge — which exploded on dense histograms (~37k
instructions/byte on random input). Cache per-cluster costs and the
pairwise-delta matrix, updating only the merged cluster each round. The
merge sequence and compressed output are byte-for-byte identical;
incompressible encode is ~8x faster.
zstd: the match finder used a fixed 64 Ki-bucket hash table over an
up-to-8 MiB window (load factor in the hundreds), so each probe walked a
full chain of useless far links. Size the table to the window. Also build
the per-block match index incrementally — the chains persist across blocks
(the history prefix is byte-stable until a window trim) instead of
re-indexing all of history every block, which was O(history) per block and
quadratic over a stream. Output is unchanged on single-block inputs and
equal-or-smaller on multi-block ones (0 ratio regressions observed);
random encode is ~3x faster.
Verified: brotli output byte-identical across inputs x quality 0..11;
zstd 0 ratio regressions and interop both ways with the zstd CLI; 50-case
zstd fuzz (incl. >8 MiB trim path and block boundaries) round-trips
through both our decoder and the CLI; full suite, clippy, and fmt clean.
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
0 commit comments