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

Skip to content

Commit da514c7

Browse files
iliaalmichael-grunder
authored andcommitted
perf: Pre-size reply arrays where the element count is known
Several multibulk reply builders called array_init (8-bucket default) right after reading the element count off the wire, forcing one or more HashTable resizes as elements were appended. Switch these sites to array_init_size using the known count. The count is clamped to >= 0 because a null multibulk header yields -1, and array_init_size takes a uint32_t; a negative value would otherwise request a huge table. array_init_size(_, 0) is equivalent to array_init, so the clamp is never worse than the prior behavior. Covers redis_sock_read_multibulk_reply_zval, the LPOS COUNT path, CLIENT TRACKINGINFO, HELLO, and nested multibulk in the recursive variant reader.
1 parent 44f4944 commit da514c7

1 file changed

Lines changed: 5 additions & 5 deletions

File tree

library.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -762,7 +762,7 @@ redis_sock_read_multibulk_reply_zval(RedisSock *redis_sock, zval *z_tab)
762762
ZVAL_NULL(z_tab);
763763
return NULL;
764764
}
765-
array_init(z_tab);
765+
array_init_size(z_tab, numElems > 0 ? numElems : 0);
766766
redis_mbulk_reply_loop(redis_sock, z_tab, numElems, UNSERIALIZE_ALL);
767767

768768
return z_tab;
@@ -1630,7 +1630,7 @@ redis_read_lpos_response(zval *zdst, RedisSock *redis_sock, char reply_type,
16301630
if (reply_type != TYPE_MULTIBULK)
16311631
return FAILURE;
16321632

1633-
array_init(zdst);
1633+
array_init_size(zdst, elements > 0 ? elements : 0);
16341634

16351635
for (i = 0; i < elements; ++i) {
16361636
if (redis_sock_gets(redis_sock, inbuf, sizeof(inbuf), &len) < 0) {
@@ -2060,7 +2060,7 @@ redis_client_trackinginfo_reply(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_s
20602060
return FAILURE;
20612061
}
20622062

2063-
array_init(&z_ret);
2063+
array_init_size(&z_ret, numElems > 0 ? numElems : 0);
20642064
redis_read_multibulk_recursive(redis_sock, numElems, 0, &z_ret);
20652065
array_zip_values_and_scores(redis_sock, &z_ret, 0);
20662066

@@ -2100,7 +2100,7 @@ redis_hello_response(INTERNAL_FUNCTION_PARAMETERS,
21002100
if (read_mbulk_header(redis_sock, &numElems) < 0)
21012101
goto fail;
21022102

2103-
array_init(&z_ret);
2103+
array_init_size(&z_ret, numElems > 0 ? numElems : 0);
21042104

21052105
if (redis_read_multibulk_recursive(redis_sock, numElems, 0, &z_ret) != SUCCESS ||
21062106
array_zip_values_recursive(&z_ret) != SUCCESS)
@@ -4716,7 +4716,7 @@ redis_read_multibulk_recursive(RedisSock *redis_sock, long long elements, int st
47164716
if (reply_info < 0 && redis_sock->null_mbulk_as_null) {
47174717
add_next_index_null(z_ret);
47184718
} else {
4719-
array_init(&z_subelem);
4719+
array_init_size(&z_subelem, reply_info > 0 ? reply_info : 0);
47204720
if (reply_info > 0) {
47214721
redis_read_multibulk_recursive(redis_sock, reply_info, status_strings, &z_subelem);
47224722
}

0 commit comments

Comments
 (0)