From 1a3212a9e258737655e7ee6042fafba522ca372b Mon Sep 17 00:00:00 2001 From: Paavo-Einari Kaipila Date: Sat, 30 May 2026 09:14:03 +0300 Subject: [PATCH] configurable compression level for apcu --- php_zstd.h | 9 +++++++-- tests/apcu_serializer.phpt | 27 +++++++++++++++++++++++++++ zstd.c | 34 ++++++++++++++++++++-------------- 3 files changed, 54 insertions(+), 16 deletions(-) diff --git a/php_zstd.h b/php_zstd.h index d9fad27..0567550 100644 --- a/php_zstd.h +++ b/php_zstd.h @@ -44,8 +44,11 @@ extern zend_module_entry zstd_module_entry; typedef struct _php_zstd_context php_zstd_context; -#if PHP_VERSION_ID >= 80000 ZEND_BEGIN_MODULE_GLOBALS(zstd) +#if defined(HAVE_APCU_SUPPORT) + zend_long apcu_compression_level; +#endif +#if PHP_VERSION_ID >= 80000 zend_long output_compression; zend_long output_compression_default; zend_long output_compression_level; @@ -53,8 +56,10 @@ ZEND_BEGIN_MODULE_GLOBALS(zstd) php_zstd_context *ob_handler; bool handler_registered; int compression_coding; -ZEND_END_MODULE_GLOBALS(zstd); +#else + int unused_dummy; /* make coderabbit happy */ #endif +ZEND_END_MODULE_GLOBALS(zstd); #ifdef ZTS #define PHP_ZSTD_G(v) TSRMG(zstd_globals_id, zend_zstd_globals *, v) diff --git a/tests/apcu_serializer.phpt b/tests/apcu_serializer.phpt index 193672d..cdb9360 100644 --- a/tests/apcu_serializer.phpt +++ b/tests/apcu_serializer.phpt @@ -61,6 +61,32 @@ var_dump($unserialized); if ($unserialized[0] === $unserialized[1]) { echo "SAME\n"; } + +function getEntrySize(string $key) { + $info = apcu_cache_info(); + if (!is_array($info) || !isset($info['cache_list']) || !is_array($info['cache_list'])) { + return null; + } + foreach($info['cache_list'] as $entry) { + if (($entry['info'] ?? null) === $key) { + return $entry['mem_size']; + } + } + return null; +} +include(dirname(__FILE__) . '/data.inc'); + +ini_set('zstd.apcu_compression_level', 3); +apcu_store('size_test', [$data]); +$a = getEntrySize('size_test'); + +ini_set('zstd.apcu_compression_level', 19); +apcu_store('size_test', [$data]); +$b = getEntrySize('size_test'); + +if ($a !== null && $b !== null && $b < $a) { + echo "SMALLER\n"; +} ?> --EXPECTF-- zstd @@ -100,3 +126,4 @@ array(2) { } } SAME +SMALLER diff --git a/zstd.c b/zstd.c index 136eb79..3942421 100644 --- a/zstd.c +++ b/zstd.c @@ -432,9 +432,9 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_ob_zstd_handler, 0, 0, 2) ZEND_ARG_INFO(0, flags) ZEND_END_ARG_INFO() #endif +#endif ZEND_DECLARE_MODULE_GLOBALS(zstd); -#endif #ifndef Z_PARAM_STR_OR_NULL #define Z_PARAM_STR_OR_NULL(dest) Z_PARAM_STR_EX(dest, 1, 0) @@ -1182,6 +1182,11 @@ static int APC_SERIALIZER_NAME(zstd)(APC_SERIALIZER_ARGS) php_serialize_data_t var_hash; size_t size; smart_str var = {0}; + int level = PHP_ZSTD_G(apcu_compression_level); + + if (!zstd_check_compress_level(level) || level == 0) { + level = ZSTD_CLEVEL_DEFAULT; + } PHP_VAR_SERIALIZE_INIT(var_hash); php_var_serialize(&var, (zval*) value, &var_hash); @@ -1194,7 +1199,7 @@ static int APC_SERIALIZER_NAME(zstd)(APC_SERIALIZER_ARGS) *buf = emalloc(size + 1); *buf_len = ZSTD_compress(*buf, size, ZSTR_VAL(var.s), ZSTR_LEN(var.s), - ZSTD_CLEVEL_DEFAULT); + level); if (ZSTD_isError(*buf_len) || *buf_len == 0) { efree(*buf); *buf = NULL; @@ -1716,24 +1721,32 @@ static PHP_INI_MH(OnUpdate_zstd_output_compression) return SUCCESS; } +#endif #define STRINGIFY(n) #n #define TOSTRING(n) STRINGIFY(n) PHP_INI_BEGIN() - STD_PHP_INI_BOOLEAN("zstd.output_compression", "0", +#if PHP_VERSION_ID >= 80000 + STD_PHP_INI_BOOLEAN("zstd.output_compression", "0", PHP_INI_ALL, OnUpdate_zstd_output_compression, output_compression_default, zend_zstd_globals, zstd_globals) - STD_PHP_INI_ENTRY("zstd.output_compression_level", + STD_PHP_INI_ENTRY("zstd.output_compression_level", TOSTRING(ZSTD_CLEVEL_DEFAULT), PHP_INI_ALL, OnUpdateLong, output_compression_level, zend_zstd_globals, zstd_globals) - STD_PHP_INI_ENTRY("zstd.output_compression_dict", "", + STD_PHP_INI_ENTRY("zstd.output_compression_dict", "", PHP_INI_ALL, OnUpdateString, output_compression_dict, zend_zstd_globals, zstd_globals) -PHP_INI_END() #endif +#if defined(HAVE_APCU_SUPPORT) + STD_PHP_INI_ENTRY("zstd.apcu_compression_level", + TOSTRING(ZSTD_CLEVEL_DEFAULT), + PHP_INI_ALL, OnUpdateLong, apcu_compression_level, + zend_zstd_globals, zstd_globals) +#endif +PHP_INI_END() ZEND_MINIT_FUNCTION(zstd) { @@ -1827,18 +1840,14 @@ ZEND_MINIT_FUNCTION(zstd) php_output_handler_conflict_register( ZEND_STRL(PHP_ZSTD_OUTPUT_HANDLER_NAME), php_zstd_output_conflict_check); - - REGISTER_INI_ENTRIES(); #endif - + REGISTER_INI_ENTRIES(); return SUCCESS; } ZEND_MSHUTDOWN_FUNCTION(zstd) { -#if PHP_VERSION_ID >= 80000 UNREGISTER_INI_ENTRIES(); -#endif return SUCCESS; } @@ -1876,10 +1885,7 @@ ZEND_MINFO_FUNCTION(zstd) php_info_print_table_row(2, "APCu serializer ABI", APC_SERIALIZER_ABI); #endif php_info_print_table_end(); - -#if PHP_VERSION_ID >= 80000 DISPLAY_INI_ENTRIES(); -#endif } #if PHP_VERSION_ID >= 80000