Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 4082dd0

Browse filesBrowse files
JakubOnderkamichael-grunder
authored andcommitted
Avoid unnecessary allocation in redis_hdel_cmd
This will slightly reduce memory usage for HDEL command
1 parent 99650e1 commit 4082dd0
Copy full SHA for 4082dd0

File tree

1 file changed

+13
-38
lines changed
Filter options

1 file changed

+13
-38
lines changed

‎redis_commands.c

Copy file name to clipboardExpand all lines: redis_commands.c
+13-38Lines changed: 13 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -3867,57 +3867,32 @@ int redis_sort_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
38673867
int redis_hdel_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
38683868
char **cmd, int *cmd_len, short *slot, void **ctx)
38693869
{
3870-
zval *z_args;
38713870
smart_string cmdstr = {0};
3872-
char *arg;
3873-
int arg_free, i;
3874-
size_t arg_len;
3875-
int argc = ZEND_NUM_ARGS();
3876-
zend_string *zstr;
3877-
3878-
// We need at least KEY and one member
3879-
if (argc < 2) {
3880-
return FAILURE;
3881-
}
3882-
3883-
// Grab arguments as an array
3884-
z_args = emalloc(argc * sizeof(zval));
3885-
if (zend_get_parameters_array(ht, argc, z_args) == FAILURE) {
3886-
efree(z_args);
3887-
return FAILURE;
3888-
}
3889-
3890-
// Get first argument (the key) as a string
3891-
zstr = zval_get_string(&z_args[0]);
3892-
arg = ZSTR_VAL(zstr);
3893-
arg_len = ZSTR_LEN(zstr);
3871+
zend_string *key = NULL;
3872+
int i;
3873+
int argc = 0;
3874+
zval *args;
38943875

3895-
// Prefix
3896-
arg_free = redis_key_prefix(redis_sock, &arg, &arg_len);
3876+
ZEND_PARSE_PARAMETERS_START(2, -1)
3877+
Z_PARAM_STR(key)
3878+
Z_PARAM_VARIADIC('*', args, argc)
3879+
ZEND_PARSE_PARAMETERS_END_EX(return FAILURE);
38973880

38983881
// Start command construction
3899-
redis_cmd_init_sstr(&cmdstr, argc, ZEND_STRL("HDEL"));
3900-
redis_cmd_append_sstr(&cmdstr, arg, arg_len);
3882+
redis_cmd_init_sstr(&cmdstr, argc + 1, ZEND_STRL("HDEL"));
39013883

3902-
// Set our slot, free key if we prefixed it
3903-
CMD_SET_SLOT(slot,arg,arg_len);
3904-
zend_string_release(zstr);
3905-
if (arg_free) efree(arg);
3884+
// Append key
3885+
redis_cmd_append_sstr_key_zstr(&cmdstr, key, redis_sock, slot);
39063886

39073887
// Iterate through the members we're removing
3908-
for (i = 1; i < argc; i++) {
3909-
zstr = zval_get_string(&z_args[i]);
3910-
redis_cmd_append_sstr(&cmdstr, ZSTR_VAL(zstr), ZSTR_LEN(zstr));
3911-
zend_string_release(zstr);
3888+
for (i = 0; i < argc; i++) {
3889+
redis_cmd_append_sstr_zval(&cmdstr, &args[i], NULL);
39123890
}
39133891

39143892
// Push out values
39153893
*cmd = cmdstr.c;
39163894
*cmd_len = cmdstr.len;
39173895

3918-
// Cleanup
3919-
efree(z_args);
3920-
39213896
// Success!
39223897
return SUCCESS;
39233898
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.