Skip to content

perf: Avoid per-key heap allocation in ra_find_node hash path#2856

Merged
michael-grunder merged 1 commit into
phpredis:developfrom
iliaal:perf/ra-find-node-stack
Jun 2, 2026
Merged

perf: Avoid per-key heap allocation in ra_find_node hash path#2856
michael-grunder merged 1 commit into
phpredis:developfrom
iliaal:perf/ra-find-node-stack

Conversation

@iliaal
Copy link
Copy Markdown
Contributor

@iliaal iliaal commented Jun 2, 2026

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).

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.

Built on PHP 8.4; runtime-tested with a RedisArray under md5 and sha256, 200 keys round-tripping correctly.

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

@michael-grunder michael-grunder left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good use of this trick.

@michael-grunder michael-grunder merged commit 8b746bf into phpredis:develop Jun 2, 2026
83 of 84 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