Repository navigation
fix: keep the frequent items sketch valid when an allocation fails - #603
Draft
stojkomilos wants to merge 5 commits into
Draft
stojkomilos wants to merge 5 commits into
stojkomilos wants to merge 5 commits into
Conversation
reverse_purge_hash_map leaked its arrays when the constructor or the copy constructor failed to allocate. If constructing a new key threw, its slot stayed active with no key in it. If resize() failed to allocate, keys_ pointed to the new uninitialized array while the states described the old table. The constructors now free what they allocated, adjust_or_insert() takes the new key out again if constructing it or growing the map fails, and resize() builds the larger map separately before it changes this one. update() adds the weight after the map accepted the item, so a failed update leaves the sketch unchanged. Adds a test that fails each allocation of the constructors, updates, assignment and merges in turn. Co-authored-by: Isaac <[email protected]>
The move assignment also swapped equal_ and allocator_, so update() needed them to be assignable. Swap only the buffers. Document that a failed update(T&&) consumes the item. Add tests for a key whose copy throws, for a non-assignable allocator and functor, and for updates that need no allocation. Co-authored-by: Isaac <[email protected]>
Declare the map first, so the implicit copy assignment fails before the weights change. Clarify the copy constructor comment, and remove a swap that does nothing. Test that a failed copy assignment leaves the target unchanged, and that a key copy failure in update() does not destroy the key. Co-authored-by: Isaac <[email protected]>
Restore the member order. Assign the map before the weights, so a failed copy leaves the target unchanged. Default the copy and move operations, which a user-declared copy assignment would otherwise suppress. Co-authored-by: Isaac <[email protected]>
Add Doxygen comments to the four new operations. In the tests, count the failures of the assignment and rvalue merge loops, drop the check on a moved-from string, and test a key with no move constructor during a resize. Co-authored-by: Isaac <[email protected]>
stojkomilos
commented
Dec 1, 2026
| if (weight == 0) { return; } | ||
| total_weight += weight; | ||
| offset += map.adjust_or_insert(std::move(item), weight); | ||
| total_weight += weight; |
Contributor
Author
There was a problem hiding this comment.
The weight is now added to total_weight only after the map has accepted the item. A failed update then leaves the whole sketch unchanged, not only the map.
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.
NOT READY FOR REVIEW YET
reverse_purge_hash_map, the map insidefrequent_items_sketch, can leak memory or leave the sketch broken when an allocation fails. Only a failing allocator triggers this. The serialized format does not change.Changes
try, and frees the ones already allocated if one fails. Before, those arrays leaked, because the destructor does not run when a constructor throws.adjust_or_insert(): if constructing the new key throws, it empties the key's slot again. Before, the slot stayed active with no key in it, so a later lookup,serialize()or the destructor read an unconstructed key.resize_or_purge_if_needed(): now gets the slot of the new key, and removes that key again ifresize()throws. Before, the map kept one item over its capacity, so every later new item threwnum_active ... > capacity ..., and a retry of the failed item counted it twice.resize(): builds the larger map as a separate object, then swaps in its arrays. It does not move-assign the map, soupdate()andmerge()work with an allocator or equality functor that is not assignable. Before, if the second or third allocation threw,keys_pointed to the new uninitialized array whilestates_still described the old table.frequent_items_sketchcopy assignment: written by hand to assign the map first, then the weights, so a failed map copy leaves the target unchanged. Before, the weights were assigned first, so a failed assignment left the target with the source's weights and its own items. The copy and move operations stay defaulted, and the member layout is unchanged.frequent_items_sketch::update(): adds the weight tototal_weightafter the map accepts the item, so a failed update leaves the sketch unchanged.update(T&&)may move from the item even when it throws.frequent_items_sketch_allocation_failure_test.cpp: fails each allocation in turn. Its string items allocate from the sketch's allocator, so item copies fail too.update()andmerge()work with a non-assignable allocator and equality functor.reset()and assignment still need assignable ones, as on master.Not changed
purge(): it does not allocate for keys whose move constructor does not allocate. A key with no move constructor is copied inhash_delete(), and a failed copy there corrupts the map, as on master.merge(): a failed merge leaves a usable sketch with part of the other sketch merged in. Its error bound does not yet include the other sketch's offset, so the bound can be too low.resize(),hash_delete()). Only allocation failures are handled.Checks
serialize()), KLL: keep the sketch valid when a merge fails to allocate #574 (KLL), Make HLL sketch and union exception-safe on allocation failure #565 (HLL). The test copies the failing allocator and allocation control from KLL: keep the sketch valid when a merge fails to allocate #574.Comparison and timing
GCC 10.5,
-O2, best of 5, two runs each:lg_max_map_size14lg_max_map_size14Both builds print
hash 1e7f327e985c81af.This pull request and its description were written by Isaac.