You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Optimize raxLowWalk child lookup with memchr() #3386
Thank you @ahmadbelb.
Can you please attach some benchmark numbers supporting this change? also for cases where SIMD is NOT applied, so that we understand there is no introduced degradation...
@ranshid you're welcome :) I did some digging, and here are the results. It's low-hanging fruit in terms of performance.
memchr() is C89 standard. Every libc ships an optimized version, typically using SIMD on platforms that support it.
Typical Valkey Fan-out
The typical fan-out in Valkey is 10-11. The benchmark results below focus on this range.
Update: The original benchmark measured full raxFind() end-to-end, which inflated the speedup numbers since tree traversal time is constant between both versions. An isolated benchmark of just the child scan (the only code path that changed) shows more accurate results on Apple M1:
Bytes to scan
Match position
Linear
memchr
Speedup
1
hit
1.61 ns
2.53 ns
0.64x
3
first
1.26 ns
2.52 ns
0.50x
3
last
2.52 ns
2.52 ns
1.00x
10 (typical)
first
1.26 ns
2.52 ns
0.50x
10 (typical)
middle
3.54 ns
2.52 ns
1.41x
10 (typical)
last
4.73 ns
2.53 ns
1.87x
10 (typical)
miss
4.75 ns
2.24 ns
2.12x
16
middle
4.41 ns
2.52 ns
1.75x
16
miss
6.63 ns
2.20 ns
3.01x
For the typical Valkey fan-out of 10 (digits 0-9 in stream keys, confirmed by instrumenting raxLowWalk), memchr wins when the match isn't in the first 1-2 positions. When the match is at position 0, the linear scan is faster because memchr has about 2.5ns of setup overhead.
For uniformly distributed lookups across 10 children, the average speedup on the isolated scan is roughly 1.5x on M1.
@zuiderkwast As far as I understand the code so far, Streams usually like every XADD, XREAD, and XRANGE traverses the RAX and raxFind on the TrackingTable per key no ?
@dvkashapov For the record. this issue was assigned to me and I was working toward a PR. The implementation in #3472 is the same change proposed here, with no reference to this prior work. Lesson learned to move faster, but it would be good to have a process where existing assigned issues are checked before merging duplicate PRs.
@madolson can we add "Suggested-by: " line to already committed PR #3472 ? Looks like proposed change there is the same as one proposed here a month ago.
Activity
ranshid commentedon Mar 20, 2026
Thank you @ahmadbelb.
Can you please attach some benchmark numbers supporting this change? also for cases where SIMD is NOT applied, so that we understand there is no introduced degradation...
ahmadbelb commentedon Mar 20, 2026
@ranshid you're welcome :) I did some digging, and here are the results. It's low-hanging fruit in terms of performance.
memchr()is C89 standard. Every libc ships an optimized version, typically using SIMD on platforms that support it.Typical Valkey Fan-out
The typical fan-out in Valkey is 10-11. The benchmark results below focus on this range.
Update: The original benchmark measured full
raxFind()end-to-end, which inflated the speedup numbers since tree traversal time is constant between both versions. An isolated benchmark of just the child scan (the only code path that changed) shows more accurate results on Apple M1:For the typical Valkey fan-out of 10 (digits 0-9 in stream keys, confirmed by instrumenting
raxLowWalk),memchrwins when the match isn't in the first 1-2 positions. When the match is at position 0, the linear scan is faster becausememchrhas about 2.5ns of setup overhead.For uniformly distributed lookups across 10 children, the average speedup on the isolated scan is roughly 1.5x on M1.
zuiderkwast commentedon Mar 24, 2026
Which commands or other hot code paths are using raxLowWalk?
ahmadbelb commentedon Mar 24, 2026
@zuiderkwast As far as I understand the code so far, Streams usually like every XADD, XREAD, and XRANGE traverses the RAX and raxFind on the TrackingTable per key no ?
ranshid commentedon Mar 25, 2026
@ahmadbelb is there a PR for this change? I do not think that given these results there will be much objection...
dvkashapov commentedon Apr 14, 2026
Implemented in c4db208
ahmadbelb commentedon Apr 14, 2026
@dvkashapov For the record. this issue was assigned to me and I was working toward a PR. The implementation in #3472 is the same change proposed here, with no reference to this prior work. Lesson learned to move faster, but it would be good to have a process where existing assigned issues are checked before merging duplicate PRs.
dvkashapov commentedon Apr 15, 2026
@madolson can we add "Suggested-by: " line to already committed PR #3472 ? Looks like proposed change there is the same as one proposed here a month ago.
madolson commentedon Apr 30, 2026
Not easily, we would have to force push and rewrite a bunch of history including tagged releases. I would rather not touch that.