Repository navigation
Conversation
Compactor: - The copy and converting constructors release their storage and the copies made when a copy throws, or when the converting constructor rejects the order of the items. - merge() works out the sections and the capacity first and allocates once, then copies the other compactor's items and destroys the copies if one throws. Only then does it change the state and the sections. It used to change them first, and leaked the copies made before a copy that threw. It compares with the sketch's comparator, not C(). - compact() allocates the space in the next level, and the growth that more sections need, before it flips or draws the coin and changes anything. A failed compaction used to leave the coin flipped (or a random bit drawn), and could change the sections and then fail to grow. Sketch: - update() compares the item with min and max and copies it first, and sets min and max only after the item is in level zero. - merge() copies the new min and max before it sets either. The same gaps are addressed in datasketches-java by apache/datasketches-java#808, except the item-loss window in compress(), which C++ does not have: compact() reserved the next level's space before moving any item. Results are byte-identical to before. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This comment was marked as outdated.
This comment was marked as outdated.
A compactor that has never held an item has no storage, so serialize() passed a null pointer with a count of 0, and the default serde passed it on to memcpy(). UBSan on Linux reports that as undefined behavior. Serialize the items only when there are some; the bytes are unchanged. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
tisonkun
reviewed
Nov 20, 2026
- merge(): merging a sketch into itself merges a copy, as merging read other while changing this sketch. - merge(): merge the compactors from the highest level down, and set min and max with the first items of other that the sketch takes. A failed merge then leaves min and max unchanged unless items of other were added, and in that case a level above level zero holds items, so the sketch stays in estimation mode, where min and max are serialized. - update() and merge(): cap n at req_constants::MAX_N = 2^63 - 1, as KLL does. - Compactor append(): when level zero must grow, copy the item first, since it may be one of the items that growing moves and frees. - Compactor: stop doubling the sections at 48, which keeps 1 << (num_sections - 1) defined and stays within what deserialize accepts. - Compactor converting constructor: release the items if the comparator throws while the order is checked. - Compactor deserialize: an empty compactor gets no storage. - quantiles_sorted_view: check split points with the view's comparator instead of a default-constructed one, so get_CDF() and get_PMF() compile with comparators that can't be default-constructed and use the right order with ones that have state. Serialized results are byte-identical to master. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
merge() read other while it changed this sketch, so merging a sketch into itself used memory it had freed. It now merges a copy, as REQ does. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Deserialize now checks that n is the total weight of the retained items (#595), so the test builds an image whose weight is 2^63 - 2 instead of overwriting n. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
proost
requested changes
Dec 2, 2026
merge() now copies the items of other into every level, and the new min and max, before it changes anything. If an allocation or a copy fails, it destroys the copies and removes the levels it added, so this sketch is unchanged. Before, a failure could leave some levels of other merged. (Suggested by proost in review.) The compactor merge is split into merge_prepare(), which allocates and copies, and merge_commit(), which adds the copies to the compactor before it compares any, so a comparator that throws while the items are sorted or merged leaks nothing. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This branch has not been deployed
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.
This fixes the REQ gaps found while reviewing #582, and the C++ side of the gaps that apache/datasketches-java#808 addresses in Java.
Compactor:
merge()first works out the sections and capacity it will need, then allocates once. Next it copies the other compactor's items, destroying the copies if one throws. Only after that does it change the state and the sections. Before, it changed the state and sections first, and it leaked the copies made before a copy that threw. It also now compares with the sketch's comparator instead ofC().compact()now allocates the space in the next level, and any growth that more sections need, before it flips or draws the coin or changes anything. Before, a failed compaction left the coin flipped or a random bit drawn, and it could change the sections and then fail to grow. A failed compaction now leaves both compactors as they were and draws no random bit.Sketch:
update()compares the item with min and max and copies it first. It sets min and max only after the item is in level zero, so a failed append no longer leaves a min or max for an item that isn't retained.merge()copies the new min and max before it sets either.The docs of
update()andmerge()now say what a failure leaves behind. A comparator that throws while items are sorted or merged leaves the sketch undefined, as decided for Java in apache/datasketches-java#805.One difference from Java: C++ never had the window in which #808's
compress()could lose promoted items, becausecompact()already reserved space in the next level before moving any item.Tests:
const&merges: the target must stay consistent and usable;The five allocation tests fail on master and pass here. The comparator test needs this change to compile. Serialized results are byte-identical to master; I checked 600 seeded combinations of HRA and LRA, k = 4, 12 and 50,
const&and rvalue merges, and double and string sketches. All tests pass under ASan and UBSan.Added after review (a68f18e, a73df7c, 36789db, f2019a3):
merge()of a sketch into itself merges a copy. Classic quantiles does the same; on master, merging a classic quantiles sketch into itself used freed memory.merge()merges all levels or none: it copies the items of other into every level, and min and max, before it changes anything, so a failed copy or allocation leaves the sketch unchanged.update()andmerge()cap n atreq_constants::MAX_N= 2^63 - 1, as KLL does since KLL: keep the sketch valid when an item copy throws; cap n at 2^63 - 1 #585.quantiles_sorted_view(common) checks split points with the view's comparator, soget_CDF()andget_PMF()work with comparators that can't be default-constructed, for KLL and classic quantiles too.Serialized results are still byte-identical to master.
🤖 Generated with Claude Code