Skip to content

Use the zstd extension for zstd when phpredis is built without libzstd - #3020

Open
marcing wants to merge 5 commits into
phpredis:developfrom
marcing:zstd-ext-fallback
Open

marcing wants to merge 5 commits into
phpredis:developfrom
marcing:zstd-ext-fallback

Conversation

@marcing

@marcing marcing commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

Summary

zstd compression in phpredis needs a build with --enable-redis-zstd. That is not the PECL default, and no Windows build has it. Many such installs already load the zstd extension (kjdev/php-ext-zstd), which writes the same standard frames. With this PR, a build without libzstd calls that extension's zstd_compress() / zstd_uncompress() when the extension is loaded at startup. That covers both Redis::OPT_COMPRESSION and redis.session.compression=zstd.

This is a fallback, not a replacement: --enable-redis-zstd stays the recommended build. A libzstd build compiles none of the new code (everything sits in #ifndef HAVE_REDIS_ZSTD), so its behaviour does not change, even when the zstd extension is loaded too.

Behaviour

Situation Result
Built with libzstd (with or without ext-zstd) Unchanged.
No libzstd, ext-zstd loaded Redis::COMPRESSION_ZSTD* constants are defined, setOption() accepts zstd, sessions compress. php --ri redis shows Zstd provider => zstd extension <version>.
No libzstd, no ext-zstd Unchanged: constants undefined, setOption() returns false. The session notice now also suggests loading the zstd extension.
zstd_compress / zstd_uncompress in disable_functions Constants defined (MINIT runs first), but setOption() returns false and sessions get the "not available" notice.
A userland function named zstd_compress Never called. Only the extension's own internal functions are used.

Frames are interchangeable between the two kinds of build in both directions. This was checked from 0 B to 5 MB, including session data.

How it works

  • Constants: declared at MINIT only when the extension is usable, so defined('Redis::COMPRESSION_ZSTD') still says whether zstd works. _MIN is 1, matching the libzstd branch, which treats every level below 1 as the default. ZEND_MOD_OPTIONAL("zstd") makes the engine start ext-zstd first, whatever the ini order.
  • Function lookup: done on every call, with no pointer cached at MINIT. disable_functions is applied after MINIT and frees the entry. A function must be ZEND_INTERNAL_FUNCTION and belong to the zstd module. On 7.4, a disabled function keeps its module but loses its arginfo, so that is checked as well.
  • Error isolation: the call runs under EH_THROW, with the user error handler moved aside and error_reporting limited to fatal errors. The extension's warnings become an exception that is dropped. Nothing is shown, logged, passed to a user error handler or left in error_get_last(). A fatal error restores everything and bails out.
  • Decoding: a frame reaches the extension only if its header declares a content size of at most INT_MAX, which is the libzstd branch's own requirement. The result must have exactly that size; otherwise the value is returned as stored, as with libzstd. The header parser follows RFC 8878 §3.1.1 and reads only the header.
  • Empty values: the frame of an empty value is checked and decoded in phpredis. Released ext-zstd (0.18.0) resizes its output buffer to 0 bytes for that frame, which valgrind reports as realloc() with size 0. Their development branch no longer does this.
  • Levels: clamped as in the libzstd branch, so the extension's level warning cannot fire.

Known differences from the libzstd decoder

Neither case occurs with data phpredis itself writes:

  • A valid frame followed by trailing bytes, or a frame missing only its trailing checksum, can decode through the extension. libzstd returns it as stored. This depends on the installed ext-zstd version, since released versions decode differently from its development branch.
  • A value that is only a skippable frame decodes to '' with libzstd. The fallback returns it as stored.

Closing these would take a block walk of about 20 lines. I left it out, but can add it if you want.

Why not something else

  • A libzstd option in config.w32: config.w32 has no compression options at all. Adding one would need libzstd in the Windows builder's dependencies, and it would do nothing for PECL or PIE installs on Linux.
  • A C API in ext-zstd: none exists. Using one would need an upstream release, a version floor, headers at build time and a hard link-time dependency.

Cost

Each call makes one input copy, one output copy and one hash lookup. That is small next to zstd itself, but the libzstd build remains the faster path.

Rollout note for mixed fleets

After upgrading, hosts that load ext-zstd start defining Redis::COMPRESSION_ZSTD. Apps that pick compression with defined() will begin writing zstd. Hosts with neither provider read those values as raw bytes. This is the same as moving to a libzstd build: upgrade the readers first.

Commits

  1. fix: an unknown redis.session.compression value such as gzip was told to "rebuild phpredis with gzip support" instead of being reported as invalid. The or-ed strcasecmp() calls lacked == 0, so the condition was always true. This commit stands alone.
  2. The fallback.
  3. Tests:
    • testCompressionZSTDInvalid: invalid values with a throwing error handler; error_get_last() stays empty; the empty frame with and without its checksum.
    • testCompressionZSTDLevels.
    • testCompressionZSTDInterop.
    • Session child processes now load ext-zstd when it is present.
  4. CI: a zstd-ext job runs PHP 7.4, 8.0 and 8.5 NTS, plus 8.5 ZTS (checked to really be ZTS), without libzstd. It runs the Redis, RedisArray and RedisCluster suites, also under valgrind. Every other job builds with libzstd, so this code ran nowhere before.
  5. Docs: README (session compression) and INSTALL (Windows packages).

Not included, for you to decide:

  • A CHANGELOG entry; your entries cite merged commits.
  • Rewording the "must be compiled with ZSTD" stub comment. That would mean regenerating the arginfo files.

Tested

Run locally in Docker against Redis 8:

  • No libzstd + ext-zstd 0.18.0, on PHP 8.5 NTS, 8.5 ZTS and 7.4: the ZSTD, Compress, Pack and Session test groups pass. Valgrind is clean on the ZSTD group (8.5 NTS and ZTS).
  • libzstd build + ext-zstd, same three PHP versions: the same test groups pass.
  • Session round trips and frames crossing between providers both ways; ext-zstd absent; functions disabled; a userland polyfill; redis loaded before zstd on the command line.

Not run locally: the new CI job, Windows, PHP 8.0 to 8.4.

🤖 Generated with Claude Code

The check that picks between "not available" and "is invalid" or-ed three
strcasecmp() calls, which is true for every value, so a misspelt name such as
'gzip' was told to rebuild phpredis with gzip support.
A phpredis built without --enable-redis-zstd (the PECL default and every
Windows build) could not compress with zstd even when the zstd extension
was loaded. Such a build now calls zstd_compress() and zstd_uncompress()
when that extension is present at startup, for OPT_COMPRESSION and for
redis.session.compression alike. Builds with libzstd are unchanged: all of
this is compiled out there.

- The constants COMPRESSION_ZSTD, _DEFAULT, _MIN and _MAX are declared at
  MINIT only when the extension is usable, so defined() keeps telling
  whether zstd is available. ZEND_MOD_OPTIONAL("zstd") makes it start first.
- Only the extension's own internal functions are called, looked up on
  each use: a userland function of the same name is never run, and one
  switched off by disable_functions makes zstd unavailable.
- The call runs under EH_THROW, with the user error handler moved aside
  and error_reporting limited to fatal errors. A warning becomes an
  exception that is dropped, so nothing is shown, logged or left in
  error_get_last(), and undecodable data falls back to "not compressed"
  as with libzstd.
- Only frames whose header declares a content size of at most INT_MAX are
  passed on, as the libzstd branch requires, and the result must have that
  size. The frame of an empty value is checked and decoded in phpredis:
  released versions of the extension resize their buffer to 0 bytes for it.
- Levels are clamped as in the libzstd branch.
- phpinfo lists zstd under "Available compression" and adds a
  "Zstd provider" row naming the extension.

The frames are the same standard frames libzstd writes, so data moves
between both kinds of build without conversion.
- testCompressionZSTDInvalid: plain data, a bare magic number, garbage
  after it, a truncated frame, a frame without a content size and an empty
  frame followed by junk come back as stored, with a throwing error handler
  installed and error_get_last() left empty, and _uncompress() refuses
  them. The empty frame, with and without its checksum, reads as ''.
- testCompressionZSTDLevels: levels below the minimum and above the
  maximum round-trip without a warning.
- testCompressionZSTDInterop: frames written by the zstd extension and by
  phpredis read back on the other side.
- Session child processes load the zstd extension when the test run has
  it, so the session tests cover the extension path too.
A job builds phpredis without libzstd and loads the zstd extension
(PHP 7.4, 8.0 and 8.5, plus 8.5 ZTS, checked to be ZTS). It checks that
the constants and the "Zstd provider" row appear, also with redis loaded
before zstd, and that disable_functions turns zstd off, then runs the
Redis, RedisArray and RedisCluster suites, also under valgrind, and
uploads the logs. Every other job builds with --enable-redis-zstd, so the
new code ran nowhere before.
README (session compression) and INSTALL (Windows packages): a build
without libzstd uses the zstd extension when it is loaded.

This branch has not been deployed

No deployments
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