Repository navigation
[Draft] ZSET (ordered index) interface and UTs for zskiplist - preparation to replace zskiplist with fbtree - #3552
rainsupreme wants to merge 13 commits into
Conversation
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## unstable #3552 +/- ##
============================================
+ Coverage 76.34% 76.81% +0.47%
============================================
Files 159 164 +5
Lines 80045 82236 +2191
============================================
+ Hits 61107 63172 +2065
- Misses 18938 19064 +126
🚀 New features to boost your workflow:
|
Cherry-picked from zset-ordered-index-interface branch. Resolved hashtable API conflicts (no key_len on unstable). Signed-off-by: Rain Valentine <[email protected]>
Cherry-picked from zset-ordered-index-interface branch. Signed-off-by: Rain Valentine <[email protected]>
Cherry-picked from zset-ordered-index-interface branch. Resolved hashtable API conflict (no key_len on unstable). Signed-off-by: Rain Valentine <[email protected]>
…nterface where possible Adapted from zset-ordered-index-interface branch. Key differences from source branch: hashtable callbacks use unstable API signatures (no key_len parameters), zsetHashtableType moved to t_zset.c with entryGetKey returning SDS element directly. Signed-off-by: Rain Valentine <[email protected]>
Signed-off-by: Rain Valentine <[email protected]>
Signed-off-by: Rain Valentine <[email protected]>
Signed-off-by: Rain Valentine <[email protected]>
Signed-off-by: Rain Valentine <[email protected]>
Signed-off-by: Rain Valentine <[email protected]>
Signed-off-by: Rain Valentine <[email protected]>
…ed in OrderedIndexItem for incoming fbtree implementation, misc cleanup Signed-off-by: Rain Valentine <[email protected]>
Signed-off-by: Rain Valentine <[email protected]>
Signed-off-by: Rain Valentine <[email protected]>
727028a to
3ef0d62
Compare
|
@JimB123 no need to review this whole thing - I'm breaking it down into a series of 6 PRs, but what do you think of the interface? |
| @@ -0,0 +1,187 @@ | |||
| #ifndef ORDERED_INDEX_H | |||
There was a problem hiding this comment.
I love to see good documentation in the .h file. This is the API.
Other's have argued that in Valkey, a lot of the documentation should be in the .c file. However we don't have a .c file here - so that's a non-issue.
Please:
- Add a good header comment. A big block comment at the top which explains what an "ordered index" is. Talk about the high level concepts of what this data structure provides (but don't explain how it is implemented).
- Please add a comment on each of the API functions (below). For each function, give a few lines explaining what it does, and what the parameters represent.
| #include "skiplist_ordered_index.h" | ||
|
|
||
| /* Lifecycle */ | ||
| static inline OrderedIndex *orderedIndexCreate(void) { |
There was a problem hiding this comment.
As I commented in the recent review on dict, I think we should avoid the use of static inline. In the past, this was a good technique to improve performance. But now, with LTO, the benefit isn't there. And though in the code below, each function resolves to a single call to another function - that might not always be the case. It also serves as an example to others - who might not use static inline so carefully.
When misused, this technique can cause code bloat, increasing pressure on L1 cache. (and when used properly provides no benefit over LTO)
There was a problem hiding this comment.
The intent here was to have compile-time dispatch for the interface, such that these are reduced to the implementation function calls they wrap at compile time. This is intended as a more performant alternative to run-time dispatch via a vtable.
That said, you make a good point about setting examples and establishing norms. the same functions in a .c file should get cleaned up by LTO similarly. non-LTO builds will have a small penalty.
| } | ||
|
|
||
| /* Modification */ | ||
| static inline OrderedIndexItem *orderedIndexInsertRaw(OrderedIndex *idx, double score, const char *ele, size_t len) { |
There was a problem hiding this comment.
At this point of reading the header, I have no established mental context for score or ele or why we'd need len.
I have no mental context for "raw" or what it implies to "insert raw". (Why not just "insert"?)
There was a problem hiding this comment.
This is fair. There are two insert methods, one that takes sds, and this one takes (char*, length), thus being raw data without any header or such attached. I think I'll remove the convenience sds one and just have insert.
| return skiplistCreate(); | ||
| } | ||
|
|
||
| static inline void orderedIndexFree(OrderedIndex *idx) { |
There was a problem hiding this comment.
Using idx as for the OrderedIndex is likely to prove confusing to readers. idx typically indicates a numeric index into an ordered structure (like an array or list index).
Are we sure we want to call this data structure an "ordered index"? If we've already said it's ordered, what's the purpose of the word "index"? Is it an "ordered sds set"? I'm not exactly sure.
There was a problem hiding this comment.
I think OrderedIndex is a good name. We could also call it OrderedCollection perhaps, or maybe ZsetOrderedIndex because it is specific to zset. It isn't really a set itself because it doesn't enforce uniqueness - the hashtable is used for that.
idx is not the best though - maybe oi instead?
| skiplistFreeItem(item); | ||
| } | ||
|
|
||
| static inline OrderedIndexItem *orderedIndexCreateDetached(double score, const char *ele, size_t len) { |
There was a problem hiding this comment.
a nice that isn't inserted/attached to a set. this is used for some multi-set operations where we sum scores for all occurrences of an item in a given list of two sets (or more iirc). this avoids the delete+update cost each time we update score for an item that's part of a set.
so that's the intention and reason. could make it cleaner maybe, or use a workaround to avoid this in the interface. I'll give it some thought.
| @@ -0,0 +1,54 @@ | |||
| #ifndef SKIPLIST_ORDERED_INDEX_H | |||
There was a problem hiding this comment.
Big header comment. What is this? What's it for? High level concepts?
| #include "sds.h" | ||
|
|
||
| /* Lifecycle */ | ||
| OrderedIndex *skiplistCreate(void); |
There was a problem hiding this comment.
Compilation issue. OrderedIndex is undefined here unless header file ordering is constrained in the calling code (bad).
| void skiplistFree(OrderedIndex *idx); | ||
|
|
||
| /* Modification */ | ||
| OrderedIndexItem *skiplistInsert(OrderedIndex *idx, double score, const char *ele, size_t len); |
There was a problem hiding this comment.
OrderedIndexItem is undefined here. What does this represent from a caller's perspective. How do I use an OrderedIndexItem?
| size_t skiplistEstimateMemory(OrderedIndex *idx, size_t sample_size); | ||
|
|
||
| /* Defrag */ | ||
| OrderedIndex *skiplistDefragInternals(OrderedIndex *idx, void *(*defragfn)(void *)); |
| /* Debug */ | ||
| int skiplistGetHeight(OrderedIndex *idx); | ||
| int skiplistVerifyIntegrity(OrderedIndex *idx, char *errmsg, size_t errmsg_len); |
There was a problem hiding this comment.
No need to expose debug functions in the .h (API).
|
|
||
| #include "sds.h" | ||
|
|
||
| /* Opaque types for ordered index, positions, and iterators */ |
There was a problem hiding this comment.
General comment. This data structure is ordered by a double. It's very specific to data which has a double and a "lexical" string.
Could we use a generic "comparitor" function so that ANY orderable data can be stored? Would this add value?
There was a problem hiding this comment.
as we discussed in person, the data structure assumes that items are contiguous lexically ordered (binary safe) sds strings, and by giving up the arbitrary comparison flexibility it becomes a different class of data structure with higher performance. we can access those gains if we can come up with a transform operation between our items (score double, element string) and the collection's native type (SDS string), which is perhaps the main "trick" of the feature I'm implementing. :)
- Remove static inline wrappers; add ordered_index.c with regular functions that delegate to the backend (LTO handles inlining) - Add comprehensive header comment to ordered_index.h explaining what an OrderedIndex is, its relationship to the companion hashtable, and why it does not enforce uniqueness - Add per-function doc comments for all API functions - Add header comment to skiplist_ordered_index.h - Rename 'idx' parameter to 'oi' throughout to avoid confusion with numeric array indices - Drop orderedIndexInsert(sds) convenience wrapper; rename orderedIndexInsertRaw to orderedIndexInsert (callers pass ptr+len) - Remove debug functions (skiplistGetHeight, skiplistVerifyIntegrity) from ordered_index.h public API; they remain in skiplist_ordered_index.h for unit tests and DEBUG command - Fix include ordering: skiplist_ordered_index.h now includes ordered_index.h for type visibility Feedback from JimB123 on PR valkey-io#3552. Signed-off-by: Rain Valentine <[email protected]>
- Remove static inline wrappers; add ordered_index.c with regular functions that delegate to the backend (LTO handles inlining) - Add comprehensive header comment to ordered_index.h explaining what an OrderedIndex is, its relationship to the companion hashtable, and why it does not enforce uniqueness - Add per-function doc comments for all API functions - Add header comment to skiplist_ordered_index.h - Rename 'idx' parameter to 'oi' throughout to avoid confusion with numeric array indices - Drop orderedIndexInsert(sds) convenience wrapper; rename orderedIndexInsertRaw to orderedIndexInsert (callers pass ptr+len) - Remove debug functions (skiplistGetHeight, skiplistVerifyIntegrity) from ordered_index.h public API; they remain in skiplist_ordered_index.h for unit tests and DEBUG command - Fix include ordering: skiplist_ordered_index.h now includes ordered_index.h for type visibility Feedback from JimB123 on PR valkey-io#3552. Signed-off-by: Rain Valentine <[email protected]>
🚧 Currently a draft, but feel free to take a look. I think it's complete but I'm self-reviewing first. 🚧
This is the first PR that lays the foundation for #3166. I add an interface and unit tests to codify current zskiplist behavior. I also refactored so that across Valkey we respect the interface abstraction and don't leak implementation details.
The next PR planned will introduce the new data structure (fbtree) and use it to implement this interface. Unit tests will run on both zskiplist and fbtree implementations to ensure behavior is the same.