Skip to content

Commit 0c7644b

Browse files
authored
Merge pull request #96 from pavetheway91/apcu_level
configurable apcu compression level
2 parents 5942b5d + 02f264a commit 0c7644b

3 files changed

Lines changed: 56 additions & 22 deletions

File tree

php_zstd.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,17 +44,22 @@ extern zend_module_entry zstd_module_entry;
4444

4545
typedef struct _php_zstd_context php_zstd_context;
4646

47-
#if PHP_VERSION_ID >= 80000
4847
ZEND_BEGIN_MODULE_GLOBALS(zstd)
48+
#if defined(HAVE_APCU_SUPPORT)
49+
zend_long apcu_compression_level;
50+
#endif
51+
#if PHP_VERSION_ID >= 80000
4952
zend_long output_compression;
5053
zend_long output_compression_default;
5154
zend_long output_compression_level;
5255
char *output_compression_dict;
5356
php_zstd_context *ob_handler;
5457
bool handler_registered;
5558
int compression_coding;
56-
ZEND_END_MODULE_GLOBALS(zstd);
59+
#else
60+
int unused_dummy; /* no empty struct */
5761
#endif
62+
ZEND_END_MODULE_GLOBALS(zstd);
5863

5964
#ifdef ZTS
6065
#define PHP_ZSTD_G(v) TSRMG(zstd_globals_id, zend_zstd_globals *, v)

tests/apcu_serializer.phpt

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,32 @@ var_dump($unserialized);
6161
if ($unserialized[0] === $unserialized[1]) {
6262
echo "SAME\n";
6363
}
64+
65+
function getEntrySize(string $key) {
66+
$info = apcu_cache_info();
67+
if (!is_array($info) || !isset($info['cache_list']) || !is_array($info['cache_list'])) {
68+
return null;
69+
}
70+
foreach($info['cache_list'] as $entry) {
71+
if (($entry['info'] ?? null) === $key) {
72+
return $entry['mem_size'];
73+
}
74+
}
75+
return null;
76+
}
77+
include(dirname(__FILE__) . '/data.inc');
78+
79+
ini_set('zstd.apcu_compression_level', 3);
80+
apcu_store('size_test', [$data]);
81+
$a = getEntrySize('size_test');
82+
83+
ini_set('zstd.apcu_compression_level', 19);
84+
apcu_store('size_test', [$data]);
85+
$b = getEntrySize('size_test');
86+
87+
if ($a !== null && $b !== null && $b < $a) {
88+
echo "SMALLER\n";
89+
}
6490
?>
6591
--EXPECTF--
6692
zstd
@@ -100,3 +126,4 @@ array(2) {
100126
}
101127
}
102128
SAME
129+
SMALLER

zstd.c

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -432,9 +432,9 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_ob_zstd_handler, 0, 0, 2)
432432
ZEND_ARG_INFO(0, flags)
433433
ZEND_END_ARG_INFO()
434434
#endif
435+
#endif
435436

436437
ZEND_DECLARE_MODULE_GLOBALS(zstd);
437-
#endif
438438

439439
#ifndef Z_PARAM_STR_OR_NULL
440440
#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)
11821182
php_serialize_data_t var_hash;
11831183
size_t size;
11841184
smart_str var = {0};
1185+
int level = PHP_ZSTD_G(apcu_compression_level);
1186+
1187+
if (!zstd_check_compress_level(level) || level == 0) {
1188+
level = ZSTD_CLEVEL_DEFAULT;
1189+
}
11851190

11861191
PHP_VAR_SERIALIZE_INIT(var_hash);
11871192
php_var_serialize(&var, (zval*) value, &var_hash);
@@ -1194,7 +1199,7 @@ static int APC_SERIALIZER_NAME(zstd)(APC_SERIALIZER_ARGS)
11941199
*buf = emalloc(size + 1);
11951200

11961201
*buf_len = ZSTD_compress(*buf, size, ZSTR_VAL(var.s), ZSTR_LEN(var.s),
1197-
ZSTD_CLEVEL_DEFAULT);
1202+
level);
11981203
if (ZSTD_isError(*buf_len) || *buf_len == 0) {
11991204
efree(*buf);
12001205
*buf = NULL;
@@ -1716,24 +1721,32 @@ static PHP_INI_MH(OnUpdate_zstd_output_compression)
17161721

17171722
return SUCCESS;
17181723
}
1724+
#endif
17191725

