Skip to content

Optimize streams range hot path - #3002

Merged
zuiderkwast merged 4 commits into
valkey-io:unstablefrom
nesty92:perf/xrange
Mar 13, 2026
Merged

zuiderkwast merged 4 commits into
valkey-io:unstablefrom
nesty92:perf/xrange

Conversation

@nesty92

@nesty92 nesty92 commented Jan 4, 2026 •

Copy link
Copy Markdown
Contributor

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

@nesty92

nesty92 commented Jan 4, 2026 •

Copy link
Copy Markdown
Contributor Author

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:

  • unstable: 100 total runs, 20 configurations (avg 5.00 runs per config)
  • perf/xrange: 100 total runs, 20 configurations (avg 5.00 runs per config)

Statistical Notes:

  • CI99%: 99% Confidence Interval - range where the true population mean is likely to fall
  • PI99%: 99% Prediction Interval - range where a single future observation is likely to fall
  • CV: Coefficient of Variation - relative variability (Οƒ/ΞΌ Γ— 100%)

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:

  • architecture: x86_64
  • benchmark_mode: duration
  • clients: 25
  • cluster_mode: False
  • data_size: 100
  • duration: 10
  • tls: False
  • valkey_benchmark_threads: 4
  • warmup: 10
Command Metric Pipeline io_threads unstable perf/xrange Diff % Change
XADD rps 1 4 393212.364 (n=5, Οƒ=9823.215, CV=2.50%, CI99%=Β±5.144%, PI99%=Β±12.600%, CI[372986.228, 413438.500], PI[343668.651, 442756.077]) 405138.236 (n=5, Οƒ=2152.207, CV=0.53%, CI99%=Β±1.094%, PI99%=Β±2.679%, CI[400706.812, 409569.660], PI[394283.509, 415992.963]) 11925.872 +3.033%
XADD avg_latency 1 4 0.048 (n=5, Οƒ=0.000, CV=0.00%) 0.046 (n=5, Οƒ=0.000, CV=0.00%) -0.001 -2.929%
XADD p50_latency 1 4 0.049 (n=5, Οƒ=0.000, CV=0.00%) 0.047 (n=5, Οƒ=0.000, CV=0.00%) -0.002 -3.292%
XADD p95_latency 1 4 0.066 (n=5, Οƒ=0.000, CV=0.00%) 0.063 (n=5, Οƒ=0.000, CV=0.00%) -0.003 -4.834%
XADD p99_latency 1 4 0.076 (n=5, Οƒ=0.000, CV=0.00%) 0.071 (n=5, Οƒ=0.000, CV=0.00%) -0.005 -6.332%
XADD rps 10 4 1175434.400 (n=5, Οƒ=29049.914, CV=2.47%, CI99%=Β±5.089%, PI99%=Β±12.465%, CI[1115620.225, 1235248.575], PI[1028920.193, 1321948.607]) 1285092.000 (n=5, Οƒ=19499.477, CV=1.52%, CI99%=Β±3.124%, PI99%=Β±7.653%, CI[1244942.307, 1325241.693], PI[1186745.738, 1383438.262]) 109657.600 +9.329%
XADD avg_latency 10 4 0.207 (n=5, Οƒ=0.000, CV=0.00%) 0.189 (n=5, Οƒ=0.000, CV=0.00%) -0.018 -8.872%
XADD p50_latency 10 4 0.204 (n=5, Οƒ=0.000, CV=0.00%) 0.189 (n=5, Οƒ=0.000, CV=0.00%) -0.014 -7.066%
XADD p95_latency 10 4 0.284 (n=5, Οƒ=0.000, CV=0.00%) 0.225 (n=5, Οƒ=0.000, CV=0.00%) -0.059 -20.860%
XADD p99_latency 10 4 0.346 (n=5, Οƒ=0.000, CV=0.00%) 0.287 (n=5, Οƒ=0.000, CV=0.00%) -0.059 -17.100%
XRANGE_100 (first 100 elements) rps 1 4 29118.160 (n=5, Οƒ=250.983, CV=0.86%, CI99%=Β±1.775%, PI99%=Β±4.347%, CI[28601.383, 29634.937], PI[27852.321, 30383.999]) 34370.060 (n=5, Οƒ=525.441, CV=1.53%, CI99%=Β±3.148%, PI99%=Β±7.710%, CI[33288.169, 35451.951], PI[31719.980, 37020.140]) 5251.900 +18.037%
XRANGE_100 (first 100 elements) avg_latency 1 4 0.817 (n=5, Οƒ=0.000, CV=0.00%) 0.681 (n=5, Οƒ=0.000, CV=0.00%) -0.136 -16.626%
XRANGE_100 (first 100 elements) p50_latency 1 4 0.825 (n=5, Οƒ=0.000, CV=0.00%) 0.690 (n=5, Οƒ=0.000, CV=0.00%) -0.134 -16.299%
XRANGE_100 (first 100 elements) p95_latency 1 4 1.034 (n=5, Οƒ=0.000, CV=0.00%) 0.863 (n=5, Οƒ=0.000, CV=0.00%) -0.171 -16.554%
XRANGE_100 (first 100 elements) p99_latency 1 4 1.196 (n=5, Οƒ=0.000, CV=0.00%) 1.023 (n=5, Οƒ=0.000, CV=0.00%) -0.173 -14.451%
XRANGE_100 (first 100 elements) rps 10 4 29432.302 (n=5, Οƒ=1126.793, CV=3.83%, CI99%=Β±7.883%, PI99%=Β±19.309%, CI[27112.219, 31752.385], PI[23749.283, 35115.321]) 36914.780 (n=5, Οƒ=194.516, CV=0.53%, CI99%=Β±1.085%, PI99%=Β±2.658%, CI[36514.269, 37315.291], PI[35933.732, 37895.828]) 7482.478 +25.423%
XRANGE_100 (first 100 elements) avg_latency 10 4 5.013 (n=5, Οƒ=0.000, CV=0.00%) 3.907 (n=5, Οƒ=0.000, CV=0.00%) -1.106 -22.066%
XRANGE_100 (first 100 elements) p50_latency 10 4 5.127 (n=5, Οƒ=0.000, CV=0.00%) 4.004 (n=5, Οƒ=0.000, CV=0.00%) -1.123 -21.908%
XRANGE_100 (first 100 elements) p95_latency 10 4 7.061 (n=5, Οƒ=0.000, CV=0.00%) 5.401 (n=5, Οƒ=0.000, CV=0.00%) -1.661 -23.519%
XRANGE_100 (first 100 elements) p99_latency 10 4 8.485 (n=5, Οƒ=0.000, CV=0.00%) 5.989 (n=5, Οƒ=0.000, CV=0.00%) -2.496 -29.415%
XRANGE_300 (first 300 elements) rps 1 4 9844.460 (n=5, Οƒ=219.607, CV=2.23%, CI99%=Β±4.593%, PI99%=Β±11.251%, CI[9392.285, 10296.635], PI[8736.863, 10952.057]) 12033.440 (n=5, Οƒ=165.729, CV=1.38%, CI99%=Β±2.836%, PI99%=Β±6.946%, CI[11692.201, 12374.679], PI[11197.578, 12869.302]) 2188.980 +22.236%
XRANGE_300 (first 300 elements) avg_latency 1 4 2.373 (n=5, Οƒ=0.000, CV=0.00%) 1.897 (n=5, Οƒ=0.000, CV=0.00%) -0.475 -20.035%
XRANGE_300 (first 300 elements) p50_latency 1 4 2.381 (n=5, Οƒ=0.000, CV=0.00%) 1.921 (n=5, Οƒ=0.000, CV=0.00%) -0.461 -19.350%
XRANGE_300 (first 300 elements) p95_latency 1 4 3.041 (n=5, Οƒ=0.000, CV=0.00%) 2.418 (n=5, Οƒ=0.000, CV=0.00%) -0.622 -20.470%
XRANGE_300 (first 300 elements) p99_latency 1 4 3.689 (n=5, Οƒ=0.000, CV=0.00%) 2.791 (n=5, Οƒ=0.000, CV=0.00%) -0.898 -24.334%
XRANGE_300 (first 300 elements) rps 10 4 9483.592 (n=5, Οƒ=274.333, CV=2.89%, CI99%=Β±5.956%, PI99%=Β±14.589%, CI[8918.736, 10048.448], PI[8099.984, 10867.200]) 11486.526 (n=5, Οƒ=65.008, CV=0.57%, CI99%=Β±1.165%, PI99%=Β±2.854%, CI[11352.675, 11620.377], PI[11158.658, 11814.394]) 2002.934 +21.120%
XRANGE_300 (first 300 elements) avg_latency 10 4 9.332 (n=5, Οƒ=0.000, CV=0.00%) 7.321 (n=5, Οƒ=0.000, CV=0.00%) -2.011 -21.550%
XRANGE_300 (first 300 elements) p50_latency 10 4 9.055 (n=5, Οƒ=0.000, CV=0.00%) 7.135 (n=5, Οƒ=0.000, CV=0.00%) -1.920 -21.204%
XRANGE_300 (first 300 elements) p95_latency 10 4 16.605 (n=5, Οƒ=0.000, CV=0.00%) 12.914 (n=5, Οƒ=0.000, CV=0.00%) -3.691 -22.229%
XRANGE_300 (first 300 elements) p99_latency 10 4 19.961 (n=5, Οƒ=0.000, CV=0.00%) 15.329 (n=5, Οƒ=0.000, CV=0.00%) -4.632 -23.206%
XRANGE_500 (first 500 elements) rps 1 4 5891.132 (n=5, Οƒ=234.114, CV=3.97%, CI99%=Β±8.183%, PI99%=Β±20.043%, CI[5409.089, 6373.175], PI[4710.372, 7071.892]) 7172.208 (n=5, Οƒ=150.066, CV=2.09%, CI99%=Β±4.308%, PI99%=Β±10.553%, CI[6863.220, 7481.196], PI[6415.346, 7929.070]) 1281.076 +21.746%
XRANGE_500 (first 500 elements) avg_latency 1 4 2.800 (n=5, Οƒ=0.000, CV=0.00%) 2.240 (n=5, Οƒ=0.000, CV=0.00%) -0.560 -20.006%
XRANGE_500 (first 500 elements) p50_latency 1 4 2.895 (n=5, Οƒ=0.000, CV=0.00%) 2.273 (n=5, Οƒ=0.000, CV=0.00%) -0.622 -21.499%
XRANGE_500 (first 500 elements) p95_latency 1 4 3.746 (n=5, Οƒ=0.000, CV=0.00%) 2.892 (n=5, Οƒ=0.000, CV=0.00%) -0.854 -22.807%
XRANGE_500 (first 500 elements) p99_latency 1 4 4.701 (n=5, Οƒ=0.000, CV=0.00%) 3.437 (n=5, Οƒ=0.000, CV=0.00%) -1.264 -26.886%
XRANGE_500 (first 500 elements) rps 10 4 4668.602 (n=5, Οƒ=56.063, CV=1.20%, CI99%=Β±2.473%, PI99%=Β±6.056%, CI[4553.168, 4784.036], PI[4385.848, 4951.356]) 5628.320 (n=5, Οƒ=64.084, CV=1.14%, CI99%=Β±2.344%, PI99%=Β±5.743%, CI[5496.370, 5760.270], PI[5305.110, 5951.530]) 959.718 +20.557%
XRANGE_500 (first 500 elements) avg_latency 10 4 18.222 (n=5, Οƒ=0.000, CV=0.00%) 13.549 (n=5, Οƒ=0.000, CV=0.00%) -4.673 -25.647%
XRANGE_500 (first 500 elements) p50_latency 10 4 17.137 (n=5, Οƒ=0.000, CV=0.00%) 12.253 (n=5, Οƒ=0.000, CV=0.00%) -4.883 -28.496%
XRANGE_500 (first 500 elements) p95_latency 10 4 37.260 (n=5, Οƒ=0.000, CV=0.00%) 29.017 (n=5, Οƒ=0.000, CV=0.00%) -8.243 -22.124%
XRANGE_500 (first 500 elements) p99_latency 10 4 43.295 (n=5, Οƒ=0.000, CV=0.00%) 33.538 (n=5, Οƒ=0.000, CV=0.00%) -9.757 -22.536%
XRANGE_600 (first 600 elements) rps 1 4 4865.832 (n=5, Οƒ=163.573, CV=3.36%, CI99%=Β±6.922%, PI99%=Β±16.955%, CI[4529.034, 5202.630], PI[4040.848, 5690.816]) 5922.352 (n=5, Οƒ=76.499, CV=1.29%, CI99%=Β±2.660%, PI99%=Β±6.515%, CI[5764.840, 6079.864], PI[5536.528, 6308.176]) 1056.520 +21.713%
XRANGE_600 (first 600 elements) avg_latency 1 4 3.284 (n=5, Οƒ=0.000, CV=0.00%) 2.598 (n=5, Οƒ=0.000, CV=0.00%) -0.686 -20.877%
XRANGE_600 (first 600 elements) p50_latency 1 4 3.361 (n=5, Οƒ=0.000, CV=0.00%) 2.629 (n=5, Οƒ=0.000, CV=0.00%) -0.731 -21.758%
XRANGE_600 (first 600 elements) p95_latency 1 4 4.410 (n=5, Οƒ=0.000, CV=0.00%) 3.405 (n=5, Οƒ=0.000, CV=0.00%) -1.005 -22.784%
XRANGE_600 (first 600 elements) p99_latency 1 4 5.103 (n=5, Οƒ=0.000, CV=0.00%) 3.993 (n=5, Οƒ=0.000, CV=0.00%) -1.110 -21.760%
XRANGE_600 (first 600 elements) rps 10 4 3914.632 (n=5, Οƒ=56.607, CV=1.45%, CI99%=Β±2.977%, PI99%=Β±7.293%, CI[3798.077, 4031.187], PI[3629.131, 4200.133]) 4533.292 (n=5, Οƒ=53.221, CV=1.17%, CI99%=Β±2.417%, PI99%=Β±5.921%, CI[4423.708, 4642.876], PI[4264.868, 4801.716]) 618.660 +15.804%
XRANGE_600 (first 600 elements) avg_latency 10 4 21.282 (n=5, Οƒ=0.000, CV=0.00%) 16.982 (n=5, Οƒ=0.000, CV=0.00%) -4.301 -20.207%
XRANGE_600 (first 600 elements) p50_latency 10 4 19.788 (n=5, Οƒ=0.000, CV=0.00%) 15.596 (n=5, Οƒ=0.000, CV=0.00%) -4.192 -21.185%
XRANGE_600 (first 600 elements) p95_latency 10 4 43.647 (n=5, Οƒ=0.000, CV=0.00%) 35.666 (n=5, Οƒ=0.000, CV=0.00%) -7.981 -18.285%
XRANGE_600 (first 600 elements) p99_latency 10 4 48.857 (n=5, Οƒ=0.000, CV=0.00%) 39.743 (n=5, Οƒ=0.000, CV=0.00%) -9.114 -18.654%

