Repository navigation
Optimize streams range hot path - #3002
Conversation
|
For reproducing the benchmark valkey-io/valkey-perf-benchmark#25 is needed AMD Ryzen 7 7840HS (16) @ 5.137GHz Benchmark Comparison: unstable vs perf/xrange (averaged)Run Summary:
Statistical Notes:
Note: Values with (n=X, Ο=Y, CV=Z%, CI99%=Β±W%, PI99%=Β±V%) indicate averages from X runs with standard deviation Y, coefficient of variation Z%, 99% confidence interval margin of error Β±W% of the mean, and 99% prediction interval margin of error Β±V% of the mean. CI bounds [A, B] and PI bounds [C, D] show the actual interval ranges. Configuration:
Configuration:
|
|
All the places that use ll2string or ull2string should see also some benefits, https://gist.github.com/nesty92/5387f605884059d9a9145105d1036796
|
Codecov Reportβ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## unstable #3002 +/- ##
============================================
- Coverage 74.69% 74.34% -0.35%
============================================
Files 130 130
Lines 72719 72731 +12
============================================
- Hits 54319 54074 -245
- Misses 18400 18657 +257
π New features to boost your workflow:
|
|
@zuiderkwast @artikell I'd appreciate any feedback on this one |
There was a problem hiding this comment.
As far as the code goes, these changes look good to me! I appreciate your testing and the graphs - very nice π
My only concern is whether this contains redis code - afaik their license is restrictive and doesn't allow copying code to a BSD project like Valkey. You say this is "inspired by" a redis pr, but what do you mean by that?
|
@rainsupreme it's inspired by the idea of: "By comparing stream IDs directly in native-endian form, we eliminate repeated encoding and memcmp() calls that were responsible for a significant portion of total CPU time during stream iteration." But I went down the rabbit hole of optimization :) and ended up finding more and this is the result. |
rainsupreme
left a comment
There was a problem hiding this comment.
Ok, so you read that description and then did your own research and optimization, and didn't reference their source code? I'm pretty sure that's ok then! π
|
I took the high-level concept of the comparisons and implemented it from scratch. This PR actually goes quite a bit further than the original idea |
There was a problem hiding this comment.
Sorry for the long wait! This looks good. It contains multiple changes:
-
Changes in the endian conversion code. I think these are no longer necessary after #3201.
-
Move getClientType to header file and make it
static inline. Did this really affect performance? We compile with link-time-optimization by default so it can be inlined anyway. Usually we trust the compiler when to inline and not. -
Replace memcmp with streamCompareID in range code paths.
-
Use static char array instead of sds to avoid allocations when converting stream ID to string.
-
Use memcpy in ull2string instead of two assignments.
-
Add several XRANGE predefined tests in valkey-benchmark. Please revert this. It's possible to run any tests on the command line by just like
valkey-benchmark [options] XRANGE mystream - + COUNT 300. I guess this wasn't possible in the past when the LRANGE tests were added, but now it is.
I may be wrong about some things. If I'm wrong, please explain. If you agree, revert the changes that are not necessary and update the PR description to mention the remaining changes.
/* Measure Optimized */
start = clock();
for (int j = 0; j < ITERATIONS; j++) {
sink += ull2string_optimized(buf, BUFFER_SIZE, val);
}
double t_opt = (double)(clock() - start) / CLOCKS_PER_SEC;
printf("%d,%.6f,%.6f\n", d, t_orig, t_opt);Good microbenchmark, but it's not mentioned that the execution time is for 50M iterations. If you want to show the duration of each |
|
This is a good change! But I'd echo @zuiderkwast's point that the PR is a still a bit broad. We can keep the stream optimization isolated in this PR. |
|
Hi @zuiderkwast, @sarthakaggarwal97 yes totally agree. Will make the changes as soon I have a chance. I notice now that i forgot to include the flamegraphs π |
|
Hi @zuiderkwast @sarthakaggarwal97 here is a more detailed results for each of the changes so we could make a better decision if something should be removed. BTW @zuiderkwast getClientType is not being inlined at this point unstable (4cbec4a1d9620bc02c9de526264536f1cc3b611c)
objdump -d src/valkey-server | grep "call.*getClientType"
96937: e8 74 ca 0e 00 call 1833b0 <getClientType>
ef230: e8 ab 42 09 00 call 1834e0 <getClientTypeByName>
132c8b: e8 20 07 05 00 call 1833b0 <getClientType>
132e88: e8 23 05 05 00 call 1833b0 <getClientType>
132f72: e8 39 04 05 00 call 1833b0 <getClientType>
15deec: e8 bf 54 02 00 call 1833b0 <getClientType>
172fcc: e8 df 03 01 00 call 1833b0 <getClientType>
17c5d2: e8 d9 6d 00 00 call 1833b0 <getClientType>
17d5c5: e8 e6 5d 00 00 call 1833b0 <getClientType>
17d950: e8 5b 5a 00 00 call 1833b0 <getClientType>
17fb6e: e8 3d 38 00 00 call 1833b0 <getClientType>
180110: e8 9b 32 00 00 call 1833b0 <getClientType>
1801b4: e8 f7 31 00 00 call 1833b0 <getClientType>
1823c0: e8 1b 11 00 00 call 1834e0 <getClientTypeByName>
182580: e8 5b 0f 00 00 call 1834e0 <getClientTypeByName>
183682: e8 29 fd ff ff call 1833b0 <getClientType>
184ff0: e8 bb e3 ff ff call 1833b0 <getClientType>
186463: e8 48 cf ff ff call 1833b0 <getClientType>
186667: e8 44 cd ff ff call 1833b0 <getClientType>
188f49: e8 62 a4 ff ff call 1833b0 <getClientType>
18999f: e8 0c 9a ff ff call 1833b0 <getClientType>
18a00f: e8 9c 93 ff ff call 1833b0 <getClientType>
18bc35: e8 76 77 ff ff call 1833b0 <getClientType>
1bb705: e8 a6 7c fc ff call 1833b0 <getClientType>
1c7f34: e8 77 b4 fb ff call 1833b0 <getClientType>
1e2c37: e8 74 07 fa ff call 1833b0 <getClientType>
1e753f: e8 6c be f9 ff call 1833b0 <getClientType>
1e757b: e8 30 be f9 ff call 1833b0 <getClientType>perf/xrange (553d4ee7b36eb1a38e679eaf5645c980f7368090)
objdump -d src/valkey-server | grep "call.*getClientType"
efa30: e8 8b 47 09 00 call 1841c0 <getClientTypeByName>
180e40: e8 ab cd fe ff call 16dbf0 <getClientType.lto_priv.0>
183100: e8 bb 10 00 00 call 1841c0 <getClientTypeByName>
1832c0: e8 fb 0e 00 00 call 1841c0 <getClientTypeByName>
187272: e8 79 69 fe ff call 16dbf0 <getClientType.lto_priv.0>Staged Performance Benchmark ResultsEach stage is cumulative β applied on top of the previous one. Benchmarked with 5 runs per stage, XRANGE COUNT 10
XRANGE COUNT 100
XRANGE COUNT 300
XRANGE COUNT 1000
Benchmark configuration
|
Signed-off-by: Ernesto Alejandro Santana Hidalgo <ernesto.alejandrosantana@gmail.com>
Signed-off-by: Ernesto Alejandro Santana Hidalgo <ernesto.alejandrosantana@gmail.com>
Signed-off-by: Ernesto Alejandro Santana Hidalgo <ernesto.alejandrosantana@gmail.com>
Signed-off-by: Ernesto Alejandro Santana Hidalgo <ernesto.alejandrosantana@gmail.com>
|
nicee! Thanks @nesty92 |
- Fix release date to Mon Mar 18 2026 - Fix typos: duplicate 'load', 'keyes' -> 'keys', duplicate 'INFO' - Remove reverted contributor (arshidkv12, valkey-io#3137) - Add 7 new release-notes entries from upstream/unstable merge: CLUSTERSCAN (valkey-io#2934), MSETEX (valkey-io#3121), availability-zone (valkey-io#3156), stream range optimization (valkey-io#3002), RDB as AOF preamble (valkey-io#1901), unsigned 64-bit module config (valkey-io#1546), fast_float -> ffc (valkey-io#3329) Signed-off-by: Madelyn Olson <madelyneolson@gmail.com>
This PR improves stream performance in the range iteration and reply generation paths, benefits xadd, xrange, xrevrange, xreadgroup. - ull2string memcpy optimization - streamID struct + streamCompareID - streamID2string + reply path - getClientType inline + cache locality Inspired by the high-level description (not the code) of redis/redis#14480. --------- Signed-off-by: Ernesto Alejandro Santana Hidalgo <ernesto.alejandrosantana@gmail.com>
|
Just pointing a small issue with this change: |
getClientType() is moved from networking.c to a static inline in server.h. The previous implementation called isImportSlotMigrationJob() (defined in cluster_migrateslots.c), which caused a linker failure for executables that do not link cluster_migrateslots.o (e.g. valkey-cli) when compiled at -O0, where the compiler emits the full function body without inlining or dead-code elimination. Fix: add a uint8_t slot_migration_type field to the client struct that is set to CLIENT_TYPE_SLOT_IMPORT or CLIENT_TYPE_SLOT_EXPORT alongside slot_migration_job in cluster_migrateslots.c. The static inline reads this field directly, removing any dependency on cluster_migrateslots.o from translation units that only include server.h. Addresses: valkey-io#3002 https://claude.ai/code/session_01PYkR229wUMqNkkr9dGy4py

This PR improves stream performance in the range iteration and reply generation paths, benefits xadd, xrange, xrevrange, xreadgroup.
Inspired by the high-level description (not the code) of redis/redis#14480