Repository navigation
KLL: weighted update, matching datasketches-java - #582
Conversation
…numerators Doubling denom until it exceeds numer overflows to zero once numer >= 2^63, so the loop never terminated. ub_on_num_levels(n) calls it with the merged stream weight, which made a merge reaching n >= 2^63 hang. Compare against numer / 2 instead, which is equivalent and cannot overflow. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
update(item, weight) is equivalent to calling update(item) weight times. A weight that fits into the free space of level zero is applied as plain updates; a larger one builds an exact sketch holding one copy of the item at each level whose bit is set in the weight and merges it, so the cost is logarithmic in the weight. This mirrors the weighted update in datasketches-java. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The const_iterator started at level 0 without skipping empty levels. When level 0 is empty, which a merge can leave, every item was reported with weight 1 instead of the weight of its level. Quantile queries were not affected: the sorted view reads the levels directly. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- Reject weights above 2^61 - 1 (kll_constants::MAX_WEIGHT), as datasketches-java does, instead of folding the high bits into level 60. Java cannot read an image with n >= 2^63. - Allocate the items storage before writing into it. Since items are allocated lazily, the small-weight path wrote through a null pointer on a new sketch. Allocating first also leaves the sketch unchanged when the allocation fails. - Construct each copy before counting it, so that a failed copy leaves a valid sketch. - A weight of 1 is a plain update, as in Java. - Check the item before the weight, as in Java. - Tests: the maximum weight, weights above it, weight 1, the first weighted update of a new sketch on both paths, and allocation failures. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Coverage Report for CI Build 44436712044Warning Build has drifted: This PR's base is out of sync with its target branch, so coverage data may include unrelated changes. Coverage increased (+0.1%) to 96.974%Details
Uncovered ChangesNo uncovered changes found. Coverage Regressions92 previously-covered lines in 13 files lost coverage.
Coverage Stats
💛 - Coveralls |
Both deserializers, from bytes and from a stream, must allocate only what a newly built empty sketch does. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
| } | ||
| items_size_ = levels_[num_levels_]; | ||
| items_ = allocator_.allocate(items_size_); | ||
| for (uint32_t i = 0; i < items_size_; ++i) new (&items_[i]) T(item); |
There was a problem hiding this comment.
Because of this logic, If bad_alloc happens, we should deallocate in L248. If not, this is memory leak.
There was a problem hiding this comment.
You're right, thanks. If a copy of the item throws, the destructor doesn't run, so the storage and the copies already made leaked. Fixed in 0ab4ab6: the constructor now destroys the copies it made and frees the storage before rethrowing.
The constructor that builds the weighted sketch now destroys the copies it made and frees its storage if a copy throws. The weighted update resets the sorted view before changing anything. merge() now forwards an rvalue source to merge_higher_levels(), so its higher levels are moved rather than copied, and the merge the weighted update makes copies no items. A new test makes each item copy of a weighted update throw in turn, and checks for leaks and a stale sorted view. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
| new (&items_[index]) T(conditional_forward<FwdSk>(other.items_[i])); | ||
| } | ||
| if (other.num_levels_ >= 2) { merge_higher_levels(other, final_n); } | ||
| if (other.num_levels_ >= 2) { merge_higher_levels(std::forward<FwdSk>(other), final_n); } |
There was a problem hiding this comment.
Forwarding other into merge_higher_levels only avoids copies on the (self_pop == 0) && (other_pop > 0) branch in populate_work_arrays (kll_sketch_impl.hpp:963). Once this is in estimation mode and has items at the same higher level as other ((self_pop > 0) && (other_pop > 0), line 968), populate_work_arrays calls the 8-argument kll_helper::merge_sorted_arrays(const T* buf_a, ..., const T* buf_b, ...) (kll_helper_impl.hpp:164), which has two issues when an item copy (or comparator) throws:
other.items_is passed asconst T*without forwarding, so elements fromotherare still copy-constructed (new (&buf_c[c]) T(buf_b[b++])). In addition,buf_a(this->items_) is alsoconst T*, sonew (&buf_c[c]) T(std::move(buf_a[a]))(kll_helper_impl.hpp:177, 180) producesconst T&&and invokesT's copy constructor rather than its move constructor right before callingbuf_a[a++].~T().- If any copy or comparison throws inside
populate_work_arrays,workbuf's deleter (kll_sketch_impl.hpp:891) deallocates raw memory without destroying the objects already constructed inworkbuf(leaking them), whilethis->items_has already destroyed elements inlevels_[0]..levels_[lvl]and[levels_[lvl], a)without updatinglevels_. Subsequent destruction in~kll_sketch()(kll_sketch_impl.hpp:146) then calls~T()a second time on those destroyed slots (double-free / UB).
The new fragile_item test (kll_sketch_allocation_test.cpp:319) does not reach this branch because k = 200 with 5 updates stays at num_levels_ == 1 (self_pop == 0 for all lvl >= 1). Changing that fixture to k = 8 with 9 initial updates and weight = 18 reproduces a leak at fail_at = 5 and an ASan double-free in ~kll_sketch() at fail_at = 6.
Taking non-const T* buf_a in merge_sorted_arrays (and forwarding from buf_b when other is an rvalue), along with tracking constructed elements in workbuf and destroyed slots in this->items_ on unwind, would close this gap.
There was a problem hiding this comment.
Thanks, confirmed. Your fixture SEGVs under ASan. I was wrong in my earlier reply: the weighted update's merge copies no items only while this sketch has nothing at the levels it merges into. In estimation mode it still copied, after this sketch's items had already been moved out. The same was true of merge(const&) before this PR.
Fixed in #585. The merge now copies other's items into the work buffer first. If a copy throws, it destroys them, and this sketch is unchanged. After that, this sketch's items are merged in front of them using moves only, so nothing is copied from a const T*. Your fixture is one of the tests. The helpers now use the sketch's comparator instead of C().
| if (weight == 0) throw std::invalid_argument("weight must be at least 1"); | ||
| if (weight > kll_constants::MAX_WEIGHT) { | ||
| throw std::invalid_argument("weight must be less than 2^61: " + std::to_string(weight)); | ||
| } |
There was a problem hiding this comment.
MAX_WEIGHT (2^61 - 1) bounds a single call, but merge (const uint64_t final_n = n_ + other.n_; at line 319) and the small-weight path do not check for uint64_t overflow on n_:
- 5 calls of
update(x, MAX_WEIGHT)already pushn_ >= 2^63(producing images beyond Java's signedlongrange). - 9 calls of
update(x, MAX_WEIGHT)wrapn_modulo2^64to2^61 - 9. Becausekll_helper::sum_the_sample_weightswraps modulo2^64in the same way,assert_correct_total_weight()still passes onk = 200, silently leaving a corruptedget_n()(and onk = 8, accumulating 68+ items at level 60 eventually throws"depth > 60"mid-merge after mutatingmin_item_and level zero).
Checking weight > std::numeric_limits<uint64_t>::max() - n_ (and other.n_ > std::numeric_limits<uint64_t>::max() - n_ in merge, analogous to Java's Math.addExact) before mutating the sketch would prevent silent wraparound.
There was a problem hiding this comment.
Agreed. #585 caps n at 2^63 - 1, Java's Long.MAX_VALUE, since Java's merge uses Math.addExact on a signed long. update(), the weighted update and merge() now throw std::overflow_error before changing anything. With n below 2^63, a sketch can't reach the 62 levels that make level_capacity throw "depth > 60", so that failure partway through a merge goes away too.
| template<typename FwdT> | ||
| void kll_sketch<T, C, A>::update(FwdT&& item, uint64_t weight) { | ||
| if (!check_update_item(item)) { return; } | ||
| if (weight == 0) throw std::invalid_argument("weight must be at least 1"); |
There was a problem hiding this comment.
Throwing on weight == 0 matches datasketches-java's KllSketch, though two points may be worth discussing for cross-sketch / cross-language alignment:
- In
datasketches-rust(feat(kll): add weighted update datasketches-rust#293), we madeKllSketch::update_with_weight(item, 0)a no-op to matchfrequent_items_sketch::update(item, 0)(which is also a no-op in both Java and C++) andCountMinSketch, since repeatingupdate(item)zero times is naturally an identity operation whenweightis unsigned (e.g., pre-aggregated bucket counts that can be 0). Happy to discuss whether we want to keep KLL aligned with Java's exception here or align zero-weight handling across sketches; we documented the difference explicitly on the Rust side for now. - Because
check_update_item(item)runs on line 199 before theweightvalidation,sketch.update(NaN, 0)(andsketch.update(NaN, MAX_WEIGHT + 1)) returns silently whilesketch.update(1.0f, 0)throwsstd::invalid_argument.
There was a problem hiding this comment.
Both follow datasketches-java. Its update(item, weight) returns on NaN first, then throws on a weight below 1, and C++ does the same in the same order. Whether a weight of 0 should be a no-op in every language is a good question, but it's a cross-language change, so I'd rather take it to dev@ than decide it here. For now C++ matches Java.
| } | ||
| allocate_items(); // first, so that a failure leaves the sketch unchanged | ||
| reset_sorted_view(); // before any change, so that a failed copy can't leave a stale view | ||
| if (weight < levels_[0]) { // fits into level zero without compaction |
There was a problem hiding this comment.
internal_update() only compacts when levels_[0] == 0, so weight == levels_[0] also fits into level zero without compaction. With <, hitting the exact capacity boundary (for example, k = 200 with 150 updates followed by update(x, 50)) takes the merge path and transitions into estimation mode (num_levels_ = 6, 153 retained items) at n == k, whereas 50 plain update(x) calls keep all 200 items in exact mode. Using <= keeps exact mode up to n == k unless < is intentionally kept to mirror Java's guard.
There was a problem hiding this comment.
This is intentional. Java uses the same guard (weight < levelsArr[0]). With <=, C++ would build a different sketch from Java for the same input at that boundary, and we keep the two byte-identical.
There was a problem hiding this comment.
Makes sense! One related detail just as an FYI (no action needed here): for k > 200 (DEFAULT_K), #569 initializes a new C++ sketch with items_size_ = 200 and levels_[0] = 200 (until reserve() or lazy growth), whereas Java heap KLL initializes levelsArr = {k, k}. As a result, on a fresh k = 400 sketch, update(x, 250) has 250 < levels_[0] false in C++ (taking the merge path into estimation mode) while 250 < levelsArr[0] is true in Java (staying in level zero).
| /** | ||
| * Updates this sketch with the given data item repeated the given number of times. | ||
| * The result is equivalent to calling update(item) weight times, at a cost that grows | ||
| * with the logarithm of the weight rather than with the weight itself. |
There was a problem hiding this comment.
Nit: when weight < levels_[0] (up to 65,534 for MAX_K), update(item, weight) runs an O(weight) copy loop into level zero rather than O(log(weight)). Describing the two paths (direct level-zero insertion when weight fits in the remaining free space, and merging a binary-level sketch otherwise) would avoid overstating the complexity bound for large k.
There was a problem hiding this comment.
Good point, fixed in #585. The doc now describes both paths.
| } | ||
|
|
||
| SECTION("weighted update: estimation mode") { | ||
| kll_float_sketch sketch(200, std::less<float>(), 0); |
There was a problem hiding this comment.
Nit: seeding random_utils::random_bit explicitly at the start of this section (as in weight 1 is a plain update on line 933) would keep it deterministic when executed standalone via Catch2 -c "weighted update: estimation mode", instead of testing 10,000 queries against the 99% confidence bound RANK_EPS_FOR_K_200 under a clock-based seed.
There was a problem hiding this comment.
Done in #585. The section now seeds random_bit.
Adds
kll_sketch::update(item, weight), equivalent to callingupdate(item)weighttimes. It follows the weighted update of the KLL sketches in datasketches-java.This builds on #542 by Shani Elharrar, whose two commits are kept here unchanged, with fixes on top.
Behavior
weightis smaller than the free space in level zero, the item is insertedweighttimes as plain updates. No compaction can happen on this path.weight, and merges it in. This costs one merge with a sketch of at most 61 items, however large the weight.kll_constants::MAX_WEIGHT), throwsstd::invalid_argument. These are the same rules as in Java, which cannot read an image with n >= 2^63.update(item).Fixes on top of #542
update(item), so a failed allocation also leaves the sketch unchanged.Also included
kll_helper::floor_of_log2_of_fractionlooped forever oncenumer >= 2^63, because doublingdenomoverflowed to zero. Any merge that reached n >= 2^63 hung. This is the first commit from Add weighted update to kll_sketch #542. Java computes the same bound without a loop, so it is not affected.Tests
kll_sketch_test.cpp: zero weight, NaN, same as repeated updates in exact mode, one item with a large weight, the maximum weight with a serialization round trip, weights above the maximum, weight 1, the first weighted update of a new sketch on both paths, large weights into a full sketch, estimation mode within the k=200 error bound, and strings.kll_sketch_allocation_test.cpp: a failed first weighted update leaves the sketch unchanged on both paths, and failing each allocation of a stream of weighted updates in turn leaves a valid, usable sketch.All tests pass under ASan and UBSan.
🤖 Generated with Claude Code