Configuration:

  • architecture: x86_64
  • benchmark_mode: duration
  • clients: 50
  • cluster_mode: False
  • data_size: 100
  • duration: 10
  • tls: False
  • valkey_benchmark_threads: 4
  • warmup: 10
Command Metric Pipeline io_threads unstable perf/xrange Diff % Change
XADD rps 1 4 404728.900 (n=5, Οƒ=10411.204, CV=2.57%, CI99%=Β±5.297%, PI99%=Β±12.974%, CI[383292.088, 426165.712], PI[352219.648, 457238.152]) 405355.724 (n=5, Οƒ=9775.339, CV=2.41%, CI99%=Β±4.965%, PI99%=Β±12.163%, CI[385228.166, 425483.282], PI[356053.478, 454657.970]) 626.824 +0.155%
XADD avg_latency 1 4 0.080 (n=5, Οƒ=0.000, CV=0.00%) 0.080 (n=5, Οƒ=0.000, CV=0.00%) -0.001 -0.746%
XADD p50_latency 1 4 0.082 (n=5, Οƒ=0.000, CV=0.00%) 0.081 (n=5, Οƒ=0.000, CV=0.00%) -0.002 -1.946%
XADD p95_latency 1 4 0.106 (n=5, Οƒ=0.000, CV=0.00%) 0.106 (n=5, Οƒ=0.000, CV=0.00%) 0.000 +0.000%
XADD p99_latency 1 4 0.122 (n=5, Οƒ=0.000, CV=0.00%) 0.122 (n=5, Οƒ=0.000, CV=0.00%) 0.000 +0.000%
XADD rps 10 4 1162925.200 (n=5, Οƒ=31457.476, CV=2.71%, CI99%=Β±5.570%, PI99%=Β±13.643%, CI[1098153.822, 1227696.578], PI[1004268.374, 1321582.026]) 1314050.400 (n=5, Οƒ=56427.175, CV=4.29%, CI99%=Β±8.842%, PI99%=Β±21.658%, CI[1197866.068, 1430234.732], PI[1029458.070, 1598642.730]) 151125.200 +12.995%
XADD avg_latency 10 4 0.423 (n=5, Οƒ=0.000, CV=0.00%) 0.374 (n=5, Οƒ=0.000, CV=0.00%) -0.049 -11.620%
XADD p50_latency 10 4 0.409 (n=5, Οƒ=0.000, CV=0.00%) 0.370 (n=5, Οƒ=0.000, CV=0.00%) -0.038 -9.398%
XADD p95_latency 10 4 0.647 (n=5, Οƒ=0.000, CV=0.00%) 0.500 (n=5, Οƒ=0.000, CV=0.00%) -0.147 -22.751%
XADD p99_latency 10 4 0.765 (n=5, Οƒ=0.000, CV=0.00%) 0.601 (n=5, Οƒ=0.000, CV=0.00%) -0.165 -21.531%
XRANGE_100 (first 100 elements) rps 1 4 29577.680 (n=5, Οƒ=535.455, CV=1.81%, CI99%=Β±3.728%, PI99%=Β±9.130%, CI[28475.171, 30680.189], PI[26877.096, 32278.264]) 34609.700 (n=5, Οƒ=299.219, CV=0.86%, CI99%=Β±1.780%, PI99%=Β±4.360%, CI[33993.605, 35225.795], PI[33100.581, 36118.819]) 5032.020 +17.013%
XRANGE_100 (first 100 elements) avg_latency 1 4 1.647 (n=5, Οƒ=0.000, CV=0.00%) 1.395 (n=5, Οƒ=0.000, CV=0.00%) -0.252 -15.309%
XRANGE_100 (first 100 elements) p50_latency 1 4 1.639 (n=5, Οƒ=0.000, CV=0.00%) 1.383 (n=5, Οƒ=0.000, CV=0.00%) -0.256 -15.619%
XRANGE_100 (first 100 elements) p95_latency 1 4 1.895 (n=5, Οƒ=0.000, CV=0.00%) 1.666 (n=5, Οƒ=0.000, CV=0.00%) -0.229 -12.074%
XRANGE_100 (first 100 elements) p99_latency 1 4 2.549 (n=5, Οƒ=0.000, CV=0.00%) 2.317 (n=5, Οƒ=0.000, CV=0.00%) -0.232 -9.100%
XRANGE_100 (first 100 elements) rps 10 4 29406.702 (n=5, Οƒ=1078.838, CV=3.67%, CI99%=Β±7.554%, PI99%=Β±18.503%, CI[27185.359, 31628.045], PI[23965.545, 34847.859]) 36194.990 (n=5, Οƒ=737.045, CV=2.04%, CI99%=Β±4.193%, PI99%=Β±10.270%, CI[34677.404, 37712.576], PI[32477.679, 39912.301]) 6788.288 +23.084%
XRANGE_100 (first 100 elements) avg_latency 10 4 9.430 (n=5, Οƒ=0.000, CV=0.00%) 7.424 (n=5, Οƒ=0.000, CV=0.00%) -2.006 -21.270%
XRANGE_100 (first 100 elements) p50_latency 10 4 9.442 (n=5, Οƒ=0.000, CV=0.00%) 7.476 (n=5, Οƒ=0.000, CV=0.00%) -1.966 -20.826%
XRANGE_100 (first 100 elements) p95_latency 10 4 13.681 (n=5, Οƒ=0.000, CV=0.00%) 10.485 (n=5, Οƒ=0.000, CV=0.00%) -3.195 -23.356%
XRANGE_100 (first 100 elements) p99_latency 10 4 15.801 (n=5, Οƒ=0.000, CV=0.00%) 11.684 (n=5, Οƒ=0.000, CV=0.00%) -4.117 -26.055%
XRANGE_300 (first 300 elements) rps 1 4 10090.700 (n=5, Οƒ=283.857, CV=2.81%, CI99%=Β±5.792%, PI99%=Β±14.188%, CI[9506.234, 10675.166], PI[8659.057, 11522.343]) 12033.626 (n=5, Οƒ=189.513, CV=1.57%, CI99%=Β±3.243%, PI99%=Β±7.943%, CI[11643.417, 12423.835], PI[11077.812, 12989.440]) 1942.926 +19.255%
XRANGE_300 (first 300 elements) avg_latency 1 4 4.785 (n=5, Οƒ=0.000, CV=0.00%) 3.974 (n=5, Οƒ=0.000, CV=0.00%) -0.811 -16.953%
XRANGE_300 (first 300 elements) p50_latency 1 4 4.746 (n=5, Οƒ=0.000, CV=0.00%) 3.949 (n=5, Οƒ=0.000, CV=0.00%) -0.797 -16.788%
XRANGE_300 (first 300 elements) p95_latency 1 4 5.754 (n=5, Οƒ=0.000, CV=0.00%) 4.580 (n=5, Οƒ=0.000, CV=0.00%) -1.174 -20.409%
XRANGE_300 (first 300 elements) p99_latency 1 4 7.445 (n=5, Οƒ=0.000, CV=0.00%) 5.874 (n=5, Οƒ=0.000, CV=0.00%) -1.571 -21.103%
XRANGE_300 (first 300 elements) rps 10 4 9138.042 (n=5, Οƒ=461.259, CV=5.05%, CI99%=Β±10.393%, PI99%=Β±25.458%, CI[8188.304, 10087.780], PI[6811.668, 11464.416]) 10896.486 (n=5, Οƒ=280.466, CV=2.57%, CI99%=Β±5.300%, PI99%=Β±12.982%, CI[10319.003, 11473.969], PI[9481.948, 12311.024]) 1758.444 +19.243%
XRANGE_300 (first 300 elements) avg_latency 10 4 19.314 (n=5, Οƒ=0.000, CV=0.00%) 15.221 (n=5, Οƒ=0.000, CV=0.00%) -4.094 -21.195%
XRANGE_300 (first 300 elements) p50_latency 10 4 18.581 (n=5, Οƒ=0.000, CV=0.00%) 14.517 (n=5, Οƒ=0.000, CV=0.00%) -4.064 -21.871%
XRANGE_300 (first 300 elements) p95_latency 10 4 35.769 (n=5, Οƒ=0.000, CV=0.00%) 29.308 (n=5, Οƒ=0.000, CV=0.00%) -6.461 -18.063%
XRANGE_300 (first 300 elements) p99_latency 10 4 43.090 (n=5, Οƒ=0.000, CV=0.00%) 34.898 (n=5, Οƒ=0.000, CV=0.00%) -8.192 -19.011%
XRANGE_500 (first 500 elements) rps 1 4 5851.314 (n=5, Οƒ=195.315, CV=3.34%, CI99%=Β±6.873%, PI99%=Β±16.835%, CI[5449.159, 6253.469], PI[4866.238, 6836.390]) 7005.836 (n=5, Οƒ=44.730, CV=0.64%, CI99%=Β±1.315%, PI99%=Β±3.220%, CI[6913.736, 7097.936], PI[6780.238, 7231.434]) 1154.522 +19.731%
XRANGE_500 (first 500 elements) avg_latency 1 4 5.667 (n=5, Οƒ=0.000, CV=0.00%) 4.433 (n=5, Οƒ=0.000, CV=0.00%) -1.234 -21.777%
XRANGE_500 (first 500 elements) p50_latency 1 4 5.788 (n=5, Οƒ=0.000, CV=0.00%) 4.565 (n=5, Οƒ=0.000, CV=0.00%) -1.222 -21.120%
XRANGE_500 (first 500 elements) p95_latency 1 4 7.325 (n=5, Οƒ=0.000, CV=0.00%) 5.524 (n=5, Οƒ=0.000, CV=0.00%) -1.802 -24.594%
XRANGE_500 (first 500 elements) p99_latency 1 4 8.498 (n=5, Οƒ=0.000, CV=0.00%) 6.162 (n=5, Οƒ=0.000, CV=0.00%) -2.336 -27.488%
XRANGE_500 (first 500 elements) rps 10 4 4787.038 (n=5, Οƒ=84.962, CV=1.77%, CI99%=Β±3.654%, PI99%=Β±8.951%, CI[4612.101, 4961.975], PI[4358.531, 5215.545]) 5571.048 (n=5, Οƒ=194.093, CV=3.48%, CI99%=Β±7.174%, PI99%=Β±17.571%, CI[5171.407, 5970.689], PI[4592.132, 6549.964]) 784.010 +16.378%
XRANGE_500 (first 500 elements) avg_latency 10 4 33.928 (n=5, Οƒ=0.000, CV=0.00%) 26.626 (n=5, Οƒ=0.000, CV=0.00%) -7.302 -21.521%
XRANGE_500 (first 500 elements) p50_latency 10 4 31.020 (n=5, Οƒ=0.000, CV=0.00%) 23.154 (n=5, Οƒ=0.000, CV=0.00%) -7.866 -25.357%
XRANGE_500 (first 500 elements) p95_latency 10 4 74.137 (n=5, Οƒ=0.000, CV=0.00%) 60.569 (n=5, Οƒ=0.000, CV=0.00%) -13.568 -18.301%
XRANGE_500 (first 500 elements) p99_latency 10 4 82.713 (n=5, Οƒ=0.000, CV=0.00%) 70.757 (n=5, Οƒ=0.000, CV=0.00%) -11.955 -14.454%
XRANGE_600 (first 600 elements) rps 1 4 4882.130 (n=5, Οƒ=277.841, CV=5.69%, CI99%=Β±11.718%, PI99%=Β±28.703%, CI[4310.051, 5454.209], PI[3480.830, 6283.430]) 5782.150 (n=5, Οƒ=73.023, CV=1.26%, CI99%=Β±2.600%, PI99%=Β±6.370%, CI[5631.794, 5932.506], PI[5413.855, 6150.445]) 900.020 +18.435%
XRANGE_600 (first 600 elements) avg_latency 1 4 6.484 (n=5, Οƒ=0.000, CV=0.00%) 5.226 (n=5, Οƒ=0.000, CV=0.00%) -1.257 -19.394%
XRANGE_600 (first 600 elements) p50_latency 1 4 6.634 (n=5, Οƒ=0.000, CV=0.00%) 5.386 (n=5, Οƒ=0.000, CV=0.00%) -1.248 -18.812%
XRANGE_600 (first 600 elements) p95_latency 1 4 8.634 (n=5, Οƒ=0.000, CV=0.00%) 6.679 (n=5, Οƒ=0.000, CV=0.00%) -1.955 -22.645%
XRANGE_600 (first 600 elements) p99_latency 1 4 9.682 (n=5, Οƒ=0.000, CV=0.00%) 7.449 (n=5, Οƒ=0.000, CV=0.00%) -2.234 -23.069%
XRANGE_600 (first 600 elements) rps 10 4 3939.376 (n=5, Οƒ=73.132, CV=1.86%, CI99%=Β±3.822%, PI99%=Β±9.363%, CI[3788.796, 4089.956], PI[3570.531, 4308.221]) 4469.790 (n=5, Οƒ=123.364, CV=2.76%, CI99%=Β±5.683%, PI99%=Β±13.920%, CI[4215.782, 4723.798], PI[3847.599, 5091.981]) 530.414 +13.464%
XRANGE_600 (first 600 elements) avg_latency 10 4 40.044 (n=5, Οƒ=0.000, CV=0.00%) 33.048 (n=5, Οƒ=0.000, CV=0.00%) -6.996 -17.470%
XRANGE_600 (first 600 elements) p50_latency 10 4 36.786 (n=5, Οƒ=0.000, CV=0.00%) 29.189 (n=5, Οƒ=0.000, CV=0.00%) -7.597 -20.651%
XRANGE_600 (first 600 elements) p95_latency 10 4 87.244 (n=5, Οƒ=0.000, CV=0.00%) 74.597 (n=5, Οƒ=0.000, CV=0.00%) -12.646 -14.495%
XRANGE_600 (first 600 elements) p99_latency 10 4 96.268 (n=5, Οƒ=0.000, CV=0.00%) 86.297 (n=5, Οƒ=0.000, CV=0.00%) -9.971 -10.358%

