diff --git a/kll/include/kll_sketch_impl.hpp b/kll/include/kll_sketch_impl.hpp index e1ed85ec..6e518b26 100644 --- a/kll/include/kll_sketch_impl.hpp +++ b/kll/include/kll_sketch_impl.hpp @@ -181,8 +181,15 @@ template void kll_sketch::update(FwdT&& item) { if (!check_update_item(item)) { return; } update_min_max(static_cast(item)); // min and max are always copies - const uint32_t index = internal_update(); - new (&items_[index]) T(std::forward(item)); + if (levels_[0] == 0) { + // item can refer into items_, and internal_update() can destroy it + T copy(std::forward(item)); + const uint32_t index = internal_update(); + new (&items_[index]) T(std::move(copy)); + } else { + const uint32_t index = internal_update(); + new (&items_[index]) T(std::forward(item)); + } reset_sorted_view(); } diff --git a/kll/test/kll_sketch_test.cpp b/kll/test/kll_sketch_test.cpp index 05eea991..490348a0 100644 --- a/kll/test/kll_sketch_test.cpp +++ b/kll/test/kll_sketch_test.cpp @@ -915,4 +915,55 @@ TEST_CASE("kll sketch: deserialize rejects inconsistent levels", "[kll_sketch]") } } +// Compaction: level 0 is full, so update(item) compacts it, which can free the item it reads +TEST_CASE("kll sketch: update with aliased item during compaction", "[kll_sketch]") { + const uint16_t k = 200; + kll_sketch sketch(k); + + for (int i = 0; i < kll_constants::DEFAULT_K; ++i) { + sketch.update(std::string(64, 'x') + std::to_string(i)); + } + + REQUIRE(sketch.get_n() == kll_constants::DEFAULT_K); + + const std::string& item = sketch.begin()->first; + const std::string expected = item; + + // Triggers compaction + sketch.update(item); + + REQUIRE(sketch.get_n() == kll_constants::DEFAULT_K + 1); + + // the compaction emptied level 0, so the first item is the one just added + REQUIRE(sketch.begin()->first == expected); + REQUIRE(sketch.begin()->second == 1); +} + +// Regrowth: if the buffer starts below k items, update(item) grows it, which can free the item +TEST_CASE("kll sketch: update with aliased item during buffer growth", "[kll_sketch]") { + const uint16_t k = 5000; + kll_sketch sketch(k); + + for (int i = 0; i < kll_constants::DEFAULT_K; ++i) { + sketch.update(std::string(64, 'x') + std::to_string(i)); + } + + REQUIRE(sketch.get_n() == kll_constants::DEFAULT_K); + + const std::string& item = sketch.begin()->first; + const std::string expected = item; + + sketch.update(item); + + REQUIRE(sketch.get_n() == kll_constants::DEFAULT_K + 1); + + uint32_t count = 0; + for (const auto& entry : sketch) { + if (entry.first == expected) { + ++count; + } + } + REQUIRE(count == 2); +} + } /* namespace datasketches */