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 99beb92

Browse filesBrowse files
JakubOnderkamichael-grunder
authored andcommitted
Initialize arrays with known size
We know in advance the array size, so it makes sense to avoid reallocation when adding new elements. Also use immutable empty array in when we know in advance that redis will return zero elements.
1 parent b665925 commit 99beb92
Copy full SHA for 99beb92

File tree

1 file changed

+15
-9
lines changed
Filter options

1 file changed

+15
-9
lines changed

‎library.c

Copy file name to clipboardExpand all lines: library.c
+15-9Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3350,8 +3350,10 @@ PHP_REDIS_API int redis_sock_read_multibulk_reply(INTERNAL_FUNCTION_PARAMETERS,
33503350
}
33513351
if (numElems == -1 && redis_sock->null_mbulk_as_null) {
33523352
ZVAL_NULL(&z_multi_result);
3353+
} else if (numElems < 1) {
3354+
ZVAL_EMPTY_ARRAY(&z_multi_result);
33533355
} else {
3354-
array_init(&z_multi_result);
3356+
array_init_size(&z_multi_result, numElems);
33553357
redis_mbulk_reply_loop(redis_sock, &z_multi_result, numElems, UNSERIALIZE_ALL);
33563358
}
33573359

@@ -3380,7 +3382,7 @@ redis_mbulk_reply_raw(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock, zval
33803382
return FAILURE;
33813383
}
33823384
zval z_multi_result;
3383-
array_init(&z_multi_result); /* pre-allocate array for multi's results. */
3385+
array_init_size(&z_multi_result, numElems); /* pre-allocate array for multi's results. */
33843386

33853387
redis_mbulk_reply_loop(redis_sock, &z_multi_result, numElems, UNSERIALIZE_NONE);
33863388

@@ -3409,14 +3411,18 @@ redis_mbulk_reply_double(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock, zv
34093411
return FAILURE;
34103412
}
34113413

3412-
array_init(&z_multi_result);
3413-
for (i = 0; i < numElems; ++i) {
3414-
if ((line = redis_sock_read(redis_sock, &len)) == NULL) {
3415-
add_next_index_bool(&z_multi_result, 0);
3416-
continue;
3414+
if (numElems < 1) {
3415+
ZVAL_EMPTY_ARRAY(&z_multi_result);
3416+
} else {
3417+
array_init_size(&z_multi_result, numElems);
3418+
for (i = 0; i < numElems; ++i) {
3419+
if ((line = redis_sock_read(redis_sock, &len)) == NULL) {
3420+
add_next_index_bool(&z_multi_result, 0);
3421+
continue;
3422+
}
3423+
add_next_index_double(&z_multi_result, atof(line));
3424+
efree(line);
34173425
}
3418-
add_next_index_double(&z_multi_result, atof(line));
3419-
efree(line);
34203426
}
34213427

34223428
if (IS_ATOMIC(redis_sock)) {

0 commit comments

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