Skip to content

Commit d8e9689

Browse files
authored
Merge pull request phpredis#1128 from yatsukhnenko/develop
refactoring
2 parents 9310b25 + a070a43 commit d8e9689

File tree

3 files changed

+26
-48
lines changed

3 files changed

+26
-48
lines changed

common.h

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ typedef struct {
2222
char *val;
2323
} zend_string;
2424

25-
2625
#define zend_string_release(s) do { \
2726
if ((s) && (s)->gc) { \
2827
if ((s)->gc & 0x10 && (s)->val) efree((s)->val); \
@@ -475,18 +474,15 @@ typedef enum _PUBSUB_TYPE {
475474
#define IF_NOT_PIPELINE() if (redis_sock->mode != PIPELINE)
476475

477476
#define PIPELINE_ENQUEUE_COMMAND(cmd, cmd_len) do { \
478-
request_item *tmp = malloc(sizeof(request_item)); \
479-
tmp->request_str = calloc(cmd_len, 1);\
480-
memcpy(tmp->request_str, cmd, cmd_len);\
481-
tmp->request_size = cmd_len;\
482-
tmp->next = NULL;\
483-
if (redis_sock->pipeline_current) { \
484-
redis_sock->pipeline_current->next = tmp; \
485-
} \
486-
redis_sock->pipeline_current = tmp; \
487-
if(NULL == redis_sock->pipeline_head) { \
488-
redis_sock->pipeline_head = redis_sock->pipeline_current;\
477+
if (redis_sock->pipeline_cmd == NULL) { \
478+
redis_sock->pipeline_cmd = estrndup(cmd, cmd_len); \
479+
} else { \
480+
redis_sock->pipeline_cmd = erealloc(redis_sock->pipeline_cmd, \
481+
redis_sock->pipeline_len + cmd_len); \
482+
memcpy(&redis_sock->pipeline_cmd[redis_sock->pipeline_len], \
483+
cmd, cmd_len); \
489484
} \
485+
redis_sock->pipeline_len += cmd_len; \
490486
} while (0)
491487

492488
#define SOCKET_WRITE_COMMAND(redis_sock, cmd, cmd_len) \
@@ -633,8 +629,8 @@ typedef struct {
633629
fold_item *head;
634630
fold_item *current;
635631

636-
request_item *pipeline_head;
637-
request_item *pipeline_current;
632+
char *pipeline_cmd;
633+
size_t pipeline_len;
638634

639635
char *err;
640636
int err_len;

library.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1522,8 +1522,9 @@ redis_sock_create(char *host, int host_len, unsigned short port, double timeout,
15221522
redis_sock->mode = ATOMIC;
15231523
redis_sock->head = NULL;
15241524
redis_sock->current = NULL;
1525-
redis_sock->pipeline_head = NULL;
1526-
redis_sock->pipeline_current = NULL;
1525+
1526+
redis_sock->pipeline_cmd = NULL;
1527+
redis_sock->pipeline_len = 0;
15271528

15281529
redis_sock->err = NULL;
15291530
redis_sock->err_len = 0;
@@ -1939,6 +1940,9 @@ PHP_REDIS_API void redis_free_socket(RedisSock *redis_sock)
19391940
if(redis_sock->prefix) {
19401941
efree(redis_sock->prefix);
19411942
}
1943+
if (redis_sock->pipeline_cmd) {
1944+
efree(redis_sock->pipeline_cmd);
1945+
}
19421946
if(redis_sock->err) {
19431947
efree(redis_sock->err);
19441948
}

redis.c

Lines changed: 10 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -429,7 +429,6 @@ static void
429429
free_reply_callbacks(RedisSock *redis_sock)
430430
{
431431
fold_item *fi;
432-
request_item *ri;
433432

434433
for (fi = redis_sock->head; fi; ) {
435434
fold_item *fi_next = fi->next;
@@ -438,15 +437,6 @@ free_reply_callbacks(RedisSock *redis_sock)
438437
}
439438
redis_sock->head = NULL;
440439
redis_sock->current = NULL;
441-
442-
for (ri = redis_sock->pipeline_head; ri; ) {
443-
struct request_item *ri_next = ri->next;
444-
free(ri->request_str);
445-
free(ri);
446-
ri = ri_next;
447-
}
448-
redis_sock->pipeline_head = NULL;
449-
redis_sock->pipeline_current = NULL;
450440
}
451441

452442
#if (PHP_MAJOR_VERSION < 7)
@@ -2394,36 +2384,24 @@ PHP_METHOD(Redis, exec)
23942384
}
23952385

23962386
IF_PIPELINE() {
2397-
char *request = NULL;
2398-
int total = 0, offset = 0;
2399-
struct request_item *ri;
2400-
2401-
/* compute the total request size */
2402-
for(ri = redis_sock->pipeline_head; ri; ri = ri->next) {
2403-
total += ri->request_size;
2404-
}
2405-
if (total) {
2406-
request = emalloc(total + 1);
2407-
/* concatenate individual elements one by one in the target buffer */
2408-
for (ri = redis_sock->pipeline_head; ri; ri = ri->next) {
2409-
memcpy(request + offset, ri->request_str, ri->request_size);
2410-
offset += ri->request_size;
2411-
}
2412-
request[total] = '\0';
2413-
if (redis_sock_write(redis_sock, request, total TSRMLS_CC) < 0) {
2387+
if (redis_sock->pipeline_cmd == NULL) {
2388+
/* Empty array when no command was run. */
2389+
array_init(return_value);
2390+
} else {
2391+
if (redis_sock_write(redis_sock, redis_sock->pipeline_cmd,
2392+
redis_sock->pipeline_len TSRMLS_CC) < 0) {
24142393
ZVAL_FALSE(return_value);
24152394
} else {
24162395
array_init(return_value);
24172396
redis_sock_read_multibulk_multi_reply_loop(
24182397
INTERNAL_FUNCTION_PARAM_PASSTHRU, redis_sock, return_value, 0);
24192398
}
2420-
efree(request);
2421-
} else {
2422-
/* Empty array when no command was run. */
2423-
array_init(return_value);
2399+
efree(redis_sock->pipeline_cmd);
2400+
redis_sock->pipeline_cmd = NULL;
2401+
redis_sock->pipeline_len = 0;
24242402
}
2425-
redis_sock->mode = ATOMIC;
24262403
free_reply_callbacks(redis_sock);
2404+
redis_sock->mode = ATOMIC;
24272405
}
24282406
}
24292407

0 commit comments

Comments
 (0)