perf: Avoid per-key heap allocation in ra_find_node hash path#2856
Merged
Conversation
When a RedisArray uses a custom hash algorithm, ra_find_node allocated the hash context and digest buffers on the heap for every key lookup. Both are small (the largest common context is SHA-512 at ~208 bytes), so use stack buffers for the common case and fall back to emalloc only when an algorithm's context or digest exceeds them. The context buffer is a union with a double member to guarantee the alignment the context structs require for their uint64_t state.
michael-grunder
approved these changes
Jun 2, 2026
Member
michael-grunder
left a comment
There was a problem hiding this comment.
Good use of this trick.
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.
When a RedisArray uses a custom hash algorithm,
ra_find_nodeallocated the hash context and digest buffers on the heap for every key lookup. Both are small (the largest common context is SHA-512 at ~208 bytes).Use stack buffers for the common case and fall back to
emalloconly when an algorithm's context or digest exceeds them. The context buffer is a union with adoublemember to guarantee the alignment the context structs require for theiruint64_tstate.Built on PHP 8.4; runtime-tested with a RedisArray under md5 and sha256, 200 keys round-tripping correctly.