Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions kll/include/kll_sketch_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,15 @@ template<typename FwdT>
void kll_sketch<T, C, A>::update(FwdT&& item) {
if (!check_update_item(item)) { return; }
update_min_max(static_cast<const T&>(item)); // min and max are always copies
const uint32_t index = internal_update();
new (&items_[index]) T(std::forward<FwdT>(item));
if (levels_[0] == 0) {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Compaction and regrowth can only happen when level 0 is full. So we use that fact to handle this situation.

// item can refer into items_, and internal_update() can destroy it
T copy(std::forward<FwdT>(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<FwdT>(item));
}
reset_sorted_view();
}

Expand Down
51 changes: 51 additions & 0 deletions kll/test/kll_sketch_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::string> 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<std::string> 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 */
Loading