perf: Pre-size the MSET command buffer from argument count - #2861
Open
iliaal wants to merge 1 commit into
Open
Conversation
redis_mset_cmd built the command into a smart_string that starts at 256 bytes and grows to the next 4KB page on demand, so a high-arity MSET realloc'd (and recopied) the buffer once per ~4KB produced. The pair count is known up front, so reserve the buffer before the append loop. A cheap pre-pass sums each field's RESP framing, the configured key prefix, and the value length when it is already a string (non-strings get a fixed allowance). The estimate only reads types and lengths, so it cannot change the emitted bytes; under-estimating just falls back to the normal growth path. Same builder backs MSETNX. Verified on PHP 8.4: small and 2000-pair MSET round-trips, mixed-type and prefixed keys, serialized values that exceed the estimate (growth fallback), MSETNX, no leaks under report_memleaks, and testSetGet/ testMSetNX/testHashes.
Member
Contributor
Author
|
Yeah, make sense, there are few more items, I am just pulling things out of a local tree and trying to keep PRs small, hence the #. |
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.
redis_mset_cmdbuilds the command into asmart_stringthat grows to the next 4KB page on demand, so a high-arityMSETreallocs and recopies once per ~4KB. The pair count is known up front, so a cheap length-only pre-pass reserves the buffer before the append loop. The estimate only reads types/lengths, so it can't change the emitted bytes; under-estimating just falls back to normal growth. Same builder backsMSETNX; the pattern extends toZADD/HMSET/variadic-key commands if wanted.Verified on PHP 8.4: small and 2000-pair MSET round-trips, mixed types, prefix, serializer growth-fallback, MSETNX, no leaks, and testSetGet/testMSetNX/testHashes pass.