From 4e42b3f795a202b0080ed6d75599c37214b40956 Mon Sep 17 00:00:00 2001 From: Lee Rhodes Date: Thu, 12 Nov 2026 11:40:19 -0800 Subject: [PATCH 1/6] REQ: allocate compactor storage on first use, and add reserve() A new compactor no longer allocates its item storage in its constructor. It allocates the same 2 * nominal capacity on its first append, merge or compaction into it, so a REQ sketch that stays empty holds no item storage. req_sketch::reserve() allocates the storage of the first compactor up front. update() reserves it before it touches min and max, so a failed first update leaves the sketch unchanged. Allocation-failure tests found three existing problems, fixed here: - The iterator did not skip levels without items. Iterating an empty sketch never reached end() and read past the item storage. - An allocation that failed after a compaction or partway through a merge left n, the number of retained items and the nominal size out of step with the compactors. They are now recomputed from the compactors, and the sorted view is reset before anything changes. - update() compressed only when the number of retained items was exactly the nominal size. After a failed compression it could pass that size and never compress again. It now compresses at or above it, as merge() does. The new tests use an allocator that fails on purpose, at each allocation of reserve(), a stream of updates and a merge, and check that the sketch stays consistent and usable and keeps compressing. Co-Authored-By: Claude Opus 5.5 --- req/include/req_compactor.hpp | 7 +- req/include/req_compactor_impl.hpp | 15 +- req/include/req_sketch.hpp | 26 ++ req/include/req_sketch_impl.hpp | 58 ++++- req/test/CMakeLists.txt | 1 + req/test/req_sketch_allocation_test.cpp | 318 ++++++++++++++++++++++++ 6 files changed, 410 insertions(+), 15 deletions(-) mode change 100755 => 100644 req/include/req_sketch_impl.hpp create mode 100644 req/test/req_sketch_allocation_test.cpp diff --git a/req/include/req_compactor.hpp b/req/include/req_compactor.hpp index 87ddf1b34..7c41ab52e 100755 --- a/req/include/req_compactor.hpp +++ b/req/include/req_compactor.hpp @@ -53,6 +53,9 @@ class req_compactor { uint64_t compute_weight(const T& item, bool inclusive) const; + // allocates the initial storage for items, unless the compactor has storage already + void reserve(); + template void append(FwdT&& item); @@ -113,8 +116,8 @@ class req_compactor { uint8_t num_sections_; uint64_t state_; // state of the deterministic compaction schedule uint32_t num_items_; - uint32_t capacity_; - T* items_; + uint32_t capacity_; // 0 while items_ is nullptr + T* items_; // nullptr until first use, see reserve() bool ensure_enough_sections(); std::pair compute_compaction_range(uint32_t secs_to_compact) const; diff --git a/req/include/req_compactor_impl.hpp b/req/include/req_compactor_impl.hpp index f30a7b6c1..34d6fd4c7 100755 --- a/req/include/req_compactor_impl.hpp +++ b/req/include/req_compactor_impl.hpp @@ -44,8 +44,8 @@ section_size_(section_size), num_sections_(req_constants::INIT_NUM_SECTIONS), state_(0), num_items_(0), -capacity_(2 * get_nom_capacity()), -items_(allocator_.allocate(capacity_)) +capacity_(0), +items_(nullptr) // allocated on first use, see reserve() {} template @@ -97,6 +97,8 @@ capacity_(other.capacity_), items_(other.items_) { other.items_ = nullptr; + other.capacity_ = 0; + other.num_items_ = 0; } template @@ -193,9 +195,15 @@ uint64_t req_compactor::compute_weight(const T& item, bool inclusive) c return std::distance(begin(), it) << lg_weight_; } +template +void req_compactor::reserve() { + if (items_ == nullptr) grow(2 * get_nom_capacity()); +} + template template void req_compactor::append(FwdT&& item) { + reserve(); if (num_items_ == capacity_) grow(capacity_ + get_nom_capacity()); const uint32_t i = hra_ ? capacity_ - num_items_ - 1 : num_items_; new (items_ + i) T(std::forward(item)); @@ -211,13 +219,14 @@ void req_compactor::grow(uint32_t new_capacity) { new (new_items + new_i) T(std::move(*it)); (*it).~T(); } - allocator_.deallocate(items_, capacity_); + if (items_ != nullptr) allocator_.deallocate(items_, capacity_); items_ = new_items; capacity_ = new_capacity; } template void req_compactor::ensure_space(uint32_t num) { + reserve(); if (num_items_ + num > capacity_) grow(num_items_ + num + get_nom_capacity()); } diff --git a/req/include/req_sketch.hpp b/req/include/req_sketch.hpp index ed6d11f7a..79654000a 100755 --- a/req/include/req_sketch.hpp +++ b/req/include/req_sketch.hpp @@ -75,6 +75,13 @@ namespace datasketches { * two levels of detail output. This is not only useful for debugging, but is a powerful tool to * help users understand how the sketch works. * + * + *

Memory allocation: a new sketch does not allocate storage for items until it first needs it, + * on the first update or the first merge of a non-empty sketch. Sketches that stay empty use + * little memory. As a result, the first update or merge can throw std::bad_alloc while allocating + * this storage, rather than the constructor. If the first update fails this way, the sketch is + * unchanged and can still be used. Call reserve() right after construction to allocate the storage + * up front.

*/ template< typename T, @@ -106,6 +113,7 @@ class req_sketch { * @param comparator strict weak ordering function (see C++ named requirements: Compare) * @param allocator used by this sketch to allocate memory * @throws std::invalid_argument if k is greater than 1024 + * Storage for items is allocated on first use, see reserve(). */ explicit req_sketch(uint16_t k, bool hra = true, const Comparator& comparator = Comparator(), const Allocator& allocator = Allocator()); @@ -188,6 +196,7 @@ class req_sketch { * Updates this sketch with the given data item. * If cross-language portability is required, callers should ensure that * the input string uses a compatible encoding (valid UTF-8). + * The first update allocates storage for items, unless reserve() or a merge already did. * @param item from a stream of items */ template @@ -197,11 +206,22 @@ class req_sketch { * Merges another sketch into this one. * If sketches contain strings, callers are responsible for ensuring that * both sketches were built using compatible string encodings. + * The first merge of a non-empty sketch allocates storage for items, + * unless reserve() or an update already did. * @param other sketch to merge into this one */ template void merge(FwdSk&& other); + /** + * Allocates storage for items now, rather than on the first update or merge. + * Call this right after construction if allocation failures should happen here, + * or if the first update should not pay for the allocation. + * Does nothing if the storage is already allocated. + * @throw std::bad_alloc if the allocation fails, in which case the sketch is unchanged + */ + void reserve(); + /** * Returns the min item of the stream. * If the sketch is empty this throws std::runtime_error. @@ -454,7 +474,11 @@ class req_sketch { void grow(); void update_max_nom_size(); void update_num_retained(); + // recomputes n, the number of retained items and the nominal size from the compactors, + // after an allocation failed partway through an update or a merge + void restore_counts(); void compress(); + void compress_levels(); static double get_rank_lb(uint16_t k, uint8_t num_levels, double rank, uint8_t num_std_dev, uint64_t n, bool hra); static double get_rank_ub(uint16_t k, uint8_t num_levels, double rank, uint8_t num_std_dev, uint64_t n, bool hra); @@ -507,6 +531,8 @@ class req_sketch::const_iterator { const T* compactor_it_; friend class req_sketch; const_iterator(LevelsIterator begin, LevelsIterator end); + // moves to the first item at or above the current level, skipping levels without items + void skip_empty_levels(); }; } /* namespace datasketches */ diff --git a/req/include/req_sketch_impl.hpp b/req/include/req_sketch_impl.hpp old mode 100755 new mode 100644 index 3c1cb4087..b6ccf2fe6 --- a/req/include/req_sketch_impl.hpp +++ b/req/include/req_sketch_impl.hpp @@ -173,6 +173,7 @@ template template void req_sketch::update(FwdT&& item) { if (!check_update_item(item)) { return; } + compactors_[0].reserve(); // first, so that a failure leaves the sketch unchanged if (is_empty()) { min_item_.emplace(item); max_item_.emplace(item); @@ -183,8 +184,8 @@ void req_sketch::update(FwdT&& item) { compactors_[0].append(std::forward(item)); ++num_retained_; ++n_; - if (num_retained_ == max_nom_size_) compress(); reset_sorted_view(); + if (num_retained_ >= max_nom_size_) compress(); } template @@ -192,6 +193,7 @@ template void req_sketch::merge(FwdSk&& other) { if (is_HRA() != other.is_HRA()) throw std::invalid_argument("merging HRA and LRA is not valid"); if (other.is_empty()) return; + reset_sorted_view(); if (is_empty()) { min_item_.emplace(conditional_forward(*other.min_item_)); max_item_.emplace(conditional_forward(*other.max_item_)); @@ -199,17 +201,26 @@ void req_sketch::merge(FwdSk&& other) { if (comparator_(*other.min_item_, *min_item_)) *min_item_ = conditional_forward(*other.min_item_); if (comparator_(*max_item_, *other.max_item_)) *max_item_ = conditional_forward(*other.max_item_); } - // grow until this has at least as many compactors as other - while (get_num_levels() < other.get_num_levels()) grow(); - // merge the items in all height compactors - for (size_t i = 0; i < other.get_num_levels(); ++i) { - compactors_[i].merge(conditional_forward(other.compactors_[i])); + try { + // grow until this has at least as many compactors as other + while (get_num_levels() < other.get_num_levels()) grow(); + // merge the items in all height compactors + for (size_t i = 0; i < other.get_num_levels(); ++i) { + compactors_[i].merge(conditional_forward(other.compactors_[i])); + } + } catch (...) { + restore_counts(); // some levels may be merged already + throw; } n_ += other.n_; update_max_nom_size(); update_num_retained(); if (num_retained_ >= max_nom_size_) compress(); - reset_sorted_view(); +} + +template +void req_sketch::reserve() { + compactors_[0].reserve(); } template @@ -631,8 +642,27 @@ void req_sketch::update_num_retained() { for (const auto& compactor: compactors_) num_retained_ += compactor.get_num_items(); } +template +void req_sketch::restore_counts() { + // the total weight of the retained items is always n + n_ = 0; + for (const auto& compactor: compactors_) n_ += static_cast(compactor.get_num_items()) << compactor.get_lg_weight(); + update_num_retained(); + update_max_nom_size(); +} + template void req_sketch::compress() { + try { + compress_levels(); + } catch (...) { + restore_counts(); // a compactor can fail to grow after a compaction moved its items + throw; + } +} + +template +void req_sketch::compress_levels() { for (size_t h = 0; h < compactors_.size(); ++h) { if (compactors_[h].get_num_items() >= compactors_[h].get_nom_capacity()) { if (h == 0) compactors_[0].sort(); @@ -791,15 +821,23 @@ template req_sketch::const_iterator::const_iterator(LevelsIterator begin, LevelsIterator end): levels_it_(begin), levels_end_(end), - compactor_it_(begin == end ? nullptr : (*levels_it_).begin()) -{} + compactor_it_(nullptr) +{ + skip_empty_levels(); +} + +template +void req_sketch::const_iterator::skip_empty_levels() { + while (levels_it_ != levels_end_ && (*levels_it_).get_num_items() == 0) ++levels_it_; + if (levels_it_ != levels_end_) compactor_it_ = (*levels_it_).begin(); +} template auto req_sketch::const_iterator::operator++() -> const_iterator& { ++compactor_it_; if (compactor_it_ == (*levels_it_).end()) { ++levels_it_; - if (levels_it_ != levels_end_) compactor_it_ = (*levels_it_).begin(); + skip_empty_levels(); } return *this; } diff --git a/req/test/CMakeLists.txt b/req/test/CMakeLists.txt index 8ebaadb0d..7bba59d10 100755 --- a/req/test/CMakeLists.txt +++ b/req/test/CMakeLists.txt @@ -39,6 +39,7 @@ target_sources(req_test PRIVATE req_sketch_test.cpp req_sketch_custom_type_test.cpp + req_sketch_allocation_test.cpp ) if (SERDE_COMPAT) diff --git a/req/test/req_sketch_allocation_test.cpp b/req/test/req_sketch_allocation_test.cpp new file mode 100644 index 000000000..0f1c1a677 --- /dev/null +++ b/req/test/req_sketch_allocation_test.cpp @@ -0,0 +1,318 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +#include +#include +#include +#include + +#include +#include + +namespace datasketches { + +namespace { + +// Counts allocations, and throws std::bad_alloc on the allocation numbered fail_at (counting from 0). +struct allocation_control { + long count = 0; + long fail_at = -1; + void arm(long at) { count = 0; fail_at = at; } + void disarm() { fail_at = -1; } +}; + +template +struct failing_allocator: test_allocator { + allocation_control* control; + template struct rebind { using other = failing_allocator; }; + explicit failing_allocator(allocation_control& c): test_allocator(0), control(&c) {} + template + failing_allocator(const failing_allocator& other): test_allocator(0), control(other.control) {} + T* allocate(std::size_t n, const T* hint = nullptr) { + if (control->fail_at >= 0 && control->count++ == control->fail_at) throw std::bad_alloc(); + return test_allocator::allocate(n, hint); + } +}; + +template +bool operator==(const failing_allocator& a, const failing_allocator& b) { return a.control == b.control; } +template +bool operator!=(const failing_allocator& a, const failing_allocator& b) { return !(a == b); } + +using req_double_test_sketch = req_sketch, test_allocator>; +using sketch_type = req_sketch, failing_allocator>; + +const uint16_t K = 12; + +std::string item(int i) { + // long enough to be allocated outside the string object + return std::string(32, 'x') + std::to_string(i); +} + +sketch_type make_sketch(int first, int count, allocation_control& control, bool hra = true) { + sketch_type sketch(K, hra, std::less(), failing_allocator(control)); + for (int i = first; i < first + count; ++i) sketch.update(item(i)); + return sketch; +} + +std::vector bytes(const sketch_type& sketch) { + const auto b = sketch.serialize(); + return std::vector(b.begin(), b.end()); +} + +// the sketch must be consistent: the weights of the retained items add up to n +void check_consistent(const sketch_type& sketch) { + uint64_t total_weight = 0; + uint32_t num_items = 0; + for (const auto pair: sketch) { + total_weight += pair.second; + ++num_items; + } + REQUIRE(total_weight == sketch.get_n()); + REQUIRE(num_items == sketch.get_num_retained()); +} + +// the sketch must still work: query it, update it, merge into it, and round-trip it +void check_usable(sketch_type& sketch, allocation_control& control) { + control.disarm(); + check_consistent(sketch); + const uint64_t n = sketch.get_n(); + sketch.update(item(-1)); + REQUIRE(sketch.get_n() == n + 1); + REQUIRE(sketch.get_min_item() <= item(-1)); + REQUIRE(sketch.get_quantile(0.5) >= sketch.get_min_item()); + sketch.merge(make_sketch(0, 500, control, sketch.is_HRA())); + REQUIRE(sketch.get_n() == n + 501); + check_consistent(sketch); + const auto b = sketch.serialize(); + const auto copy = sketch_type::deserialize(b.data(), b.size(), serde(), + std::less(), failing_allocator(control)); + REQUIRE(copy.get_n() == sketch.get_n()); + REQUIRE(copy.get_num_retained() == sketch.get_num_retained()); +} + +} // namespace + +TEST_CASE("req sketch allocation: reserve allocates item storage up front", "[req_sketch]") { + test_allocator_total_bytes = 0; + test_allocator_net_allocations = 0; + { + for (bool hra: {true, false}) { + req_double_test_sketch sketch(K, hra, std::less(), test_allocator(0)); + const long long empty_bytes = test_allocator_total_bytes; + // 2 * nominal capacity of the first compactor: 2 * 2 * 3 sections * k + const long long storage_bytes = static_cast(2 * 2 * 3 * K * sizeof(double)); + + sketch.reserve(); + const long long reserved_bytes = test_allocator_total_bytes; + REQUIRE(reserved_bytes == empty_bytes + storage_bytes); + + sketch.reserve(); // already allocated + REQUIRE(test_allocator_total_bytes == reserved_bytes); + + for (int i = 0; i < 2 * 3 * K - 1; ++i) sketch.update(i); // fills the first compactor up to its nominal capacity + REQUIRE(test_allocator_total_bytes == reserved_bytes); + REQUIRE(sketch.get_n() == 2 * 3 * K - 1); + REQUIRE(sketch.get_min_item() == 0); + + req_double_test_sketch target(K, hra, std::less(), test_allocator(0)); + target.reserve(); + target.merge(sketch); + REQUIRE(target.get_n() == sketch.get_n()); + REQUIRE(target.get_quantile(0.5) == sketch.get_quantile(0.5)); + } + } + REQUIRE(test_allocator_total_bytes == 0); + REQUIRE(test_allocator_net_allocations == 0); +} + +TEST_CASE("req sketch allocation: an empty sketch has no item storage", "[req_sketch]") { + test_allocator_total_bytes = 0; + test_allocator_net_allocations = 0; + { + req_double_test_sketch sketch(K, true, std::less(), test_allocator(0)); + const long long empty_bytes = test_allocator_total_bytes; + REQUIRE(empty_bytes < static_cast(2 * 2 * 3 * K * sizeof(double))); + + req_double_test_sketch copy(sketch); + req_double_test_sketch target(K, true, std::less(), test_allocator(0)); + target.merge(sketch); // merging an empty sketch allocates no item storage + REQUIRE(test_allocator_total_bytes == 3 * empty_bytes); + REQUIRE(target.is_empty()); + + const auto b = sketch.serialize(); + auto deserialized = req_double_test_sketch::deserialize(b.data(), b.size(), serde(), + std::less(), test_allocator(0)); + REQUIRE(deserialized.is_empty()); + deserialized.update(1); + REQUIRE(deserialized.get_n() == 1); + } + REQUIRE(test_allocator_total_bytes == 0); + REQUIRE(test_allocator_net_allocations == 0); +} + +TEST_CASE("req sketch: iterating skips levels without items", "[req_sketch]") { + for (bool hra: {true, false}) { + req_sketch sketch(K, hra); + REQUIRE(sketch.begin() == sketch.end()); + for (const auto pair: sketch) { (void) pair; FAIL("an empty sketch has no items"); } + sketch.reserve(); + REQUIRE(sketch.begin() == sketch.end()); + + // a merge can leave level zero without items while higher levels have them + req_sketch source(K, hra); + for (int i = 0; i < 10000; ++i) source.update(i); + req_sketch target(K, hra); + target.merge(source); + uint64_t total_weight = 0; + for (const auto pair: target) total_weight += pair.second; + REQUIRE(total_weight == target.get_n()); + } +} + +TEST_CASE("req sketch allocation: reserve after updates does nothing", "[req_sketch]") { + test_allocator_total_bytes = 0; + test_allocator_net_allocations = 0; + { + req_double_test_sketch sketch(K, true, std::less(), test_allocator(0)); + for (int i = 0; i < 1000; ++i) sketch.update(i); + const auto b = sketch.serialize(); + const long long allocated = test_allocator_total_bytes; + sketch.reserve(); + REQUIRE(test_allocator_total_bytes == allocated); + REQUIRE(sketch.serialize() == b); + } + REQUIRE(test_allocator_total_bytes == 0); + REQUIRE(test_allocator_net_allocations == 0); +} + +TEST_CASE("req sketch allocation: failed reserve", "[req_sketch]") { + test_allocator_total_bytes = 0; + test_allocator_net_allocations = 0; + { + allocation_control control; + const sketch_type fresh = make_sketch(0, 0, control); + sketch_type sketch = make_sketch(0, 0, control); + control.arm(0); + REQUIRE_THROWS_AS(sketch.reserve(), std::bad_alloc); + REQUIRE(sketch.is_empty()); + REQUIRE(bytes(sketch) == bytes(fresh)); + check_usable(sketch, control); + } + REQUIRE(test_allocator_total_bytes == 0); + REQUIRE(test_allocator_net_allocations == 0); +} + +TEST_CASE("req sketch allocation: failed first update leaves the sketch unchanged", "[req_sketch]") { + test_allocator_total_bytes = 0; + test_allocator_net_allocations = 0; + { + allocation_control control; + for (bool hra: {true, false}) { + const sketch_type fresh = make_sketch(0, 0, control, hra); + sketch_type sketch = make_sketch(0, 0, control, hra); + control.arm(0); + REQUIRE_THROWS_AS(sketch.update(item(1)), std::bad_alloc); + REQUIRE(sketch.is_empty()); + REQUIRE(sketch.get_num_retained() == 0); + REQUIRE(bytes(sketch) == bytes(fresh)); + + control.disarm(); + sketch.update(item(2)); + REQUIRE(sketch.get_n() == 1); + REQUIRE(sketch.get_min_item() == item(2)); // nothing left over from the failed update + REQUIRE(sketch.get_max_item() == item(2)); + } + } + REQUIRE(test_allocator_total_bytes == 0); + REQUIRE(test_allocator_net_allocations == 0); +} + +// Fail each allocation made by a stream of updates in turn: the sketch must stay valid and usable. +TEST_CASE("req sketch allocation: failure at each allocation of the updates", "[req_sketch]") { + test_allocator_total_bytes = 0; + test_allocator_net_allocations = 0; + { + allocation_control control; + for (bool hra: {true, false}) { + for (long fail_at = 0; ; ++fail_at) { + sketch_type sketch(K, hra, std::less(), failing_allocator(control)); + control.arm(fail_at); + try { + for (int i = 0; i < 5000; ++i) sketch.update(item(i)); + control.disarm(); + REQUIRE(fail_at > 0); + break; + } catch (const std::bad_alloc&) { + CAPTURE(hra, fail_at); + control.disarm(); + check_consistent(sketch); + // it must keep compressing as updates continue: retain no more than a sketch that never failed + for (int i = 0; i < 20000; ++i) sketch.update(item(i)); + const sketch_type reference = make_sketch(0, static_cast(sketch.get_n()), control, hra); + REQUIRE(sketch.get_num_retained() <= 2 * reference.get_num_retained()); + check_usable(sketch, control); + } + } + } + } + REQUIRE(test_allocator_total_bytes == 0); + REQUIRE(test_allocator_net_allocations == 0); +} + +// Fail each allocation of target.merge(source) in turn: the target must stay valid and usable, +// and a failure of the first allocation, the item storage of an empty target, leaves it unchanged. +TEST_CASE("req sketch allocation: failure at each allocation of a merge", "[req_sketch]") { + test_allocator_total_bytes = 0; + test_allocator_net_allocations = 0; + { + allocation_control control; + long failures_tested = 0; + for (bool hra: {true, false}) { + for (int target_n: {0, 5, 1000}) { + for (int source_n: {5, 1000, 20000}) { + CAPTURE(hra, target_n, source_n); + const sketch_type source = make_sketch(100000, source_n, control, hra); + const auto source_bytes = bytes(source); + for (long fail_at = 0; ; ++fail_at) { + sketch_type target = make_sketch(0, target_n, control, hra); + control.arm(fail_at); + try { + target.merge(source); + control.disarm(); + REQUIRE(target.get_n() == static_cast(target_n + source_n)); + break; + } catch (const std::bad_alloc&) { + ++failures_tested; + control.disarm(); + CAPTURE(fail_at); + REQUIRE(bytes(source) == source_bytes); + check_usable(target, control); + } + } + } + } + } + REQUIRE(failures_tested > 0); + } + REQUIRE(test_allocator_total_bytes == 0); + REQUIRE(test_allocator_net_allocations == 0); +} + +} /* namespace datasketches */ From 84f79fc89f9a7d31394ed3a14123afa10a3ca7e3 Mon Sep 17 00:00:00 2001 From: Lee Rhodes Date: Thu, 12 Nov 2026 14:11:24 -0800 Subject: [PATCH 2/6] optional: emplace destroys the current value, as std::optional does emplace() constructed the new value over the current one without destroying it. The sketches call emplace() for min and max only when they are empty, so this didn't matter until a REQ merge into an empty sketch failed after setting min and max: the next update emplaced them again and leaked the old values. LeakSanitizer found it in CI. Co-Authored-By: Claude Opus 5.5 --- common/include/optional.hpp | 1 + common/test/optional_test.cpp | 25 +++++++++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/common/include/optional.hpp b/common/include/optional.hpp index 862b27e04..dd68e6405 100644 --- a/common/include/optional.hpp +++ b/common/include/optional.hpp @@ -114,6 +114,7 @@ class optional { template void emplace(Args&&... args) noexcept(std::is_nothrow_constructible::value) { + reset(); // destroys the current value, if any, as std::optional::emplace does new (&value_) T(args...); initialized_ = true; } diff --git a/common/test/optional_test.cpp b/common/test/optional_test.cpp index 3d2b8b358..85aa9f8f3 100644 --- a/common/test/optional_test.cpp +++ b/common/test/optional_test.cpp @@ -82,4 +82,29 @@ TEST_CASE("optional conversion", "[common]") { REQUIRE(*opt_d == static_cast(*opt_f)); } +// counts live instances +struct counted { + static int live; + int val; + counted(int v): val(v) { ++live; } + counted(const counted& other): val(other.val) { ++live; } + ~counted() { --live; } +}; +int counted::live = 0; + +TEST_CASE("optional emplace replaces the current value", "[common]") { + { + optional opt; + opt.emplace(1); + REQUIRE(counted::live == 1); + opt.emplace(2); // destroys the first value + REQUIRE(counted::live == 1); + REQUIRE(opt->val == 2); + opt.reset(); + REQUIRE(counted::live == 0); + opt.emplace(3); + } + REQUIRE(counted::live == 0); +} + } /* namespace datasketches */ From 9b9287eba4be83b163e23536c4bfe8180387bc43 Mon Sep 17 00:00:00 2001 From: Lee Rhodes Date: Sun, 15 Nov 2026 13:09:36 -0800 Subject: [PATCH 3/6] REQ: merging an empty compactor allocates no item storage ensure_space(0) returns before reserve(), as suggested in review. Co-Authored-By: Claude Opus 5.5 --- req/include/req_compactor_impl.hpp | 1 + req/test/req_sketch_allocation_test.cpp | 16 ++++++++++++++++ 2 files changed, 17 insertions(+) diff --git a/req/include/req_compactor_impl.hpp b/req/include/req_compactor_impl.hpp index 34d6fd4c7..bad8aa98d 100755 --- a/req/include/req_compactor_impl.hpp +++ b/req/include/req_compactor_impl.hpp @@ -226,6 +226,7 @@ void req_compactor::grow(uint32_t new_capacity) { template void req_compactor::ensure_space(uint32_t num) { + if (num == 0) return; // nothing to add, so an empty compactor stays unallocated reserve(); if (num_items_ + num > capacity_) grow(num_items_ + num + get_nom_capacity()); } diff --git a/req/test/req_sketch_allocation_test.cpp b/req/test/req_sketch_allocation_test.cpp index 0f1c1a677..fe9091669 100644 --- a/req/test/req_sketch_allocation_test.cpp +++ b/req/test/req_sketch_allocation_test.cpp @@ -167,6 +167,22 @@ TEST_CASE("req sketch allocation: an empty sketch has no item storage", "[req_sk REQUIRE(test_allocator_net_allocations == 0); } +TEST_CASE("req compactor allocation: merging an empty compactor allocates no item storage", "[req_sketch]") { + using compactor_type = req_compactor, test_allocator>; + for (bool hra: {true, false}) { + test_allocator_total_bytes = 0; + test_allocator_net_allocations = 0; + { + compactor_type target(hra, 0, K, std::less(), test_allocator(0)); + compactor_type source(hra, 0, K, std::less(), test_allocator(0)); + target.merge(source); + REQUIRE(test_allocator_total_bytes == 0); + REQUIRE(target.get_num_items() == 0); + } + REQUIRE(test_allocator_net_allocations == 0); + } +} + TEST_CASE("req sketch: iterating skips levels without items", "[req_sketch]") { for (bool hra: {true, false}) { req_sketch sketch(K, hra); From fd662ddddd2cb992badc527bf75f65658b08d811 Mon Sep 17 00:00:00 2001 From: Lee Rhodes Date: Mon, 16 Nov 2026 14:22:05 -0800 Subject: [PATCH 4/6] REQ: test that deserializing an empty image allocates no item storage 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 --- req/test/req_sketch_allocation_test.cpp | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/req/test/req_sketch_allocation_test.cpp b/req/test/req_sketch_allocation_test.cpp index fe9091669..68aa1f6b2 100644 --- a/req/test/req_sketch_allocation_test.cpp +++ b/req/test/req_sketch_allocation_test.cpp @@ -19,6 +19,7 @@ #include #include +#include #include #include @@ -157,11 +158,21 @@ TEST_CASE("req sketch allocation: an empty sketch has no item storage", "[req_sk REQUIRE(target.is_empty()); const auto b = sketch.serialize(); + const long long bytes_before = test_allocator_total_bytes; auto deserialized = req_double_test_sketch::deserialize(b.data(), b.size(), serde(), std::less(), test_allocator(0)); + REQUIRE(test_allocator_total_bytes - bytes_before == empty_bytes); // no item storage, like a new sketch REQUIRE(deserialized.is_empty()); deserialized.update(1); REQUIRE(deserialized.get_n() == 1); + + std::stringstream s(std::ios::in | std::ios::out | std::ios::binary); + sketch.serialize(s); + const long long stream_bytes_before = test_allocator_total_bytes; + auto from_stream = req_double_test_sketch::deserialize(s, serde(), std::less(), + test_allocator(0)); + REQUIRE(test_allocator_total_bytes - stream_bytes_before == empty_bytes); + REQUIRE(from_stream.is_empty()); } REQUIRE(test_allocator_total_bytes == 0); REQUIRE(test_allocator_net_allocations == 0); From ccbcf6acf632ad50710e903e13ca457f9a8b4b63 Mon Sep 17 00:00:00 2001 From: Lee Rhodes Date: Tue, 17 Nov 2026 14:38:42 -0800 Subject: [PATCH 5/6] REQ: a failed merge removes the levels it added If merge() grew the sketch and then failed, the new levels stayed behind empty, so a sketch could report estimation mode with no items above level 0, and its rank bounds used too many levels. Remove the added levels that received no items. Grow before widening min and max, so that a failure to grow changes nothing. Co-Authored-By: Claude Opus 5.5 --- req/include/req_sketch_impl.hpp | 17 ++++++++++------- req/test/req_sketch_allocation_test.cpp | 6 ++++++ 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/req/include/req_sketch_impl.hpp b/req/include/req_sketch_impl.hpp index b6ccf2fe6..ab19d43ed 100644 --- a/req/include/req_sketch_impl.hpp +++ b/req/include/req_sketch_impl.hpp @@ -194,21 +194,24 @@ void req_sketch::merge(FwdSk&& other) { if (is_HRA() != other.is_HRA()) throw std::invalid_argument("merging HRA and LRA is not valid"); if (other.is_empty()) return; reset_sorted_view(); - if (is_empty()) { - min_item_.emplace(conditional_forward(*other.min_item_)); - max_item_.emplace(conditional_forward(*other.max_item_)); - } else { - if (comparator_(*other.min_item_, *min_item_)) *min_item_ = conditional_forward(*other.min_item_); - if (comparator_(*max_item_, *other.max_item_)) *max_item_ = conditional_forward(*other.max_item_); - } + const size_t num_levels = compactors_.size(); try { // grow until this has at least as many compactors as other while (get_num_levels() < other.get_num_levels()) grow(); + if (is_empty()) { + min_item_.emplace(conditional_forward(*other.min_item_)); + max_item_.emplace(conditional_forward(*other.max_item_)); + } else { + if (comparator_(*other.min_item_, *min_item_)) *min_item_ = conditional_forward(*other.min_item_); + if (comparator_(*max_item_, *other.max_item_)) *max_item_ = conditional_forward(*other.max_item_); + } // merge the items in all height compactors for (size_t i = 0; i < other.get_num_levels(); ++i) { compactors_[i].merge(conditional_forward(other.compactors_[i])); } } catch (...) { + // remove the levels added for this merge that received no items + while (compactors_.size() > num_levels && compactors_.back().get_num_items() == 0) compactors_.pop_back(); restore_counts(); // some levels may be merged already throw; } diff --git a/req/test/req_sketch_allocation_test.cpp b/req/test/req_sketch_allocation_test.cpp index 68aa1f6b2..2ee7fd07a 100644 --- a/req/test/req_sketch_allocation_test.cpp +++ b/req/test/req_sketch_allocation_test.cpp @@ -319,6 +319,7 @@ TEST_CASE("req sketch allocation: failure at each allocation of a merge", "[req_ const auto source_bytes = bytes(source); for (long fail_at = 0; ; ++fail_at) { sketch_type target = make_sketch(0, target_n, control, hra); + const bool was_estimation_mode = target.is_estimation_mode(); control.arm(fail_at); try { target.merge(source); @@ -330,6 +331,11 @@ TEST_CASE("req sketch allocation: failure at each allocation of a merge", "[req_ control.disarm(); CAPTURE(fail_at); REQUIRE(bytes(source) == source_bytes); + // levels added for the merge must not stay behind empty: if no items were merged, + // the target has the same levels as before + if (target.get_n() == static_cast(target_n)) { + REQUIRE(target.is_estimation_mode() == was_estimation_mode); + } check_usable(target, control); } } From 1267c75183acba302152ea4ed6f1f7107c94f653 Mon Sep 17 00:00:00 2001 From: Lee Rhodes Date: Wed, 18 Nov 2026 17:31:10 -0800 Subject: [PATCH 6/6] REQ: remove an empty level added by a failed compression compress() can add a level and then fail to compact into it, which left the sketch in estimation mode with an empty top level. On failure it now removes the levels it added that hold no items, like merge() does; both use a new helper, remove_empty_levels_above(). The allocation tests now check that a sketch in estimation mode retains an item of weight above 1, which fails without this change. Co-Authored-By: Claude Opus 5.5 --- req/include/req_sketch.hpp | 2 ++ req/include/req_sketch_impl.hpp | 10 ++++++++-- req/test/req_sketch_allocation_test.cpp | 5 +++++ 3 files changed, 15 insertions(+), 2 deletions(-) diff --git a/req/include/req_sketch.hpp b/req/include/req_sketch.hpp index 79654000a..8e3468f05 100755 --- a/req/include/req_sketch.hpp +++ b/req/include/req_sketch.hpp @@ -479,6 +479,8 @@ class req_sketch { void restore_counts(); void compress(); void compress_levels(); + // removes the levels above the given number that hold no items, after a failed merge or compression + void remove_empty_levels_above(size_t num_levels); static double get_rank_lb(uint16_t k, uint8_t num_levels, double rank, uint8_t num_std_dev, uint64_t n, bool hra); static double get_rank_ub(uint16_t k, uint8_t num_levels, double rank, uint8_t num_std_dev, uint64_t n, bool hra); diff --git a/req/include/req_sketch_impl.hpp b/req/include/req_sketch_impl.hpp index ab19d43ed..d6f4c8e4f 100644 --- a/req/include/req_sketch_impl.hpp +++ b/req/include/req_sketch_impl.hpp @@ -210,8 +210,7 @@ void req_sketch::merge(FwdSk&& other) { compactors_[i].merge(conditional_forward(other.compactors_[i])); } } catch (...) { - // remove the levels added for this merge that received no items - while (compactors_.size() > num_levels && compactors_.back().get_num_items() == 0) compactors_.pop_back(); + remove_empty_levels_above(num_levels); restore_counts(); // some levels may be merged already throw; } @@ -656,14 +655,21 @@ void req_sketch::restore_counts() { template void req_sketch::compress() { + const size_t num_levels = compactors_.size(); try { compress_levels(); } catch (...) { + remove_empty_levels_above(num_levels); // a level can be added, and then the compaction into it fail restore_counts(); // a compactor can fail to grow after a compaction moved its items throw; } } +template +void req_sketch::remove_empty_levels_above(size_t num_levels) { + while (compactors_.size() > num_levels && compactors_.back().get_num_items() == 0) compactors_.pop_back(); +} + template void req_sketch::compress_levels() { for (size_t h = 0; h < compactors_.size(); ++h) { diff --git a/req/test/req_sketch_allocation_test.cpp b/req/test/req_sketch_allocation_test.cpp index 2ee7fd07a..1857cf0ed 100644 --- a/req/test/req_sketch_allocation_test.cpp +++ b/req/test/req_sketch_allocation_test.cpp @@ -18,6 +18,7 @@ */ #include +#include #include #include #include @@ -80,13 +81,17 @@ std::vector bytes(const sketch_type& sketch) { // the sketch must be consistent: the weights of the retained items add up to n void check_consistent(const sketch_type& sketch) { uint64_t total_weight = 0; + uint64_t max_weight = 0; uint32_t num_items = 0; for (const auto pair: sketch) { total_weight += pair.second; + max_weight = std::max(max_weight, pair.second); ++num_items; } REQUIRE(total_weight == sketch.get_n()); REQUIRE(num_items == sketch.get_num_retained()); + // no empty level is left above level zero + REQUIRE(sketch.is_estimation_mode() == (max_weight > 1)); } // the sketch must still work: query it, update it, merge into it, and round-trip it