enhance raxLowWalk: use memchr() for child-edge lookup in non-compressed nodes#3472
Merged
madolson merged 2 commits intovalkey-io:unstablefrom Apr 14, 2026
Merged
Conversation
Replace the open-coded byte-by-byte loop in raxLowWalk() with memchr(). libc implementations of memchr() on common platforms are SIMD-optimized (SSE2/AVX2 on x86_64, NEON on arm64), which significantly outperforms a scalar loop while remaining faster than a binary search at the small fan-out sizes (<= 256) that rax nodes can have. Microbenchmark on Apple Silicon (ns/op for finding a byte in a sorted N-byte array): N scalar memchr speedup 16 4.46 1.56 2.86x 32 8.43 1.86 4.53x 64 15.32 2.30 6.66x 128 31.27 3.11 10.05x 256 38.09 4.03 9.45x The change is purely algorithmic: tree structure and memory layout are unchanged. Workloads where every node has small fan-out still benefit because memchr() is faster than the scalar loop even for short scans. Signed-off-by: charsyam <charsyam@naver.com>
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## unstable #3472 +/- ##
============================================
- Coverage 76.40% 76.26% -0.14%
============================================
Files 159 159
Lines 79809 79809
============================================
- Hits 60977 60868 -109
- Misses 18832 18941 +109
🚀 New features to boost your workflow:
|
madolson
approved these changes
Apr 14, 2026
Member
madolson
left a comment
There was a problem hiding this comment.
Wanted to actually validate your claim, and micro benchmarking did show it's faster. Neat!
Contributor
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.
Replace the open-coded byte-by-byte loop in raxLowWalk() with memchr(). libc implementations of memchr() on common platforms are SIMD-optimized (SSE2/AVX2 on x86_64, NEON on arm64), which significantly outperforms a scalar loop while remaining faster than a binary search at the small fan-out sizes (<= 256) that rax nodes can have.
in macOS ARM(M4 pro)
Lookup Performance (Mops/s, , Avg with 5 Repeats)
Insert Performance (Mops/s, Avg with 5 Repeats)
in Linux(Linux x86_64 (Ryzen 7 8845HS, GCC 13.3, Avg with 5 Repeats)
Lookup Performance (Mops/s, , Avg with 5 Repeats)
Insert Performance (Mops/s, Avg with 5 Repeats)
Actually I tested binary search method if h->size is greater than T(8, 16, 32) and SIMD implementation. SIMD(especially NEON in arm is a little bit faster than this PR). but memchr is stable and robust and it is more readable.
Thanks.