Skip to content

Support negative Zstd compression levels#2874

Open
tillkruss wants to merge 1 commit into
phpredis:developfrom
tillkruss:fix-negative-zstd-level
Open

Support negative Zstd compression levels#2874
tillkruss wants to merge 1 commit into
phpredis:developfrom
tillkruss:fix-negative-zstd-level

Conversation

@tillkruss

Copy link
Copy Markdown
Member

Summary

OPT_COMPRESSION_LEVEL treated any value below 1 as "use the default", so negative Zstd levels (e.g. -5) were silently discarded and replaced with the default level 3. Zstd supports negative ("fast") compression levels, so they should be honored.

This changes the guard in the REDIS_COMPRESSION_ZSTD branch of redis_compress() from compression_level < 1 to compression_level == 0:

  • 0 — the ecalloc default and Zstd's own "use default" sentinel — still maps to ZSTD_CLEVEL_DEFAULT (or 3).
  • Negative values now pass through to ZSTD_compress().

No lower-bound clamp is added: ZSTD_compress() already clamps extreme negatives to ZSTD_minCLevel() internally, and ZSTD_minCLevel() only exists since libzstd 1.3.6 while phpredis supports ≥ 1.3.0 (config.m4), so calling it would break the build on older libzstd. The existing upper-bound clamp to ZSTD_maxCLevel() is unchanged.

Before

$redis->setOption(Redis::OPT_COMPRESSION_LEVEL, -5); // ignored, used level 3

After

$redis->setOption(Redis::OPT_COMPRESSION_LEVEL, -5); // uses Zstd level -5

OPT_COMPRESSION_LEVEL treated any value below 1 as "use the default",
so negative Zstd levels (e.g. -5) were silently replaced with the
default level 3. Zstd supports negative ("fast") compression levels,
so only 0 (the ecalloc default and Zstd's own "use default" sentinel)
should now map to the default; negative values are passed through to
ZSTD_compress, which clamps extreme values to ZSTD_minCLevel()
internally.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@michael-grunder

Copy link
Copy Markdown
Member

The change is fine. Not sure we can merge it before phpredis 7.0 though.

If many people are relying on < 1 to mean default then upgrading would immediately increase the size of the compressed Redis data.

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.

2 participants