perf: Pre-size reply arrays where the element count is known#2855
Merged
Conversation
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
approved these changes
Jun 2, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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_sizewith the known count. The count is clamped to>= 0because a null multibulk header yields-1andarray_init_sizetakes auint32_t;array_init_size(_, 0)is equivalent toarray_init, so the clamp is never worse than before. Coversredis_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.