Skip to content

perf: Pre-size reply arrays where the element count is known#2855

Merged
michael-grunder merged 1 commit into
phpredis:developfrom
iliaal:perf/array-init-size
Jun 2, 2026
Merged

perf: Pre-size reply arrays where the element count is known#2855
michael-grunder merged 1 commit into
phpredis:developfrom
iliaal:perf/array-init-size

Conversation

@iliaal
Copy link
Copy Markdown
Contributor

@iliaal iliaal commented Jun 2, 2026

Several multibulk reply builders called array_init (8-bucket default) right after reading the element count off the wire, forcing HashTable resizes as elements were appended.

Switch these sites to array_init_size with the known count. The count is clamped to >= 0 because a null multibulk header yields -1 and array_init_size takes a uint32_t; array_init_size(_, 0) is equivalent to array_init, so the clamp is never worse than before. Covers redis_sock_read_multibulk_reply_zval, the LPOS COUNT path, CLIENT TRACKINGINFO, HELLO, and nested multibulk in the recursive variant reader.

Built on PHP 8.4; smoke tests exercise MULTI/EXEC and LPOS COUNT result arrays.

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.
@michael-grunder michael-grunder merged commit da514c7 into phpredis:develop Jun 2, 2026
42 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants