From 7d19e278dd820825a1a2621e7da5bac5264e3643 Mon Sep 17 00:00:00 2001 From: Rasmus Lerdorf Date: Wed, 3 May 2023 11:13:59 -0700 Subject: [PATCH 1/2] Make the compression_level at settable option and expand it to zlib as well --- php_memcached.c | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/php_memcached.c b/php_memcached.c index 9c5ba8aa..d6a0b4d3 100644 --- a/php_memcached.c +++ b/php_memcached.c @@ -884,7 +884,7 @@ zend_bool s_invoke_cache_callback(zval *zobject, zend_fcall_info *fci, zend_fcal ****************************************/ static -zend_bool s_compress_value (php_memc_compression_type compression_type, zend_string **payload_in, uint32_t *flags) +zend_bool s_compress_value (php_memc_compression_type compression_type, zend_long compression_level, zend_string **payload_in, uint32_t *flags) { /* status */ zend_bool compress_status = 0; @@ -915,7 +915,7 @@ zend_bool s_compress_value (php_memc_compression_type compression_type, zend_str #ifdef HAVE_ZSTD_H case COMPRESSION_TYPE_ZSTD: { - compressed_size = ZSTD_compress((void *)buffer, buffer_size, ZSTR_VAL(payload), ZSTR_LEN(payload), MEMC_G(compression_level)); + compressed_size = ZSTD_compress((void *)buffer, buffer_size, ZSTR_VAL(payload), ZSTR_LEN(payload), compression_level); if (!ZSTD_isError(compressed_size)) { compress_status = 1; @@ -928,7 +928,7 @@ zend_bool s_compress_value (php_memc_compression_type compression_type, zend_str case COMPRESSION_TYPE_ZLIB: { compressed_size = buffer_size; - int status = compress((Bytef *) buffer, &compressed_size, (Bytef *) ZSTR_VAL(payload), ZSTR_LEN(payload)); + int status = compress2((Bytef *) buffer, &compressed_size, (Bytef *) ZSTR_VAL(payload), ZSTR_LEN(payload), compression_level); if (status == Z_OK) { compress_status = 1; @@ -1120,7 +1120,7 @@ zend_string *s_zval_to_payload(php_memc_object_t *intern, zval *value, uint32_t * * No need to check the return value because the payload is always valid. */ - (void)s_compress_value (memc_user_data->compression_type, &payload, flags); + (void)s_compress_value (memc_user_data->compression_type, memc_user_data->compression_level, &payload, flags); } if (memc_user_data->set_udf_flags >= 0) { @@ -1325,6 +1325,7 @@ static PHP_METHOD(Memcached, __construct) memc_user_data = pecalloc (1, sizeof(*memc_user_data), is_persistent); memc_user_data->serializer = MEMC_G(serializer_type); memc_user_data->compression_type = MEMC_G(compression_type); + memc_user_data->compression_level = MEMC_G(compression_level); memc_user_data->compression_enabled = 1; memc_user_data->encoding_enabled = 0; memc_user_data->store_retry_count = MEMC_G(store_retry_count); @@ -3075,6 +3076,11 @@ int php_memc_set_option(php_memc_object_t *intern, long option, zval *value) } break; + case MEMC_OPT_COMPRESSION_LEVEL: + lval = zval_get_long(value); + memc_user_data->compression_level = lval; + break; + case MEMC_OPT_ITEM_SIZE_LIMIT: lval = zval_get_long(value); if (lval < 0) { @@ -4054,7 +4060,7 @@ PHP_GINIT_FUNCTION(php_memcached) php_memcached_globals->memc.compression_threshold = 2000; php_memcached_globals->memc.compression_type = COMPRESSION_TYPE_FASTLZ; php_memcached_globals->memc.compression_factor = 1.30; - php_memcached_globals->memc.compression_level = 3; + php_memcached_globals->memc.compression_level = 6; php_memcached_globals->memc.store_retry_count = 2; php_memcached_globals->memc.item_size_limit = 0; From 53e14b6ec550e47be88b7dd0ba8de661ff697024 Mon Sep 17 00:00:00 2001 From: Rasmus Lerdorf Date: Wed, 3 May 2023 11:25:14 -0700 Subject: [PATCH 2/2] make sure compression_level is valid for each type --- php_memcached.c | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/php_memcached.c b/php_memcached.c index d6a0b4d3..00d5e3d4 100644 --- a/php_memcached.c +++ b/php_memcached.c @@ -917,6 +917,12 @@ zend_bool s_compress_value (php_memc_compression_type compression_type, zend_lon { compressed_size = ZSTD_compress((void *)buffer, buffer_size, ZSTR_VAL(payload), ZSTR_LEN(payload), compression_level); + if (compression_level < -22) { + compression_level = -22; + } else if (compression_level > 22) { + compression_level = 22; + } + if (!ZSTD_isError(compressed_size)) { compress_status = 1; compression_type_flag = MEMC_VAL_COMPRESSION_ZSTD; @@ -928,6 +934,13 @@ zend_bool s_compress_value (php_memc_compression_type compression_type, zend_lon case COMPRESSION_TYPE_ZLIB: { compressed_size = buffer_size; + + if (compression_level < 0) { + compression_level = 0; + } else if (compression_level > 9) { + compression_level = 9; + } + int status = compress2((Bytef *) buffer, &compressed_size, (Bytef *) ZSTR_VAL(payload), ZSTR_LEN(payload), compression_level); if (status == Z_OK) {