8000 Do not allocate empty string or string with one character · phpredis/phpredis@64da891 · GitHub
[go: up one dir, main page]

Skip to content

Commit 64da891

Browse files
JakubOnderkamichael-grunder
authored andcommitted
Do not allocate empty string or string with one character
From PHP 8, empty strings or string with one character don't need to be allocated. This change will reduce memory usage.
1 parent 99beb92 commit 64da891

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

library.c

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,13 @@ void redis_register_persistent_resource(zend_string *id, void *ptr, int le_id) {
103103
zend_register_persistent_resource(ZSTR_VAL(id), ZSTR_LEN(id), ptr, le_id);
104104
}
105105

106+
/* Do not allocate empty string or string with one character */
107+
static zend_always_inline void redis_add_next_index_stringl(zval *arg, const char *str, size_t length) {
108+
zval tmp;
109+
ZVAL_STRINGL_FAST(&tmp, str, length);
110+
zend_hash_next_index_insert(Z_ARRVAL_P(arg), &tmp);
111+
}
112+
106113
static ConnectionPool *
107114
redis_sock_get_connection_pool(RedisSock *redis_sock)
108115
{
@@ -2666,14 +2673,14 @@ PHP_REDIS_API int redis_string_response(INTERNAL_FUNCTION_PARAMETERS, RedisSock
26662673
}
26672674
if (IS_ATOMIC(redis_sock)) {
26682675
if (!redis_unpack(redis_sock, response, response_len, return_value)) {
2669-
RETVAL_STRINGL(response, response_len);
2676+
RETVAL_STRINGL_FAST(response, response_len);
26702677
}
26712678
} else {
26722679
zval z_unpacked;
26732680
if (redis_unpack(redis_sock, response, response_len, &z_unpacked)) {
26742681
add_next_index_zval(z_tab, &z_unpacked);
26752682
} else {
2676-
add_next_index_stringl(z_tab, response, response_len);
2683+
redis_add_next_index_stringl(z_tab, response, response_len);
26772684
}
26782685
}
26792686

@@ -3460,7 +3467,7 @@ redis_mbulk_reply_loop(RedisSock *redis_sock, zval *z_tab, int count,
34603467
if (unwrap && redis_unpack(redis_sock, line, len, &z_unpacked)) {
34613468
add_next_index_zval(z_tab, &z_unpacked);
34623469
} else {
3463-
add_next_index_stringl(z_tab, line, len);
3470+
redis_add_next_index_stringl(z_tab, line, len);
34643471
}
34653472
efree(line);
34663473
}
@@ -3882,7 +3889,7 @@ redis_unpack(RedisSock *redis_sock, const char *src, int srclen, zval *zdst) {
38823889
/* Uncompress, then unserialize */
38833890
if (redis_uncompress(redis_sock, &buf, &len, src, srclen)) {
38843891
if (!redis_unserialize(redis_sock, buf, len, zdst)) {
3885-
ZVAL_STRINGL(zdst, buf, len);
3892+
ZVAL_STRINGL_FAST(zdst, buf, len);
38863893
}
38873894
efree(buf);
38883895
return 1;

library.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@
2929
/* use RedisException when ValueError not available */
3030
#define REDIS_VALUE_EXCEPTION(m) REDIS_THROW_EXCEPTION(m, 0)
3131
#define RETURN_THROWS() RETURN_FALSE
32+
/* ZVAL_STRINGL_FAST and RETVAL_STRINGL_FAST macros are supported since PHP 8 */
33+
#define ZVAL_STRINGL_FAST(z, s, l) ZVAL_STRINGL(z, s, l)
34+
#define RETVAL_STRINGL_FAST(s, l) RETVAL_STRINGL(s, l)
3235
#else
3336
#define redis_hash_fetch_ops(zstr) php_hash_fetch_ops(zstr)
3437

0 commit comments

Comments
 (0)
0