Skip to content

ZSET B+ Tree PR 3: Replace skiplist with FB+ Tree implementation - #4206

Merged
ranshid merged 39 commits into
valkey-io:zset-btreefrom
rainsupreme:oi/pr3-fbtree-v2
Aug 6, 2026
Merged

ranshid merged 39 commits into
valkey-io:zset-btreefrom
rainsupreme:oi/pr3-fbtree-v2

Conversation

@rainsupreme

@rainsupreme rainsupreme commented Jul 18, 2026 •

Copy link
Copy Markdown
Contributor

This is the third PR for implementing B+ tree for ZSET (#3166). After this PR I will merge the feature branch into unstable and submit future enhancements to the unstable branch.

Overview

This PR replaces the skiplist with a B+ tree as the OrderedIndex backend. The skiplist is fully removed. The encoding name changes from "skiplist" to "btree" in OBJECT ENCODING responses.

The implementation is called "fbtree" — short for FB+ tree or Feature B+ Tree, a B+ tree variant that stores per-subtree feature metadata (subtree size, a few distinguishing bytes) in inner nodes to minimize memory fetches. See issue #3166 for details (design, performance, memory overhead)

What's included

New files:

  • fbtree.c — the B+ tree implementation (insert, delete, split, seek, iterate, defrag)
  • fbtree.h / fbtree_internal.h — public and internal headers

Modified files:

  • ordered_index.c — rewritten as the fbtree adapter
  • ordered_index.h — iterator size increased to fit fbtreeIterator
  • server.c / server.h — zsetExtractElement with packed-item detection; zsetMarkLookupKey/zsetUnmarkLookupKey for heterogeneous hashtable lookup; OBJ_ENCODING_BTREE
  • t_zset.c — lookup callsites wrapped with mark/unmark for hashtable operations
  • Makefile — skiplist.o removed, fbtree.o added
  • defrag.c — renamed defragZsetSkiplist → defragZset

Deleted files:

  • skiplist.c, skiplist.h

Reviewing guide

  • fbtree is written as a general-purpose ordered collection of strings. It only depends on sds for string operations.
  • ordered_index.c is the adapter which uses fbtree to implement the ZSET OrderedIndex interface (score packing, lex range sentinels, seek helpers).
  • hashtable does not currently support heterogeneous lookup, which is where the stored entry and the lookup key are different types. To avoid allocating a temporary packed entry for every hashtable lookup operation I've cooked up a scheme to mark the lookup string sds so hashtableType methods can differentiate between packed entry and simple lookup string. (Not pretty. I plan to separately propose hashtable changes to make this unnecessary.)

Suggested reading order

  1. fbtree.h (3 min) — public API surface
  2. fbtree_internal.h (3 min) — node layout, constants, internal structs
  3. fbtree.c (30 min) — core tree operations: leaf ops, inner node routing, splits, iteration/seek, defrag scan
  4. ordered_index.c (15 min) — adapter layer. Key areas: score-element packing, lex-range sentinel handling, seekForBound helper for positioned range operations, defrag bridge
  5. server.c changes (5 min) — zsetExtractElement packed-item path, mark/unmark helpers, zsetHashtableType
  6. Tests — encoding assertions updated from "skiplist" to "btree"; unit tests exercise orderedIndex functions directly (which now route to fbtree)

Design decisions

  • Node sizing — leaf nodes are 512 B and inner nodes 2048 B, tuned to exactly fill jemalloc size classes at 61-way fanout
  • No merge on delete — simplified initial implementation. Leaves can become sparse; background compaction is planned for a later PR
  • Score+element packed into sds — each leaf item is a single sds containing [8B score][element bytes], enabling standard sds comparison for tree ordering
  • Lookup-key marking — hashtable callbacks must distinguish packed fbtree items (skip 8B score prefix) from plain lookup keys. Solution: transiently mark lookup keys via sds aux bit (sdshdr8+) or unused type value (sdshdr5), check in zsetExtractElement, unmark after lookup
  • Sentinels handled in adapter — fbtree.c is a generic B+ tree with no ZSET knowledge; lex-range sentinel handling lives in the adapter
  • O(log N) defrag scan — uses rank-based tree navigation to resume from cursor position, processes 4 full leaves per call (~244 items)
  • Cursor-between-items iterator — fbtreeSeekToRank(N) positions cursor so next() returns rank N and prev() returns rank N-1, matching the Java ListIterator semantics established in PR 2

Testing

  • 275 unit tests pass — 193 new in test_fbtree.cpp (tree operations, SIMD feature search, property/fuzz tests), plus the 82 existing ordered-index tests from PR 2, unchanged and now exercising fbtree
  • 337 integration tests pass

Benchmark Results

ARM Graviton 3 (c7g.metal, 64 cores), io-threads=9, pipeline=10, 3M-member zset, 5 repetitions per test. 95% confidence intervals are ≤2% for all tests except ZRANDMEMBER (±5%, low absolute throughput).

Command Workload Skiplist fbtree Change
ZADD Score update (all hits) 205K 421K +105%
ZREM 50/50 random add/remove 386K 680K +76%
ZPOPMIN Pop min + append new member 1.08M 1.08M 0%
ZRANGE 100-element rank scan 174K 183K +6%
ZRANGEBYSCORE 100-element score scan 95K 96K +1%
ZRANDMEMBER 100 random samples 5.0K 5.1K +2%
ZRANK Point rank lookup 692K 825K +19%
ZCOUNT Score-range count 720K 917K +27%
ZSCORE Point score lookup (hashtable) 1.44M 1.46M +2%

Mutations that reposition elements in the ordered index (ZADD, ZREM) show the largest gains from improved cache locality. ZRANK and ZCOUNT benefit from O(log N) rank arithmetic. Scan operations are unchanged, as expected. ZSCORE and ZRANDMEMBER are hashtable-based and perform identically since the ordered index is not involved. Range operations are unchanged because they are bottlenecked by the large reply size.

Memory Overhead

5M-member zset, 20-byte members (+ 8-byte score), measured via jemalloc heap profiling. Overhead is per-member bytes excluding user data, measured across two insertion patterns that produce different ordered-index occupancy:

Insertion pattern Skiplist fbtree Savings
Sequential (ascending scores, dense packing) 50.3 B 28.5 B −21.8 B (−43%)
Random (scattered scores, fresh fill) 50.3 B 32.0 B −18.2 B (−36%)

fbtree overhead scales with leaf occupancy: dense sequential fills pack tightest (−43%), while scattered random fills leave leaves partially full (−36%). The skiplist stays flat at ~50.3 B across both patterns, since per-node cost is independent of insertion order and every node is freed on delete.

Scope boundaries

This PR does NOT:

  • Implement merge/compaction (planned for a later PR)
  • Add the heterogeneous hashtable lookup that would make the sds marking scheme unnecessary (separate PR)

Remove skiplist entirely and implement OrderedIndex directly with fbtree.
This absorbs what was previously planned as PR 3 + PR 4 + PR 5.

Key changes:
- ordered_index.c is now the fbtree adapter (no vtable, no dispatch)
- ordered_index.h unchanged (public API preserved from PR 2)
- OBJ_ENCODING_SKIPLIST -> OBJ_ENCODING_BTREE
- Lookup-key marking (zsetMarkLookupKey/zsetUnmarkLookupKey) for
  hashtable callback disambiguation between packed items and plain sds
- Defrag: void*(*defragfn)(void*) with sdsAllocPtr offset dance in fbtree.c
- Iterator: cursor-between-items model (fbtreeSeekToRank gives correct
  semantics for next()/prev() without adaptation)
- Unit tests for the tree (FbtreeTest) and its SIMD feature search
  (FeatureSearchTest)

Bug fix: zsetExtractElement must handle SDS_TYPE_5 marked keys specially
since sdslen() returns 0 when type bits are overwritten with the marker.
Read length directly from flags byte for marked SDS_TYPE_5.

337/337 zset integration tests pass.
All aofrw, keyspace, sort, memefficiency, scan tests pass.

Signed-off-by: Rain Valentine <[email protected]>
Replace the linear leaf-walk (O(range size)) with two rank-returning
seeks: count = end_rank - start_rank. fbtreeSeekToScore returns the rank
of the first item >= the seek key in O(log N) using child_sizes descent.

Boundary handling:
- min inclusive -> seek(min); min exclusive -> seek(min+1)
- max exclusive -> seek(max); max inclusive -> seek(max+1)
where +1 is the next representable score in sortable space.

No iterator step-back needed (that's only for cursor positioning, not
rank counting). +inf upper bound is safe: seeking past tree max clamps
to length, giving the correct end_rank.

337/337 integration, all unit tests pass.

Signed-off-by: Rain Valentine <[email protected]>
fbtreeSeekToValue now returns the rank of the first item with packed
value >= the seek key (mirroring fbtreeSeekToScore), accumulated from
child_sizes during the existing tree descent. Existing void-discarding
callers are unaffected.

orderedIndexCountLexRange now computes count = end_rank - start_rank
instead of walking leaves. Since lex elements are variable-length (no
'next representable' value), inclusive/exclusive boundaries are resolved
by probing whether the bound itself is present via fbtreeGetAtRank +
sdscmp -- correct because packed (score,element) values are unique.

337/337 integration, all unit tests pass. ZLEXCOUNT boundary smoke test
covers inclusive/exclusive ends, sentinels, single-element and empty
ranges.

Signed-off-by: Rain Valentine <[email protected]>
The three lex-range functions (DeleteRangeByLex, CountLexRange,
SeekToLexRange) grab the shared score prefix from the lowest element.
fbtreePeekMin reads leftmost_leaf->values[0] directly (O(1)) instead of
fbtreeGetAtRank(fbt, 0)'s full root-to-leaf descent (O(log N)). Same
result, clearer intent.

Signed-off-by: Rain Valentine <[email protected]>
fbtreeNext/fbtreePrev previously returned bool and wrote the element
through a `const_sds *pos` out-parameter. Return the element directly
(const_sds, NULL on end-of-iteration) instead.

Rationale:
- Matches the convention already used by orderedIndexNext/Prev and
  fbtreeGetAtRank, so the internal iterator reads the same as every
  consumer (`while ((x = fbtreeNext(&it)) != NULL)`).
- Collapses the orderedIndexNext/Prev wrappers from a bool->pointer
  bridge to a single cast.
- Removes a redundant return channel: the bool and the out-parameter
  both encoded end-of-iteration. NULL is an unambiguous sentinel here
  because stored elements are always valid sds and can never be NULL.

Behavioral note: the old bool form left *pos untouched on a false
return. MultilevelMixedIteration relied on that to read the last
element after draining a loop; with the new form that value is NULL,
so the test now captures the last non-null element explicitly. All
other call sites are mechanical conversions to the assignment-in-
condition idiom and ASSERT_NE/ASSERT_EQ against nullptr.

Tested: 161 FbtreeTest, 82 ordered-index unit tests, 337 zset
integration tests; build and clang-format clean.

Signed-off-by: Rain Valentine <[email protected]>
… leaf searches

Extract the shared root->split boundary-path descent into buildBoundaryPaths(),
used by both range-delete and the new range-count path, so the two are no longer
duplicated. Add fbtreeCountRangeByScore/fbtreeCountRangeByValue which count via a
single shared descent plus rank arithmetic (end_rank - start_rank) instead of two
independent seeks, and wire ZCOUNT (orderedIndexCountScoreRange) and ZLEXCOUNT
(orderedIndexCountLexRange) to them.

For the common split case (the two range boundaries land in different leaves),
resolve the two leaf indices with resolveBothIdxPrefetch(): a single-threaded,
software-pipelined pair of binary searches that interleaves the two independent
leaves and prefetches both probe payloads per step, so their cache misses overlap
(memory-level parallelism) instead of serializing. Each leaf value is a pointer to
a separate sds allocation, so every probe is a miss; this targets the ZCOUNT
regression on wide-ish ranges whose boundaries span separate leaves.

Delete callers now resolve start_idx/end_idx after the descent (behavior
unchanged; buildBoundaryPaths is descent-only).

Tests: 243 fbtree + ordered-index unit tests pass; zset ZCOUNT/ZLEXCOUNT
integration tests pass; build clean under -Werror -Wpedantic -Wextra with
ORDERED_INDEX_FBTREE=yes.

Signed-off-by: Rain Valentine <[email protected]>
…verse results

fbtreeNext/fbtreePrev only set ITER_AT_POSITION in their first-call
(current_leaf == NULL) branch. When the iterator was positioned by a seek that
landed on the leftmost element, fbtreeSeekToValue/fbtreeSeekToScore leave it in
ITER_BEFORE_START; fbtreeNext then returns the element but never clears that
state, so a following fbtreePrev early-returns NULL and the element is dropped.
Symmetric issue for ITER_PAST_END with fbtreePrev-then-fbtreeNext.

This surfaced as ZREVRANGEBYLEX/ZREVRANGEBYSCORE returning [] instead of the
smallest matching element whenever the range resolves to just that element
(e.g. `ZREVRANGEBYLEX z [a [a` with a as the rank-0 member), and as an
intermittent failure of the ZRANGEBYLEX fuzzy integration test.

Fix: set ITER_AT_POSITION whenever Next/Prev returns a valid element. Being at
the iterator level, this covers every seek-then-reverse caller.

Adds targeted unit tests for the boundary-transition family: seek-to-rank-0 then
Next-then-Prev (score and value seeks, incl. the sole-element case), seek below
all, and the PAST_END Prev-then-Next symmetric case. Reverting the fix fails
three of them.

Tests: 249 unit tests pass; a seeded ZRANGEBYLEX/ZLEXCOUNT fuzzer runs ~1.5M
queries clean across 5 seeds (incl. one that reproduced the failure pre-fix);
zset integration green over repeated runs.

Signed-off-by: Rain Valentine <[email protected]>
Add fbtreeHeight() which descends the leftmost spine in O(height), and
use it to replace the orderedIndexGetHeight stub that returned 0. Add a
unit test covering empty, single-leaf, leaf-overflow, and three-level
trees.

Signed-off-by: Rain Valentine <[email protected]>
The forward+exclusive and reverse+inclusive cases re-sought the tree
(a second O(log n) descent) to position the cursor after peeking the
boundary element. A single O(1) fbtreePrev() step lands in the same
place. Also document the offset-encoding convention shared by
orderedIndexSeekToScoreRange and orderedIndexSeekToLexRange.

Signed-off-by: Rain Valentine <[email protected]>
Merge test_fbtree_feature_search.cpp into test_fbtree.cpp, matching the
one-test-file-per-module convention in src/unit. The FeatureSearchTest
suite name is unchanged, so filtering and output grouping still work.
The local FEATURE_SIZE/FEATURE_ROW_SIZE/FEATURE_BIAS definitions are
dropped in favor of fbtree_internal.h (already included), and a stale
comment referencing fbtree_ordered_index.c is corrected to fbtree.c.

Signed-off-by: Rain Valentine <[email protected]>
@coderabbitai

coderabbitai Bot commented Jul 18, 2026 •

Copy link
Copy Markdown
Contributor

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 0c4a6380-d888-412d-aa49-ec2ce7879c90

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@rainsupreme rainsupreme moved this from Todo to Needs Review in Valkey 9.2 Jul 18, 2026
@codecov

codecov Bot commented Jul 18, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 98.62306% with 8 lines in your changes missing coverage. Please review.
✅ Project coverage is 78.28%. Comparing base (ea14eeb) to head (d3f4b61).
⚠️ Report is 1 commits behind head on zset-btree.

Files with missing lines Patch % Lines
src/module.c 0.00% 7 Missing ⚠️
src/ordered_index.c 99.56% 1 Missing ⚠️
Additional details and impacted files
@@              Coverage Diff               @@
##           zset-btree    #4206      +/-   ##
==============================================
+ Coverage       76.92%   78.28%   +1.35%     
==============================================
  Files             164      166       +2     
  Lines           80707    87109    +6402     
==============================================
+ Hits            62082    68190    +6108     
- Misses          18625    18919     +294     
Files with missing lines Coverage Δ
src/aof.c 80.33% <100.00%> (+0.02%) ⬆️
src/db.c 94.69% <100.00%> (-0.27%) ⬇️
src/debug.c 55.24% <100.00%> (+0.07%) ⬆️
src/defrag.c 80.13% <100.00%> (-1.00%) ⬇️
src/fbtree.c 95.52% <ø> (ø)
src/geo.c 94.35% <100.00%> (+0.03%) ⬆️
src/lazyfree.c 88.38% <100.00%> (ø)
src/object.c 92.51% <100.00%> (+3.94%) ⬆️
src/rdb.c 76.78% <100.00%> (-0.19%) ⬇️
src/server.c 89.57% <100.00%> (+0.04%) ⬆️
... and 8 more

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

@valkey-review-bot valkey-review-bot Bot 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 found three regressions in the new BTREE backend: signed-zero scores stop behaving like numeric zero in score-range operations, the child-save dismiss path no longer releases packed member payloads, and identical packed duplicates can become unreachable after a leaf split.

Comment thread src/ordered_index.c Resolved
Comment thread src/fbtree.c Resolved
Comment thread src/fbtree.c Resolved
@rainsupreme
rainsupreme marked this pull request as ready for review July 18, 2026 09:03
…ting

Three fixes for the regressions found in review:

1. Canonicalize negative zero in scoreToSortable. IEEE -0.0 and +0.0
   have different bit patterns, so the sortable-key transform mapped
   them to distinct tree keys while the previous skiplist compared
   scores numerically (-0 == 0). Members stored with score -0 became
   invisible to ZRANGEBYSCORE/ZCOUNT/ZREMRANGEBYSCORE on 0. Collapse
   -0.0 to +0.0 before packing.

2. Dismiss packed member payloads in fbtreeDismissMemory. The packed
   items are allocated separately from the 512-byte leaf nodes, so
   madvising only the leaves released nothing; dismissZsetObject gates
   dismissal on page-sized average member size, which is exactly when
   the member payloads dominate. Walk each leaf's items and dismiss the
   sds allocations as well.

3. Route duplicate lookups through equal-anchor siblings. Leaf items
   are matched by pointer identity, but the descent routed only to the
   leftmost child with an equal anchor. After a leaf split, duplicate
   (score, element) pairs span sibling leaves, leaving later instances
   unreachable for delete and rank lookup. Continue into the next
   sibling while the child's high key equals the item value.

Each fix comes with a unit test that fails without it (signed-zero
range ops, dismiss walk integrity, several-leaves-of-duplicates rank
and pointer-delete), plus a zset integration test for signed zero.

Signed-off-by: Rain Valentine <[email protected]>
The ENGINE_SERVER_OBJ list is alphabetically ordered. skiplist.o was
originally placed next to t_zset.o, ordered_index.o inherited that slot
through the rename, and fbtree.o followed the same pattern. Move both
to their alphabetical positions.

Signed-off-by: Rain Valentine <[email protected]>
Comment thread src/ordered_index.c Resolved
Comment thread src/fbtree.c Resolved
Comment thread src/fbtree.c Resolved
Comment thread src/ordered_index.c Resolved
The maxstring sentinel packed to [score][0xFF] as an upper bound, but a
single byte suffix cannot dominate its own continuations under sds
comparison: a strict prefix sorts before longer strings, so members with
a leading 0xFF byte sorted past the bound and escaped
ZREMRANGEBYLEX key - + and were undercounted by ZLEXCOUNT. Bound the
range with the next score bucket's 8-byte prefix, exclusive, instead.
Valid scores never map to an all-ones sortable value (that bit pattern
is a NaN), so the increment cannot wrap.

Adds a unit test with 0xFF-leading members (fails without the fix) and
a zset integration test.

Signed-off-by: Rain Valentine <[email protected]>
The score-only descent extracted FEATURE_SIZE feature bytes starting at
prefix_len without checking against the 8-byte key length. With a
shared anchor prefix of 5 to 7 bytes (clustered scores differing only
in their low mantissa bytes), the extraction read up to 3 bytes past
the caller's 8-byte score buffer. Pad positions past the key with zero,
the minimal continuation, matching findChildIndex's handling of keys
shorter than the feature window.

Adds a test inserting adjacent representable doubles so inner-node
anchors share most of their score prefix; AddressSanitizer reports a
stack-buffer-overflow at the extraction site without the fix.

Signed-off-by: Rain Valentine <[email protected]>
Inner nodes whose shared prefix exceeds EMBED_PREFIX_LEN store it in a
separately allocated buffer (innerNodeSetPrefix). freeNodeRecursive
releases that buffer, but the five paths that free an emptied child
directly — the two pointer-delete empty-child sites and the three
range-delete boundary sites — called zfree on the node alone, orphaning
the buffer. Release the spilled prefix before freeing inner nodes at
all five sites. Dying boundary leaves are unlinked by the range
delete's leaf-stitching pass, so the boundary sites only need the
inner-node prefix release.

Adds two tests that collapse a three-level tree of long-prefix members
through both paths; LeakSanitizer reports the orphaned buffers without
the fix.

Signed-off-by: Rain Valentine <[email protected]>
The estimator returned a hardcoded 64 bytes per item, ignoring the
sample_size that MEMORY USAGE forwards and losing the sampling accuracy
the skiplist estimator provided. Sample item allocation sizes from the
leftmost leaves and extrapolate, and add structural overhead from the
leaf count (exact when the sample covers the whole chain, otherwise
extrapolated from the sampled fill) with inner levels shrinking
geometrically by the fanout.

Adds a test verifying estimates scale with actual member sizes.

Signed-off-by: Rain Valentine <[email protected]>
Move payload sampling from the ordered index into objectComputeSize,
matching the pattern the hash and set encodings use: containers report
their structural memory (hashtableMemUsage, and now
orderedIndexEstimateStructureMemory), while the object layer samples
element payloads. The hashtable entries are the packed items shared
with the ordered index, so sampling them covers the member payloads,
and hashtable order does not correlate with member size the way the
index's score order can.

The ordered index API drops the sample_size parameter: item payloads
are owned jointly with the caller, so the index only accounts for its
own nodes, with leaf count extrapolated from the fill of the first
leaves.

Adds a MEMORY USAGE integration test and reworks the estimator unit
test for the structural contract.

Signed-off-by: Rain Valentine <[email protected]>
Raw ASSERT_EQ on doubles instantiates gtest's operator== comparison,
which trips -Werror -Wfloat-equal on the macOS build. The fixture's
assertScore helper (ASSERT_DOUBLE_EQ) is the established comparison for
scores.

Signed-off-by: Rain Valentine <[email protected]>
Comment thread src/fbtree.c Resolved
Comment thread src/ordered_index.c Resolved
Comment thread src/fbtree.c Resolved
The active-defrag tests are written for readability by C developers (per
CONTRIBUTING): verify order and content by iterating the tree with
fbtreeNext/fbtreePrev and comparing each element to a formatted key
buffer via memcmp, using fixed C arrays and index loops. Drops the
std::vector/std::string expectations, std::reverse, and
range-for-over-initializer-list the first drafts used.

Signed-off-by: Rain Valentine <[email protected]>
qsort declares its array argument non-null, so calling it with a NULL
pointer and count 0 (as the full-delete consistency tests do once the
index is empty) is undefined behavior that UBSan flags. Return early
when the count is zero.

Signed-off-by: Rain Valentine <[email protected]>
NodeAllocationSizes asserts zmalloc_usable_size returns the exact
jemalloc size class for the node structs. Other allocators round
differently (e.g. glibc returns 2056/520), so skip it unless jemalloc is
in use, matching the existing jemalloc-only test guard in test_entry.cpp.

Signed-off-by: Rain Valentine <[email protected]>
orderedIndexItemCreate, orderedIndexItemSetScore, and orderedIndexInsertItem back the store-aggregation path (ZUNIONSTORE/ZDIFFSTORE/ZINTERSTORE) but had no direct unit coverage; ItemSetScore's no-reposition branch is reachable only through this path. Cover create-then-set-then-insert ordering, interleaving with existing items, and a multi-level tree build.

Signed-off-by: Rain Valentine <[email protected]>
orderedIndexDefragInternals and orderedIndexScanDefrag are the wrappers defrag.c calls, but only the fbtree-layer primitives had gtests. Cover the two-phase sweep with a force-relocate defragfn (struct, nodes, leaves, and items all move), the item-relocation callback firing once per moved item, a no-op defragfn, and the empty-index case.

Signed-off-by: Rain Valentine <[email protected]>
The OrderedIndex is fbtree-backed; the overview comment still described it as a skiplist with the B+ tree "planned", and the height getter referred to "skiplist levels".

Signed-off-by: Rain Valentine <[email protected]>
Comment thread src/fbtree.c Resolved

@ranshid ranshid left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

made a highlevel pass following @eifrah-aws detailed review (THANK YOU ERAN!)
I think my main concern is related to the defrag:
Phase 1 relocates all inner nodes in a single, non-time-sliced pass. Since the inner-node count is linear in the set size (≈ N/fanout²), a very large zset makes this a Θ(N) burst on the event loop — which breaks active-defrag's incremental contract that every other path honors.What are the plans around that? can we see a benchmark of how much burst we expect for a very large sorted sets? (I know some users have millions of items in a sorted set)

Comment thread src/object.c
case OBJ_ENCODING_LISTPACK: return "listpack";
case OBJ_ENCODING_INTSET: return "intset";
case OBJ_ENCODING_SKIPLIST: return "skiplist";
case OBJ_ENCODING_BTREE: return "btree";

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This is a minor 'breaking change". I am wondering if this would create a problem for some external applications using this in order to debug/validate something. Maybe we could leave it as "skiplist" till valkey 10? (@valkey-io/core-team do you feel this is a justified breaking change?)

@madolson madolson Aug 3, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

In the past we changed ziplist to listpack in a minor version and nobody complained.

I don't think this is a breaking change. As per the docs, it can be one of these values: https://valkey.io/commands/object-encoding/. So we're just adding a new type, in a backwards compatible way.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

In the past we changed ziplist to listpack in a minor version and nobody complained. So I don't think this is a breaking change.

I think the fact that "nobody complained" does not make it a non-breaking change by definition. I do agree that maybe this is not going to be a REAL problem.

So we're just adding a new type, in a backwards compatible way.

Well we are only "adding a type", but existing sorted sets which used to return "skiplist" will now return "btree", so if THEORETICALLY you have a user/workload verifying his sortedsets are not using the "slow" listpacks and he does assert OBJECT ENCODING key == skiplist instead of assert OBJECT ENCODING key != listpack, will face a problem.

I guess, as you mentioned past changes, I am fine with doing that, I just think it require us to be aware and move via major-decision process.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think the fact that "nobody complained" does not make it a non-breaking change by definition. I do agree that maybe this is not going to be a REAL problem.

I merged two disjoint thoughts. I mostly think it's not a breaking change based on the doc definition.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

O.K I think that this is something we can also decide during RC phase so it is probably not a blocker

Relocating every inner node in one pass takes milliseconds on very large sorted sets (a 100M-item zset has ~30K inner nodes, ~60MB), far exceeding the active-defrag cycle budget. Instead, relocate each inner node in the scan call that visits its leftmost descendant leaf: the descent path is already in hand, each inner node is attempted exactly once per sweep, and each call relocates at most tree-depth nodes, so per-call work stays bounded regardless of tree size.

Remove fbtreeDefragNodes; orderedIndexDefragInternals now relocates only the top-level struct. The inner-node test asserts exactly-once relocation accounting (inner nodes + leaves + items) across single-leaf, two-level, and three-level trees.

Signed-off-by: Rain Valentine <[email protected]>
An inner node whose shared prefix exceeds EMBED_PREFIX_LEN stores the prefix in a separate heap block, which the node relocation carried over by pointer without ever passing it to defragfn, leaving one un-defraggable allocation per spilled-prefix inner node. Relocate the block just before its owning node so the node's verbatim copy carries the updated pointer.

The test asserts exactly-once accounting including spilled prefix blocks and re-descends by value through the relocated prefixes; it fails without the fix (relocation count short by the spilled-block count, and AddressSanitizer flags the stale prefix pointer).

Signed-off-by: Rain Valentine <[email protected]>
The dismiss walk covered only the leaf chain, so inner-node structs and their spilled long-prefix heap blocks were never hinted to the OS during lazy-free. Walk the inner nodes as well, dismissing each spilled prefix block and node struct, children before parent so the walk never reads a node after hinting it away.

Signed-off-by: Rain Valentine <[email protected]>
Direct node-to-innerNode downcasts trip clang's -Wcast-align (alignment 1 to 8) on the macOS CI job; casting through void pointer is the established idiom in this file.

Signed-off-by: Rain Valentine <[email protected]>
orderedIndexDismissMemory's comment claimed the index remains usable after dismissal with pages faulted back in on next access; MADV_DONTNEED on anonymous pages zero-fills them instead, and the caller is the fork child after serialization, not lazy-free. State the real contract: call only when the process will not read the index again.

Rework the dismiss test accordingly: reading the index after dismissal exercises nothing (the hint is a no-op without jemalloc, and sub-page allocations are never released), so count zmadvise_dontneed calls through the unit-test wrapper instead -- at least one hint per item.

Signed-off-by: Rain Valentine <[email protected]>
The -Wframe-larger-than cap also applies to the compiler-generated static initialization function, whose frame grows with the number of TEST_F registrations in a file: under -fsanitize=address each registration's temporaries get their own redzoned stack slot, roughly 150 bytes per test on current toolchains and more on older ones. The previous 32K cap therefore limited a test file to roughly 200 tests under the address-sanitizer build, which test_fbtree.cpp now exceeds. 128K accommodates large test files while still catching runaway frames in hand-written code.

Signed-off-by: Rain Valentine <[email protected]>
@rainsupreme

Copy link
Copy Markdown
Contributor Author

I think my main concern is related to the defrag: Phase 1 relocates all inner nodes in a single, non-time-sliced pass. Since the inner-node count is linear in the set size (≈ N/fanout²), a very large zset makes this a Θ(N) burst on the event loop — which breaks active-defrag's incremental contract that every other path honors.What are the plans around that? can we see a benchmark of how much burst we expect for a very large sorted sets? (I know some users have millions of items in a sorted set)

@ranshid I haven't benchmarked it, but given that the number of inner nodes and thus time complexity is indeed O(n) I think it doesn't scale well in any case (if not now then it'll be a future problem), so I've refactored defragmentation to be entirely incremental.

@rainsupreme rainsupreme added the run-extra-tests Run extra tests on this PR (Runs all tests from daily except valgrind and RESP) label Aug 3, 2026

@ranshid ranshid left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Overall LGTM - thank you @rainsupreme!

I mostly did a highlevel review of the areas which I think might break.
Most of the detailed review heavy-lifting was done by @eifrah-aws (Thank you!)

@ranshid
ranshid merged commit 68307ab into valkey-io:zset-btree Aug 6, 2026
27 checks passed
@github-project-automation github-project-automation Bot moved this from Needs Review to Merged in Valkey 9.2 Aug 6, 2026
madolson pushed a commit that referenced this pull request Aug 11, 2026
The sorted set skiplist is replaced with a B+ tree behind a new ordered-index
abstraction.

skiplist.c and skiplist.h are deleted, zskiplist and zskiplistNode are
removed from server.h, and zset now holds a hashtable plus an
OrderedIndex. OBJ_ENCODING_SKIPLIST becomes OBJ_ENCODING_BTREE (same
value, 7), so OBJECT ENCODING on a large sorted set answers "btree"
instead of "skiplist". The listpack encoding for small sorted sets is
untouched.

Two new layers replace it:

* ordered_index.[ch] (802 lines) is the API the rest of the server
  speaks: insert, delete, update score, pop, seek, iterate, count and
  delete by score, lex and rank range, defrag, memory estimation. Every
  skiplist call site in t_zset.c, object.c, rdb.c, aof.c, geo.c, sort.c,
  db.c, debug.c, defrag.c, lazyfree.c, module.c and valkey-check-rdb.c
  now goes through it, so nothing outside this file knows what the
  backing structure is. t_zset.c loses 643 lines net.

* fbtree.[ch] plus fbtree_internal.h (2,811 lines) is the tree itself.
It
  is a general-purpose ordered collection of sds with no ZSET knowledge,
  depending only on sds for string operations. Score packing, lex range
  sentinels and positioned seeks all live in the adapter.

Details worth knowing when reading it:

Items are packed sds. Each entry is a single allocation holding
[8-byte sortable score][element bytes], so plain sdscmp gives correct
ZSET ordering. The score is a big-endian sign-flipped IEEE 754 double,
with -0.0 normalized to 0.0 so equal scores map to one key.

Node layout is 61-way fanout, with leaves at exactly 512 B and inner
nodes at exactly 2048 B on 64-bit, enforced by static_assert so they
land
in jemalloc size classes. Inner nodes carry per-child subtree sizes for
O(log N) rank arithmetic, and 4 bytes of discriminating "feature" per
child in a cache-line-aligned row, searched with AVX2 or NEON (scalar
fallback) to route a lookup without touching the child.

Hashtable lookups need disambiguation. The hashtable stores packed items
but is looked up with a plain sds, and hashtable has no heterogeneous
lookup. Rather than allocating a temporary packed key per lookup, lookup
keys are transiently marked via an sds aux bit (sdshdr8+) or a spare
type
value (sdshdr5), and zsetHashFunction/zsetKeyCompare check the mark to
decide whether to skip the 8-byte score prefix. This is not pretty; the
plan is to add real heterogeneous lookup to hashtable and delete the
scheme.

Delete does not merge. Empty nodes are freed and a single-child root
collapses, but sibling leaves are never rebalanced, so leaves can stay
sparse after heavy deletion. Background compaction is a follow-up, along
with 32-bit node sizing (NODE_SIZE is tuned for 64-bit, so 32-bit nodes
fit their size class but do not fill it).

Defrag is rank-based. defragZsetSkiplist becomes defragZset and drops 52
lines. The cursor is a rank, resumed by O(log N) tree navigation,
processing 4 full leaves (~244 items) per call.

Compatibility:

* No RDB or AOF format change. Saves still use RDB_TYPE_ZSET_2 and
  RDB_TYPE_ZSET_LISTPACK, and loading is unchanged apart from converting
  to OBJ_ENCODING_BTREE.
* No config change and no module API change. valkeymodule.h is
untouched.
  Module zset range iteration is reimplemented on the ordered-index
  iterator with the same semantics.
* OBJECT ENCODING is the only user-visible reply change.
* Two DEBUG outputs change: the crash report logs "Index height: N"
  instead of "Skiplist level: N", and DEBUG HTSTATS-KEY and DEBUG DIGEST
  follow the new encoding constant.

Performance, measured on the branch in #4206 and not re-run for this
merge. ARM Graviton 3 (c7g.metal, 64 cores), io-threads=9, pipeline=10,
3M-member zset, 5 repetitions, 95% CI within 2% except ZRANDMEMBER (5%):

  Command         Workload                     Skiplist   fbtree  Change
  ZADD            Score update, all hits           205K     421K   +105%
  ZREM            50/50 random add/remove          386K     680K    +76%
  ZCOUNT          Score-range count                720K     917K    +27%
  ZRANK           Point rank lookup                692K     825K    +19%
  ZRANGE          100-element rank scan            174K     183K     +6%
  ZRANDMEMBER     100 random samples               5.0K     5.1K     +2%
  ZSCORE          Point score lookup              1.44M    1.46M     +2%
  ZRANGEBYSCORE   100-element score scan            95K      96K     +1%
  ZPOPMIN         Pop min, append new member      1.08M    1.08M      0%

Mutations that reposition an element gain the most from cache locality.
Range scans are bottlenecked on reply size. ZSCORE and ZRANDMEMBER are
hashtable paths and do not involve the ordered index.

Memory, 5M members, 20-byte elements, via jemalloc heap profiling.
Per-member overhead excluding user data:

  Insertion pattern            Skiplist   fbtree         Savings
  Sequential, dense packing       50.3 B   28.5 B   -21.8 B (-43%)
  Random, fresh fill              50.3 B   32.0 B   -18.2 B (-36%)

fbtree overhead tracks leaf occupancy. The skiplist is flat at ~50.3 B
regardless of insertion order. Since there is no compaction yet,
delete-heavy workloads will land worse than either number here.

Testing:

* 302 new gtest cases: 205 in test_fbtree.cpp (tree operations, SIMD
  feature search, property and fuzz tests) and 97 in
  test_ordered_index.cpp, which exercise the interface contract and were
  written against the skiplist before fbtree existed. They passed
  unchanged against the new backend, which is the main evidence the swap
  is behavior preserving.
* 21 new tcl tests, 18 in tests/unit/type/zset.tcl and 3 in
  tests/unit/sort.tcl, mostly forcing the non-listpack encoding on paths
  that previously only ran on small sets.
* New with_config helper in tests/support/util.tcl that restores the old
  value even when the body fails.
* assert_encoding skiplist becomes assert_encoding btree in 5 test
files.
* -Wframe-larger-than in src/unit/Makefile goes from 32K to 128K. The
cap
applies to the compiler-generated static initializer, whose frame grows
  with the number of gtest registrations under ASan, and test_fbtree.cpp
  has 205 of them.

Also included beyond the three feature PRs:

* #4363, a strict-aliasing miscompilation of the fbtree iterator.
fbtreeIterator is uint64_t[3] accessed through an internal struct, which
  is UB everywhere and got miscompiled on Alpine gcc 15.2 at -O3 -flto:
  the optimizer elided the seek stores, so ZRANGE and ZREVRANGE returned
  results as if the iterator had never been seeked. Fixed with
  __attribute__((may_alias)) on the internal struct.
* fbtree.c included both assert.h and serverassert.h, and only produced
  server crash reports because serverassert.h happened to come last and
  reclaim the macro. assert.h is dropped and the include order is
  documented.
* "Ba" and "Addd" added to the typos allowlist, both from fbtree test
  fixtures.

---------

Signed-off-by: Rain Valentine <[email protected]>
Signed-off-by: Rain Valentine <[email protected]>
Co-authored-by: Rain Valentine <[email protected]>
madolson added a commit to jsoref/valkey that referenced this pull request Aug 11, 2026
Resolve the conflict caused by upstream drift since this PR was opened:

- src/t_zset.c: the skiplist was replaced by a B+ tree (valkey-io#4206, valkey-io#4359), so
  zslUpdateScore and its "Otherwise the skiplist is modified by removing
  and re-adding a new element" comment no longer exist. Drop that hunk.
  The other two t_zset.c hunks (zsetRank and addZpopInitialReply) still
  apply unchanged.

Additionally fix seven sentence-initial "Otherwise" sites added upstream
after this PR was opened, so the sweep is complete against unstable:

- src/cluster_legacy.c, src/config.c, src/dict.h, src/functions.c,
  src/modules/lua/engine_lua.c, src/replication.c, src/vset.c

Verified: builds clean, clang-format-18 reports no diff across the 45
changed src/ files, server starts, ZADD/ZRANGE/ZPOPMIN/ZRANK behave
normally. Every edit lands inside a comment except the valkey-cli --help
text. Zero sentence-initial "Otherwise" without a comma remain.

Signed-off-by: Madelyn Olson <[email protected]>
@madolson madolson mentioned this pull request Aug 11, 2026
@madolson madolson added breaking-change Indicates a possible backwards incompatible change and removed breaking-change Indicates a possible backwards incompatible change labels Sep 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

run-extra-tests Run extra tests on this PR (Runs all tests from daily except valgrind and RESP)

Projects

Status: Merged

Development

Successfully merging this pull request may close these issues.

4 participants