17201726
#define STRINGIFY(n) #n
17211727
#define TOSTRING(n) STRINGIFY(n)
17221728

17231729
PHP_INI_BEGIN()
1724-
STD_PHP_INI_BOOLEAN("zstd.output_compression", "0",
1730+
#if PHP_VERSION_ID >= 80000
1731+
STD_PHP_INI_BOOLEAN("zstd.output_compression", "0",
17251732
PHP_INI_ALL, OnUpdate_zstd_output_compression,
17261733
output_compression_default,
17271734
zend_zstd_globals, zstd_globals)
1728-
STD_PHP_INI_ENTRY("zstd.output_compression_level",
1735+
STD_PHP_INI_ENTRY("zstd.output_compression_level",
17291736
TOSTRING(ZSTD_CLEVEL_DEFAULT),
17301737
PHP_INI_ALL, OnUpdateLong, output_compression_level,
17311738
zend_zstd_globals, zstd_globals)
1732-
STD_PHP_INI_ENTRY("zstd.output_compression_dict", "",
1739+
STD_PHP_INI_ENTRY("zstd.output_compression_dict", "",
17331740
PHP_INI_ALL, OnUpdateString, output_compression_dict,
17341741
zend_zstd_globals, zstd_globals)
1735-
PHP_INI_END()
17361742
#endif
1743+
#if defined(HAVE_APCU_SUPPORT)
1744+
STD_PHP_INI_ENTRY("zstd.apcu_compression_level",
1745+
TOSTRING(ZSTD_CLEVEL_DEFAULT),
1746+
PHP_INI_ALL, OnUpdateLong, apcu_compression_level,
1747+
zend_zstd_globals, zstd_globals)
1748+
#endif
1749+
PHP_INI_END()
17371750

17381751
ZEND_MINIT_FUNCTION(zstd)
17391752
{
@@ -1827,18 +1840,14 @@ ZEND_MINIT_FUNCTION(zstd)
18271840
php_output_handler_conflict_register(
18281841
ZEND_STRL(PHP_ZSTD_OUTPUT_HANDLER_NAME),
18291842
php_zstd_output_conflict_check);
1830-
1831-
REGISTER_INI_ENTRIES();
18321843
#endif
1833-
1844+
REGISTER_INI_ENTRIES();
18341845
return SUCCESS;
18351846
}
18361847

18371848
ZEND_MSHUTDOWN_FUNCTION(zstd)
18381849
{
1839-
#if PHP_VERSION_ID >= 80000
18401850
UNREGISTER_INI_ENTRIES();
1841-
#endif
18421851
return SUCCESS;
18431852
}
18441853

@@ -1876,22 +1885,19 @@ ZEND_MINFO_FUNCTION(zstd)
18761885
php_info_print_table_row(2, "APCu serializer ABI", APC_SERIALIZER_ABI);
18771886
#endif
18781887
php_info_print_table_end();
1879-
1880-
#if PHP_VERSION_ID >= 80000
18811888
DISPLAY_INI_ENTRIES();
1882-
#endif
18831889
}
18841890

1885-
#if PHP_VERSION_ID >= 80000
18861891
ZEND_GINIT_FUNCTION(zstd)
18871892
{
18881893
#if defined(COMPILE_DL_ZSTD) && defined(ZTS)
18891894
ZEND_TSRMLS_CACHE_UPDATE();
18901895
#endif
1896+
#if PHP_VERSION_ID >= 80000
18911897
zstd_globals->ob_handler = NULL;
18921898
zstd_globals->handler_registered = 0;
1893-
}
18941899
#endif
1900+
}
18951901

18961902
static zend_function_entry zstd_functions[] = {
18971903
ZEND_FE(zstd_compress, arginfo_zstd_compress)
@@ -1972,15 +1978,11 @@ zend_module_entry zstd_module_entry = {
19721978
ZEND_RSHUTDOWN(zstd),
19731979
ZEND_MINFO(zstd),
19741980
PHP_ZSTD_VERSION,
1975-
#if PHP_VERSION_ID >= 80000
19761981
PHP_MODULE_GLOBALS(zstd),
19771982
PHP_GINIT(zstd),
19781983
NULL,
19791984
NULL,
19801985
STANDARD_MODULE_PROPERTIES_EX
1981-
#else
1982-
STANDARD_MODULE_PROPERTIES
1983-
#endif
19841986
};
19851987

19861988
#ifdef COMPILE_DL_ZSTD

0 commit comments

Comments
 (0)