Repository navigation
Conversation
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.
marcing
force-pushed
the
zstd-ext-fallback
branch
from
November 24, 2026 18:34
ea0ffda to
dbe6c1c
Compare
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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'szstd_compress()/zstd_uncompress()when the extension is loaded at startup. That covers bothRedis::OPT_COMPRESSIONandredis.session.compression=zstd.This is a fallback, not a replacement:
--enable-redis-zstdstays 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
Redis::COMPRESSION_ZSTD*constants are defined,setOption()accepts zstd, sessions compress.php --ri redisshowsZstd provider => zstd extension <version>.setOption()returns false. The session notice now also suggests loading the zstd extension.zstd_compress/zstd_uncompressindisable_functionssetOption()returns false and sessions get the "not available" notice.zstd_compressFrames 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
defined('Redis::COMPRESSION_ZSTD')still says whether zstd works._MINis 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.disable_functionsis applied after MINIT and frees the entry. A function must beZEND_INTERNAL_FUNCTIONand belong to thezstdmodule. On 7.4, a disabled function keeps its module but loses its arginfo, so that is checked as well.EH_THROW, with the user error handler moved aside anderror_reportinglimited 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 inerror_get_last(). A fatal error restores everything and bails out.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.realloc() with size 0. Their development branch no longer does this.Known differences from the libzstd decoder
Neither case occurs with data phpredis itself writes:
''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
config.w32:config.w32has 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.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 withdefined()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
fix:an unknownredis.session.compressionvalue such asgzipwas told to "rebuild phpredis with gzip support" instead of being reported as invalid. The or-edstrcasecmp()calls lacked== 0, so the condition was always true. This commit stands alone.testCompressionZSTDInvalid: invalid values with a throwing error handler;error_get_last()stays empty; the empty frame with and without its checksum.testCompressionZSTDLevels.testCompressionZSTDInterop.zstd-extjob 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.Not included, for you to decide:
Tested
Run locally in Docker against Redis 8:
Not run locally: the new CI job, Windows, PHP 8.0 to 8.4.
🤖 Generated with Claude Code