@nesty92

nesty92 commented Jan 4, 2026 •

Copy link
Copy Markdown
Contributor Author

All the places that use ll2string or ull2string should see also some benefits, https://gist.github.com/nesty92/5387f605884059d9a9145105d1036796

image

@codecov

codecov Bot commented Jan 4, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 97.56098% with 1 line in your changes missing coverage. Please review.
βœ… Project coverage is 74.34%. Comparing base (4cbec4a) to head (553d4ee).
⚠️ Report is 3 commits behind head on unstable.

Files with missing lines Patch % Lines
src/t_stream.c 97.05% 1 Missing ⚠️
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     
Files with missing lines Coverage Ξ”
src/networking.c 91.38% <ΓΈ> (-0.14%) ⬇️
src/server.h 100.00% <100.00%> (ΓΈ)
src/util.c 67.48% <100.00%> (+0.17%) ⬆️
src/t_stream.c 94.45% <97.05%> (+0.03%) ⬆️

... and 27 files with indirect coverage changes

πŸš€ New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • πŸ“¦ JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@nesty92

nesty92 commented Jan 4, 2026

Copy link
Copy Markdown
Contributor Author

@zuiderkwast @artikell I'd appreciate any feedback on this one

@rainsupreme rainsupreme left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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?

@nesty92

nesty92 commented Jan 23, 2026

Copy link
Copy Markdown
Contributor Author

@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 rainsupreme left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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! 😁

@nesty92

nesty92 commented Jan 23, 2026

Copy link
Copy Markdown
Contributor Author

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

@zuiderkwast zuiderkwast left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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.

@zuiderkwast zuiderkwast moved this to In Progress in Valkey 9.1 Mar 11, 2026
@zuiderkwast

Copy link
Copy Markdown
Contributor

All the places that use ll2string or ull2string should see also some benefits, https://gist.github.com/nesty92/5387f605884059d9a9145105d1036796

         /* 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 ull2string() call, you can divide by the number of iterations. The number will be very small, so it's probably better to report it in nanoseconds.

@zuiderkwast zuiderkwast added the release-notes This issue should get a line item in the release notes label Mar 11, 2026
@sarthakaggarwal97

Copy link
Copy Markdown
Contributor

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.

@nesty92

nesty92 commented Mar 11, 2026

Copy link
Copy Markdown
Contributor Author

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 πŸ˜…

@nesty92 nesty92 changed the title perf: optimize streams range hot path dratf: perf: optimize streams range hot path Mar 12, 2026
@nesty92 nesty92 changed the title dratf: perf: optimize streams range hot path draft: perf: optimize streams range hot path Mar 12, 2026
@nesty92 nesty92 changed the title draft: perf: optimize streams range hot path perf: optimize streams range hot path Mar 12, 2026
@nesty92
nesty92 marked this pull request as draft March 12, 2026 23:18
@nesty92

nesty92 commented Mar 13, 2026 •

Copy link
Copy Markdown
Contributor Author

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 Results

Each stage is cumulative β€” applied on top of the previous one. Benchmarked with 5 runs per stage, pipeline=10, clients=50, io-threads=4, CPU-pinned, data_size=100, 10k stream entries.

XRANGE COUNT 10

Stage Description RPS vs Baseline vs Previous
0 baseline (unstable) 323,221 -- --
1 ull2string memcpy 341,256 +5.58% +5.58%
2 streamID struct + streamCompareID 344,228 +6.50% +0.87%
3 streamID2string + reply path 389,502 +20.51% +13.15%
4 getClientType inline + cache locality 446,824 +38.24% +14.72%

XRANGE COUNT 100

Stage Description RPS vs Baseline vs Previous
0 baseline (unstable) 40,588 -- --
1 ull2string memcpy 42,718 +5.25% +5.25%
2 streamID struct + streamCompareID 43,262 +6.59% +1.27%
3 streamID2string + reply path 49,281 +21.42% +13.91%
4 getClientType inline + cache locality 59,361 +46.25% +20.45%

XRANGE COUNT 300

Stage Description RPS vs Baseline vs Previous
0 baseline (unstable) 12,916 -- --
1 ull2string memcpy 12,946 +0.23% +0.23%
2 streamID struct + streamCompareID 13,183 +2.07% +1.83%
3 streamID2string + reply path 15,013 +16.23% +13.88%
4 getClientType inline + cache locality 17,909 +38.66% +19.29%

XRANGE COUNT 1000

Stage Description RPS vs Baseline vs Previous
0 baseline (unstable) 3,060 -- --
1 ull2string memcpy 3,082 +0.72% +0.72%
2 streamID struct + streamCompareID 3,110 +1.62% +0.89%
3 streamID2string + reply path 3,489 +14.01% +12.19%
4 getClientType inline + cache locality 3,960 +29.40% +13.50%
Benchmark configuration
  • Base commit: 4cbec4a1d (unstable HEAD)
  • Runs: 5 per stage
  • Pipeline: 10 | Clients: 50 | Data size: 100 bytes
  • IO threads: 4 | Benchmark threads: 4
  • Server CPUs: 0,2,4,6 | Client CPUs: 8,10,12,14
  • Duration: 10s per benchmark | Warmup: 10s
  • Stream entries: 10,000 (XADD populated before each benchmark)

@nesty92
nesty92 marked this pull request as ready for review March 13, 2026 13:03
nesty92 added 3 commits March 13, 2026 14:08
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>

@zuiderkwast zuiderkwast left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I'm convinced! πŸ˜„

@zuiderkwast zuiderkwast changed the title perf: optimize streams range hot path Optimize streams range hot path Mar 13, 2026
@zuiderkwast
zuiderkwast merged commit b58083d into valkey-io:unstable Mar 13, 2026
58 checks passed
@github-project-automation github-project-automation Bot moved this from In Progress to Done in Valkey 9.1 Mar 13, 2026
@sarthakaggarwal97

Copy link
Copy Markdown
Contributor

nicee! Thanks @nesty92

@nesty92
nesty92 deleted the perf/xrange branch March 13, 2026 16:40
madolson added a commit to madolson/valkey that referenced this pull request Mar 15, 2026
- 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>
JimB123 pushed a commit that referenced this pull request Mar 19, 2026
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>
@ranshid

ranshid commented Mar 29, 2026

Copy link
Copy Markdown
Member

Just pointing a small issue with this change:
static inline getClientType() calls isImportSlotMigrationJob(), which is defined in cluster_migrateslots.c. Every .c file that includes server.h gets a copy of this inline function in
its translation unit. This creates a problem for non-server compiled executables as they link with much limited set of objects. For example, valkey-cli links a small subset of .o files (like crc16.o, sds.o, dict.o, etc.) β€” it does NOT link cluster_migrateslots.o.
Why we do not see it? Because our default is to compile with -O3. At -O3, the compiler sees that getClientType is static inline and inlines it fully into every call site. At -O0, the compiler does no inlining and no dead-code elimination. It emits the full body of the static inline getClientType into every .o file that includes server.h (even if getClientType is not really used)

nesty92 pushed a commit to nesty92/valkey that referenced this pull request Mar 29, 2026
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

release-notes This issue should get a line item in the release notes

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

5 participants