Skip to content

Add advanced compression parameters (Part of #118) - #121

Merged
zcsizmadia merged 1 commit into
mainfrom
bench-cparams
Dec 4, 2026
Merged

zcsizmadia merged 1 commit into
mainfrom
bench-cparams

Conversation

@zcsizmadia

Copy link
Copy Markdown
Owner

Part of #118.

What

Setters on Compressor, StreamEncoder and Encoder for every libzstd compression parameter:

  • set_strategy, set_window_log, set_hash_log, set_chain_log, set_search_log, set_min_match, set_target_length;
  • set_literal_compression (Auto / Enabled / Disabled).

Each overrides one parameter of the level's set (ZSTD_overrideCParams). The output is byte-identical to libzstd 1.6.0's with the same level and parameters (ZSTD_compress2 / ZSTD_compressStream2 after ZSTD_CCtx_setParameter).

Other API changes:

  • new public types Strategy and LiteralCompression;
  • Error::ParameterOutOfBound for the values libzstd rejects;
  • StreamEncoder::set_window_log was hidden and panicked on an invalid value. It is now public and returns a Result.

StreamEncoder returns StageWrong if a parameter is changed mid-frame; libzstd allows some changes there, so this is stricter. Defaults and default output are unchanged.

Ported along the way

The new differential tests found two bugs:

  • Optimal parser: with literal compression disabled, libzstd prices literals at 8 bits and keeps no literal statistics (ZSTD_compressedLiterals). This is done with zero frequencies and a base price, so the per-position price code is unchanged.
  • Multithreading: the jobs after the first now get the literal mode too.

Tests

New tests/cparams.rs compares random parameter sets over the whole valid range against libzstd:

  • one-shot on a reused context, sizes up and down;
  • streaming with flushes, pledged and unpledged;
  • loaded and prepared dictionaries;
  • multithreading;
  • the setter bounds against ZSTD_CCtx_setParameter, and the mid-frame errors.

ZSTD_NATIVE_CPARAMS_ITERS and ZSTD_NATIVE_CPARAMS_SEED widen the search. 64 seeds × 1,500 sets pass on the EPYC host. The workspace and portable suites pass there too.

Benchmarks

EPYC 7543, 16 lanes (one core per L3 domain), interleaved binary copies.

Default path, main vs this branch: identical output in every case.

  • Geomean speed is 1.001 over 163 file/level/chunk cases (12 Silesia files, levels -5 to 22, whole files and 1 KiB / 16 KiB frames, 7 rounds).
  • perf stat instruction counts are identical (ratio 1.0000).
  • The ~1% seen at L-5, L-1 and L7 is binary placement. Three copies of main differ by up to 5.5% in cycles (nci L7). Over three copies of each, the medians are level or in this branch's favour:
main copies (median) branch copies (median)
nci 7 0.996 0.985
xml 7 0.998 0.986
ooffice -5 0.999 0.996
webster 7 1.001 1.002
mozilla -5 0.999 0.996

(cycles relative to main copy a; lower is faster)

Against zrip, at zrip's ratio. Speed on the Pareto frontier at each zrip point's ratio, divided by zrip's speed (5 rounds; levels -64 to 1, with and without lit=on,mml=7 / lit=on,mml=6):

zrip level zstd-native levels libzstd levels libzstd + params zstd-native + params
-7 0.87x 0.82x 0.92x 0.98x
-6 0.80x 0.75x 0.88x 0.95x
-5 0.81x 0.76x 0.89x 0.95x
-4 0.86x 0.81x 0.90x 0.96x
-3 0.91x 0.84x 0.90x 0.97x
-2 1.07x 1.00x 1.04x 1.11x
-1 1.08x 1.01x 1.05x 1.12x
all 84 0.91x 0.85x 0.94x 1.00x

Decoding Huffman literals at negative levels (zstd-native decoder, frames from libzstd with and without ZSTD_c_literalCompressionMode=enable):

  • 0.74x the speed of raw literals (geomean over 8 files × levels -7, -5, -3, -1);
  • that is still 1.1–2.3 GB/s;
  • for example, dickens at -5 decodes at 1,266 MiB/s instead of 1,918, and the ratio goes from 1.47 to 2.15.

Bench tools

  • profile_encode: ZSTD_BENCH_CPARAMS now applies to native too.
  • profile_decode: ZSTD_BENCH_LIT=1 turns on Huffman literals.

Not in this PR

A fuzz target with these parameters (follow-up).

Setters on Compressor, StreamEncoder and Encoder for every libzstd
compression parameter: strategy, window, hash, chain and search log,
minimum match, target length and literal compression mode
(ZSTD_c_strategy ... ZSTD_c_literalCompressionMode). Each overrides one
parameter of the level's set (ZSTD_overrideCParams); the output is
byte-identical to libzstd 1.6.0's with the same level and parameters.
New public types Strategy and LiteralCompression, and
Error::ParameterOutOfBound for the values libzstd rejects. Defaults and
default output are unchanged. StreamEncoder::set_window_log is now
public and returns a Result instead of panicking.

Ported along the way:
- the optimal parser prices literals at 8 bits and keeps no literal
  statistics when literal compression is disabled
  (ZSTD_compressedLiterals); done with zero frequencies and a base price,
  so the per-position price code is unchanged;
- the jobs of a multithreaded frame after the first take the literal
  mode too.

Tests: tests/cparams.rs compares random parameter sets over the whole
valid range with libzstd: one-shot on a reused context, streaming with
flushes, loaded and prepared dictionaries, multithreading, the setter
bounds and mid-frame errors. 64 seeds x 1500 sets pass on the EPYC host.

Benchmarks (EPYC 7543, 16 lanes, interleaved binary copies):
- default path, main vs branch: identical output, geomean speed 1.001
  over 163 file/level/chunk cases; perf stat instruction counts
  identical. The ~1% at L-5/L-1/L7 is copy placement: three copies of
  main differ by up to 5.5% (nci L7); medians over copies are level.
- against zrip at zrip's ratio (84 points): zstd-native levels 0.91x,
  libzstd + parameters 0.94x, zstd-native + parameters 1.00x.
- decoding Huffman literals at negative levels: 0.74x the speed of raw
  literals (1.1-2.3 GB/s), for much better ratios.

profile_encode: ZSTD_BENCH_CPARAMS applies to native too.
profile_decode: ZSTD_BENCH_LIT=1 enables Huffman literals.
@zcsizmadia
zcsizmadia merged commit 038c8fc into main Dec 4, 2026
20 checks passed
@zcsizmadia
zcsizmadia deleted the bench-cparams branch December 4, 2026 22:59
zcsizmadia added a commit that referenced this pull request Dec 5, 2026
#122)

The compress target now also sets the parameters added in #121:
strategy, hash, chain and search log, minimum match, target length and
literal compression mode, each left at the level's value a third of the
time, on both libzstd (ZSTD_CCtx_setParameter) and zstd-native
(StreamEncoder and Compressor setters). The new field comes after the
dictionary in EncParams, so existing corpus entries keep their meaning.

The one-shot path no longer needs the level's window log: Compressor
has set_window_log now. Inputs are capped at 48 KiB from btlazy2 on (by
level or strategy), and hash and chain logs at 20.

32 workers for 10 minutes on the EPYC host (~3.7M inputs): no
mismatch, no crash.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant