Repository navigation
ZSCAN: format skiplist scores with d2string(), consistent with ZSCORE - #15821
Merged
Merged
Conversation
The skiplist ZSCAN reply (scanCallback's OBJ_ZSET branch) formatted the emitted score with ld2string(..., LD_STR_AUTO): it widens the double to long double and runs libc's "%.17Lg" for a value that is always a plain double. It was the only client-reply ld2string() call fed a double; every other client-facing score reply already uses d2string(): ZSCORE, ZMSCORE, ZRANGE/ZRANGEBYSCORE/ZRANDMEMBER WITHSCORES, ZPOPMIN/ZPOPMAX and friends via addReplyDouble(), and listpack-encoded sorted sets, which store the d2string() form (or a listpack integer for integral scores) and echo it back from ZSCAN as-is. This is a leftover from redis#10587 (7.2), which moved d2string() and addReplyDouble() to the shortest round-trip formatter but did not touch this branch. Since then the same key has returned different score strings from ZSCAN depending on its encoding, and skiplist ZSCAN has disagreed with ZSCORE for the same member: ZADD k 3.3 m (with more than zset-max-listpack-entries members) ZSCORE k m -> "3.3" ZSCAN k 0 -> "3.2999999999999998" Switch the skiplist ZSCAN path to d2string() as well. The score string becomes byte-identical to what ZSCORE returns, and to what listpack ZSCAN returns for scores written since 7.2 (older listpack entries keep their stored %.17g text): the same short round-trip form d2string() produces everywhere else. The notation can differ from "%.17Lg" in either direction (1.2345678912345679e+8 where it printed 123456789.12345679, -123456789012345670000000 where it printed -1.2345678901234567e+23), and integral scores of 18 or more digits up to 2^62 (d2string()'s integer fast path) now print exactly (2^60 -> 1152921504606846976 instead of 1.152921504606847e+18), again matching ZSCORE. All of these re-parse to the same double through ZADD, and AOF rewrite has emitted the shortest round-trip form since 7.2. The stack buffer shrinks from MAX_LONG_DOUBLE_CHARS to MAX_D2STRING_CHARS. It is also cheaper: ZSCAN COUNT 1000 over a 200K-member skiplist sorted set roughly doubles in throughput in a same-host A/B. The module scan callback (RM_ScanKey) keeps its current formatting; it is a separate API surface and is left for a follow-up. Add a test asserting that ZSCAN scores are byte-identical to ZSCORE for the same members, for both encodings, including denormal, infinite and the longest-formatted values.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## unstable #15821 +/- ##
============================================
- Coverage 77.80% 77.63% -0.17%
============================================
Files 146 146
Lines 88016 88016
============================================
- Hits 68477 68335 -142
- Misses 19539 19681 +142
🚀 New features to boost your workflow:
|
moticless
approved these changes
Sep 27, 2026
Keep the borrowed-node reply path and queued-deletion coverage from redis#15822. Apply d2string score formatting in scanGenericCommand and preserve the score-consistency regression tests.
moticless
approved these changes
Oct 1, 2026
antirez
pushed a commit
to antirez/redis
that referenced
this pull request
Oct 3, 2026
…s#15821) Port the final PR delta relative to current unstable onto the B+tree patch stack. Keep d2string in the borrowed-node reply path from redis#15822 and adapt the encoding expectations to B+tree, as in Filipe’s integration branch. (cherry picked from commit 2ab607f, relative to parent 2)
Collaborator
|
Should we consider it a bug? should backport to the old versions? |
Collaborator
Author
|
@sundb I would treat this as a formatting consistency improvement, not a numeric correctness bug: |
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.
Problem
For the same sorted set member,
ZSCANon a skiplist-encoded key returns a different score string thanZSCORE— and thanZSCANitself on a listpack-encoded key:Root cause
#10587 (7.2) moved
d2string()/addReplyDouble()to the fpconv shortest round-trip formatter, butscanCallback()'sOBJ_ZSETbranch indb.ckept formatting the score withld2string(..., LD_STR_AUTO)— i.e. the C library's"%.17Lg"on adoublewidened tolong double. It is the only client-replyld2string()call fed adouble; every other client-facing score reply already usesd2string()(ZSCORE, ZMSCORE, theWITHSCORESvariants, ZPOPMIN/ZPOPMAX, and listpack-encoded sorted sets, which store thed2string()form and echo it from ZSCAN as-is). The result is that since 7.2 a key's ZSCAN score strings change form when it converts from listpack to skiplist.Fix
Two lines: format the skiplist ZSCAN score with
d2string()into aMAX_D2STRING_CHARSbuffer (128 bytes instead of the 5 KiBMAX_LONG_DOUBLE_CHARSstack buffer). After merging currentunstable, including #15822, these lines are inscanGenericCommand()reply construction. The borrowed-node collection and queued-deletion regression test from #15822 are preserved.Behaviour change (release notes)
This is a formatting consistency improvement, not a numeric correctness fix: the old and new strings parse to the same double. Since the reply text changes, this should be release-noted and does not warrant a bug-fix backport.
ZSCAN on skiplist-encoded sorted sets now returns the same score string as ZSCORE for the same value. Relative to
"%.17Lg"the notation can differ in either direction —1.2345678912345679e+8where it printed123456789.12345679,-123456789012345670000000where it printed-1.2345678901234567e+23— and integral scores of 18+ digits up to 2^62 print exactly (1152921504606846976instead of1.152921504606847e+18). Every form re-parses to the samedoublethrough ZADD. Notes for reviewers:rioWriteBulkDouble)."%.17Lg"already produced exponent notation (1e+100,9.8813129168249309e-323); fpconv writes minimal exponent digits (e+8vse+08), exactly as ZSCORE / ZMSCORE /WITHSCOREShave since 7.2, so any client parsing those parses this."%.17g"text (unchanged by this PR).1e-322, matching ZSCORE.Out of scope
RM_ScanKey(module scan callback) keeps its current formatting — it is a separate API surface and is left for a follow-up.Tests
New
ZSCAN scores match ZSCORE with encoding {listpack,skiplist}intests/unit/scan.tcl(runs under both{standalone}and{cluster}): walks the full cursor withCOUNT 50, asserts every ZSCAN score is byte-identical toZSCORE, and pins3.3,1,-0.1,1e-322,inf,-inf, the 25-char-123456789012345670000000,1152921504606846976and1.2345678912345679e+8. Five of those assertions fail on current unstable's skiplist path.unit/scanpasses under RESP2 and--force-resp3, and under an AddressSanitizer build.Performance (secondary)
The measurements below use the original September 18 base and source hashes listed here. They predate #15822 and the September 29 merge; they do not measure the incremental gain over current
unstable.Per score,
ld2string()("%.17Lg"on along double) costs several times whatd2string()does (formatter microbenchmark, x86: 173 → 21 ns for0, 195 → 67 ns for0.645108), and a ZSCAN reply over a large sorted set is mostly score formatting once COUNT grows, so the change is visible end-to-end.Setup: Redis
unstablebase16e021b465fbbc73d5f070b4da5ce9f5ee4baea1vs this change (headcd4d2720d9b1ed5d5173396fe1eb96581827ad72); the benchmarked commits are9e19b01cc3096a809d45e41dd65b22a63059604cplus two empty commits on top of it (d9ebca1f244c90875f12b8a884182ef3100a0cf0,1f038c32b3bb776c0f5ec5acfec6a9e54369b918, branchfilipecosta90/redis:zscan-reply-d2string-bench) so there are three distinct samples with identicalsrc/. memtier_benchmark specs from redis-benchmarks-specification,oss-standalone. Baseline = the run of the base commit plus the most recent earlierunstableruns on the same runner (n per cell; ARM ZSCAN rows 2026-08-27 → 09-17, HSCAN control from 06-08; x86 200K rows 2026-09-02 → 09-17, 1K row from 06-26, MATCH from 08-10, controls from 04-27). Medians with min–max. The four ZSCAN specs that reach the changed path (skiplist-encoded sorted sets) move and their ranges do not overlap; the three controls (listpack ZSCAN, HSCAN, ZREVRANGE WITHSCORES — code this PR does not touch) are flat.arm-aws-m8g.metal-24xl (Graviton4):
unstableops/s: median (n, min–max)1key-zset-1K-elements-zscan(6-decimal scores → fpconv path)1key-zset-200K-elements-zscan(COUNT 10; all scores0→ integer fast path)1key-zset-200K-elements-zscan-count-1000(all scores0)1key-zset-1K-elements-zscan-match-count-100(all scores1; only MATCH hits formatted)1key-zset-100-elements-zscan(listpack — control, code not touched)1key-hash-hscan-1K-fields-10B-values(control)1key-zset-1M-elements-zrevrange-withscores-5-elements-pipeline-10(control)x86-aws-m7i.metal-24xl-2 (Sapphire Rapids):
unstableops/s: median (n, min–max)1key-zset-1K-elements-zscan(6-decimal scores → fpconv path)1key-zset-200K-elements-zscan(COUNT 10; all scores0→ integer fast path)1key-zset-200K-elements-zscan-count-1000(all scores0)1key-zset-1K-elements-zscan-match-count-100(all scores1; only MATCH hits formatted)1key-zset-100-elements-zscan(listpack — control, code not touched)1key-hash-hscan-1K-fields-10B-values(control)1key-zset-1M-elements-zrevrange-withscores-5-elements-pipeline-10(control)Top-down microarchitecture analysis (x86 Sapphire Rapids, profiler-instrumented runner, one run each, ZSCAN 200K / ZSCAN 1K): Frontend_Bound 25.8% → 21.6% / 24.6% → 19.1%, Retiring 35.7% → 28.5% / 33.4% → 27.6% — the long-double
printfwas an instruction- and fetch-heavy path; what remains of the now shorter call is proportionally more memory-bound (thedictScanpointer chasing). Throughput on that profiler-instrumented runner (n=1, counters running): +22.4% / +20.3%.Supplementary, non-integral scores (laptop, server pinned to one core, memtier on separate cores, 20 s runs, arms alternated, n=3 each; corroborates the 1K rows above): 200K-member skiplist, scores
i/7:ZSCAN k 0 COUNT 10001,164 (1,108–1,277) → 2,403 (2,402–2,572) ops/s (+106%); with all scores0, 1,799 (1,789–1,942) → 3,333 (3,161–3,523) (+85%).Note
Low Risk
Read-only reply formatting in one scan callback; behavior aligns with existing ZSCORE output and is covered by new regression tests.
Overview
ZSCAN on skiplist-encoded sorted sets now formats member scores with
d2string()(same shortest round-trip path as ZSCORE / ZMSCORE), instead ofld2string(..., LD_STR_AUTO)inscanCallback()’sOBJ_ZSETbranch. That removes the encoding-dependent mismatch (e.g.3.3vs3.2999999999999998) introduced when client replies moved to fpconv in 7.2 but this scan path was left on"%.17Lg". The stack buffer shrinks fromMAX_LONG_DOUBLE_CHARStoMAX_D2STRING_CHARS.Score strings may differ in notation from the old skiplist ZSCAN output (fixed vs scientific exponents, long integers) while still parsing to the same
double; listpack ZSCAN is unchanged (stored text). Newscan.tcltests walk ZSCAN for both listpack and skiplist encodings and assert every score is byte-identical to ZSCORE, including edge cases from #2175.Reviewed by Cursor Bugbot for commit cd4d272. Bugbot is set up for automated code reviews on this repo. Configure here.