Skip to content

perf: Pre-size the MSET command buffer from argument count - #2861

Open
iliaal wants to merge 1 commit into
phpredis:developfrom
iliaal:perf/presize-mset-cmd
Open

perf: Pre-size the MSET command buffer from argument count#2861
iliaal wants to merge 1 commit into
phpredis:developfrom
iliaal:perf/presize-mset-cmd

Conversation

@iliaal

@iliaal iliaal commented Jun 3, 2026

Copy link
Copy Markdown
Contributor

redis_mset_cmd builds the command into a smart_string that grows to the next 4KB page on demand, so a high-arity MSET reallocs 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 backs MSETNX; the pattern extends to ZADD/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.

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.
@michael-grunder

Copy link
Copy Markdown
Member

We might want to wait for #2847 before merging this #2864, and #2865.

We can fold the logic into the new RedisCmd api though where appropriate though.

@iliaal

iliaal commented Jun 3, 2026

Copy link
Copy Markdown
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 #.

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