Skip to content

Optimize raxLowWalk child lookup with memchr() #3386

Closed

Description

@ahmadbelb

Replace the hand written linear scan in raxLowWalk() (rax.c:463-466)with memchr(), which uses SIMD optimized byte search internally on most platforms

Current code

for (j = 0; j < h->size; j++) {
    if (v[j] == s[i]) break;
}
if (j == h->size) break;
i++;

Proposed change

unsigned char *found = memchr(v, s[i], h->size);
if (!found) {
    j = h->size;
    break;
}
j = found - v;
i++;

Activity

  1. added
    bug
    on Mar 19, 2026
  2. added
    enhancement
    and removed
    bug
    on Mar 20, 2026
  3. ranshid commented on Mar 20, 2026

    @ranshid
    Member

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

  4. ahmadbelb commented on Mar 20, 2026

    @ahmadbelb
    ContributorAuthor

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

  5. zuiderkwast commented on Mar 24, 2026

    @zuiderkwast
    Contributor

    Which commands or other hot code paths are using raxLowWalk?

  6. ahmadbelb commented on Mar 24, 2026

    @ahmadbelb
    ContributorAuthor

    @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 ?

  7. ranshid commented on Mar 25, 2026

    @ranshid
    Member

    @ahmadbelb is there a PR for this change? I do not think that given these results there will be much objection...

  8. dvkashapov commented on Apr 14, 2026

    @dvkashapov
    Member

    Implemented in c4db208

  9. ahmadbelb commented on Apr 14, 2026

    @ahmadbelb
    ContributorAuthor

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

  10. dvkashapov commented on Apr 15, 2026

    @dvkashapov
    Member

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

  11. madolson commented on Apr 30, 2026

    @madolson
    Member

    Not easily, we would have to force push and rewrite a bunch of history including tagged releases. I would rather not touch that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

enhancement

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions