Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 445b1b0

Browse files
authored
Merge pull request phpredis#1167 from yatsukhnenko/develop
Remove `ra_find_key` + use return_value instead of stack allocated z_…
2 parents 9ec9aed + 47d3722 commit 445b1b0

File tree

3 files changed

+14
-29
lines changed

3 files changed

+14
-29
lines changed

redis_array.c

+12-10
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@ PHP_METHOD(RedisArray, __construct)
345345
static void
346346
ra_forward_call(INTERNAL_FUNCTION_PARAMETERS, RedisArray *ra, const char *cmd, int cmd_len, zval *z_args, zval *z_new_target) {
347347

348-
zval z_tmp, z_fun, *redis_inst, *z_callargs, *zp_tmp;
348+
zval z_fun, *redis_inst, *z_callargs, *zp_tmp;
349349
char *key = NULL; /* set to avoid "unused-but-set-variable" */
350350
int i, key_len = 0, argc;
351351
HashTable *h_args;
@@ -358,10 +358,12 @@ ra_forward_call(INTERNAL_FUNCTION_PARAMETERS, RedisArray *ra, const char *cmd, i
358358
redis_inst = ra->z_multi_exec; /* we already have the instance */
359359
} else {
360360
/* extract key and hash it. */
361-
if(!(key = ra_find_key(ra, z_args, cmd, &key_len))) {
361+
if ((zp_tmp = zend_hash_index_find(h_args, 0)) == NULL || Z_TYPE_P(zp_tmp) != IS_STRING) {
362362
php_error_docref(NULL TSRMLS_CC, E_ERROR, "Could not find key");
363363
RETURN_FALSE;
364-
}
364+
}
365+
key = Z_STRVAL_P(zp_tmp);
366+
key_len = Z_STRLEN_P(zp_tmp);
365367

366368
/* find node */
367369
redis_inst = ra_find_node(ra, key, key_len, NULL TSRMLS_CC);
@@ -371,9 +373,6 @@ ra_forward_call(INTERNAL_FUNCTION_PARAMETERS, RedisArray *ra, const char *cmd, i
371373
}
372374
}
373375

374-
/* check if write cmd */
375-
b_write_cmd = ra_is_write_cmd(ra, cmd, cmd_len);
376-
377376
/* pass call through */
378377
ZVAL_STRINGL(&z_fun, cmd, cmd_len); /* method name */
379378
z_callargs = ecalloc(argc + 1, sizeof(zval));
@@ -387,20 +386,23 @@ ra_forward_call(INTERNAL_FUNCTION_PARAMETERS, RedisArray *ra, const char *cmd, i
387386

388387
/* multi/exec */
389388
if(ra->z_multi_exec) {
390-
call_user_function(&redis_ce->function_table, ra->z_multi_exec, &z_fun, &z_tmp, argc, z_callargs);
389+
call_user_function(&redis_ce->function_table, ra->z_multi_exec, &z_fun, return_value, argc, z_callargs);
390+
zval_dtor(return_value);
391391
zval_dtor(&z_fun);
392-
zval_dtor(&z_tmp);
393392
efree(z_callargs);
394393
RETURN_ZVAL(getThis(), 1, 0);
395394
}
396395

396+
/* check if write cmd */
397+
b_write_cmd = ra_is_write_cmd(ra, cmd, cmd_len);
398+
397399
/* CALL! */
398400
if(ra->index && b_write_cmd) {
399401
/* add MULTI + SADD */
400402
ra_index_multi(redis_inst, MULTI TSRMLS_CC);
401403
/* call using discarded temp value and extract exec results after. */
402-
call_user_function(&redis_ce->function_table, redis_inst, &z_fun, &z_tmp, argc, z_callargs);
403-
zval_dtor(&z_tmp);
404+
call_user_function(&redis_ce->function_table, redis_inst, &z_fun, return_value, argc, z_callargs);
405+
zval_dtor(return_value);
404406

405407
/* add keys to index. */
406408
ra_index_key(key, key_len, redis_inst TSRMLS_CC);

redis_array_impl.c

+2-18
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,7 @@ ra_call_extractor(RedisArray *ra, const char *key, int key_len, int *out_len TSR
401401
return NULL;
402402
}
403403

404+
ZVAL_NULL(&z_ret);
404405
/* call extraction function */
405406
ZVAL_STRINGL(&z_argv, key, key_len);
406407
call_user_function(EG(function_table), NULL, &ra->z_fun, &z_ret, 1, &z_argv);
@@ -448,6 +449,7 @@ ra_call_distributor(RedisArray *ra, const char *key, int key_len, int *pos TSRML
448449
return 0;
449450
}
450451

452+
ZVAL_NULL(&z_ret);
451453
/* call extraction function */
452454
ZVAL_STRINGL(&z_argv, key, key_len);
453455
call_user_function(EG(function_table), NULL, &ra->z_dist, &z_ret, 1, &z_argv);
@@ -513,24 +515,6 @@ ra_find_node_by_name(RedisArray *ra, const char *host, int host_len TSRMLS_DC) {
513515
return NULL;
514516
}
515517

516-
517-
char *
518-
ra_find_key(RedisArray *ra, zval *z_args, const char *cmd, int *key_len) {
519-
520-
zval *zp_tmp;
521-
int key_pos = 0; /* TODO: change this depending on the command */
522-
523-
if (zend_hash_num_elements(Z_ARRVAL_P(z_args)) == 0 ||
524-
(zp_tmp = zend_hash_index_find(Z_ARRVAL_P(z_args), key_pos)) == NULL ||
525-
Z_TYPE_P(zp_tmp) != IS_STRING
526-
) {
527-
return NULL;
528-
}
529-
530-
*key_len = Z_STRLEN_P(zp_tmp);
531-
return Z_STRVAL_P(zp_tmp);
532-
}
533-
534518
void
535519
ra_index_multi(zval *z_redis, long multi_value TSRMLS_DC) {
536520

redis_array_impl.h

-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ zval *ra_find_node(RedisArray *ra, const char *key, int key_len, int *out_pos TS
1717
void ra_init_function_table(RedisArray *ra);
1818

1919
void ra_move_key(const char *key, int key_len, zval *z_from, zval *z_to TSRMLS_DC);
20-
char * ra_find_key(RedisArray *ra, zval *z_args, const char *cmd, int *key_len);
2120
void ra_index_multi(zval *z_redis, long multi_value TSRMLS_DC);
2221

2322
void ra_index_key(const char *key, int key_len, zval *z_redis TSRMLS_DC);

0 commit comments

Comments
 (0)