Skip to content
Open
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
3 changes: 2 additions & 1 deletion kll/include/kll_helper_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,9 @@ uint8_t kll_helper::floor_of_log2_of_fraction(uint64_t numer, uint64_t denom) {
if (denom > numer) { return 0; }
uint8_t count = 0;
while (true) {
// denom * 2 > numer, without overflowing denom
if (denom > (numer >> 1)) { return count; }
denom <<= 1;
if (denom > numer) { return count; }
count++;
}
}
Expand Down
19 changes: 19 additions & 0 deletions kll/include/kll_sketch.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ namespace kll_constants {
const uint16_t MIN_K = DEFAULT_M;
/// max value of parameter K
const uint16_t MAX_K = (1 << 16) - 1;
/// maximum weight of a weighted update, the same as in datasketches-java
const uint64_t MAX_WEIGHT = (1ULL << 61) - 1;
}

/**
Expand Down Expand Up @@ -246,6 +248,20 @@ class kll_sketch {
template<typename FwdT>
void update(FwdT&& item);

/**
* Updates this sketch with the given data item repeated the given number of times.
* The result is equivalent to calling update(item) weight times, at a cost that grows
* with the logarithm of the weight rather than with the weight itself.
* 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
* @param weight number of times the item is repeated, from 1 to kll_constants::MAX_WEIGHT (2^61 - 1)
* @throw std::invalid_argument if the weight is 0 or greater than kll_constants::MAX_WEIGHT
*/
template<typename FwdT>
void update(FwdT&& item, uint64_t weight);

/**
* Merges another sketch into this one.
* If sketches contain strings, callers are responsible for ensuring that
Expand Down Expand Up @@ -592,6 +608,9 @@ class kll_sketch {
std::unique_ptr<T, items_deleter> items, uint32_t items_size, optional<T>&& min_item,
optional<T>&& max_item, bool is_level_zero_sorted, const C& comparator);

// for weighted update
kll_sketch(uint16_t k, const T& item, uint64_t weight, const C& comparator, const A& allocator);

// common update code
inline void update_min_max(const T& item);
// items_ is allocated lazily, and these are the only places that deal with it being nullptr:
Expand Down
74 changes: 72 additions & 2 deletions kll/include/kll_sketch_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,70 @@ void kll_sketch<T, C, A>::update(FwdT&& item) {
reset_sorted_view();
}

template<typename T, typename C, typename A>
template<typename FwdT>
void kll_sketch<T, C, A>::update(FwdT&& item, uint64_t weight) {
if (!check_update_item(item)) { return; }
if (weight == 0) throw std::invalid_argument("weight must be at least 1");
if (weight > kll_constants::MAX_WEIGHT) {
throw std::invalid_argument("weight must be less than 2^61: " + std::to_string(weight));
}
if (weight == 1) {
update(std::forward<FwdT>(item));
return;
}
allocate_items(); // first, so that a failure leaves the sketch unchanged
reset_sorted_view(); // before any change, so that a failed copy can't leave a stale view
if (weight < levels_[0]) { // fits into level zero without compaction
update_min_max(static_cast<const T&>(item));
// construct each copy before counting it, so that a failed copy leaves a valid sketch
for (uint64_t i = 1; i < weight; ++i) {
new (&items_[levels_[0] - 1]) T(static_cast<const T&>(item));
internal_update();
}
Comment thread
proost marked this conversation as resolved.
Resolved
new (&items_[levels_[0] - 1]) T(std::forward<FwdT>(item));
internal_update();
} else {
merge(kll_sketch(k_, static_cast<const T&>(item), weight, comparator_, allocator_));
}
}

// An item at level h carries weight 2^h, so one copy of the item at each level
// whose bit is set in the weight is an exact sketch of that many copies.
// The weight is less than 2^61, so this needs at most 61 levels.
template<typename T, typename C, typename A>
kll_sketch<T, C, A>::kll_sketch(uint16_t k, const T& item, uint64_t weight, const C& comparator, const A& allocator):
comparator_(comparator),
allocator_(allocator),
k_(k),
m_(kll_constants::DEFAULT_M),
min_k_(k),
num_levels_(static_cast<uint8_t>(64 - count_leading_zeros_in_u64(weight))),
is_level_zero_sorted_(true),
n_(weight),
levels_(num_levels_ + 1, 0, allocator),
items_(nullptr),
items_size_(0),
min_item_(item),
max_item_(item),
sorted_view_(nullptr)
{
for (uint8_t level = 0; level < num_levels_; ++level) {
levels_[level + 1] = levels_[level] + static_cast<uint32_t>((weight >> level) & 1);
}
items_size_ = levels_[num_levels_];
items_ = allocator_.allocate(items_size_);
// the destructor doesn't run if a constructor throws, so release the items here
uint32_t num_constructed = 0;
try {
for (; num_constructed < items_size_; ++num_constructed) new (&items_[num_constructed]) T(item);
} catch (...) {
for (uint32_t i = 0; i < num_constructed; ++i) items_[i].~T();
allocator_.deallocate(items_, items_size_);
throw;
}
}

template<typename T, typename C, typename A>
void kll_sketch<T, C, A>::update_min_max(const T& item) {
if (is_empty()) {
Expand Down Expand Up @@ -257,7 +321,7 @@ void kll_sketch<T, C, A>::merge(FwdSk&& other) {
const uint32_t index = internal_update();
new (&items_[index]) T(conditional_forward<FwdSk>(other.items_[i]));
}
if (other.num_levels_ >= 2) { merge_higher_levels(other, final_n); }
if (other.num_levels_ >= 2) { merge_higher_levels(std::forward<FwdSk>(other), final_n); }
n_ = final_n;
if (other.is_estimation_mode()) { min_k_ = std::min(min_k_, other.min_k_); }
assert_correct_total_weight();
Expand Down Expand Up @@ -1097,7 +1161,13 @@ void kll_sketch<T, C, A>::reset_sorted_view() {
template<typename T, typename C, typename A>
kll_sketch<T, C, A>::const_iterator::const_iterator(const T* items, const uint32_t* levels, const uint8_t num_levels):
items(items), levels(levels), num_levels(num_levels), index(items == nullptr ? levels[num_levels] : levels[0]), level(items == nullptr ? num_levels : 0), weight(1)
{}
{
// start at the first non-empty level: level zero can be empty after a merge
while (level < num_levels && levels[level] == levels[level + 1]) {
++level;
weight *= 2;
}
}

template<typename T, typename C, typename A>
typename kll_sketch<T, C, A>::const_iterator& kll_sketch<T, C, A>::const_iterator::operator++() {
Expand Down
113 changes: 113 additions & 0 deletions kll/test/kll_sketch_allocation_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@

#include <catch2/catch.hpp>
#include <new>
#include <stdexcept>
#include <string>
#include <utility>

#include <kll_sketch.hpp>
#include <test_allocator.hpp>
Expand Down Expand Up @@ -227,4 +229,115 @@ TEST_CASE("kll sketch allocation: failure at each allocation of the updates", "[
REQUIRE(test_allocator_net_allocations == 0);
}

TEST_CASE("kll sketch allocation: failed first weighted update leaves the sketch unchanged", "[kll_sketch]") {
test_allocator_total_bytes = 0;
test_allocator_net_allocations = 0;
{
allocation_control control;
const failing_allocator<std::string> alloc(control);
const kll_string_failing_sketch fresh(200, std::less<std::string>(), alloc);
for (const uint64_t weight: {uint64_t(10), uint64_t(1000)}) { // copies into level zero, and a merge
kll_string_failing_sketch sketch(200, std::less<std::string>(), alloc);
control.arm(0);
REQUIRE_THROWS_AS(sketch.update(item(1), weight), std::bad_alloc);
check_empty(sketch, fresh);
control.disarm();
sketch.update(item(2), weight);
REQUIRE(sketch.get_n() == weight);
REQUIRE(sketch.get_min_item() == item(2));
}
}
REQUIRE(test_allocator_total_bytes == 0);
REQUIRE(test_allocator_net_allocations == 0);
}

// Fail each allocation made by a stream of weighted updates in turn: the sketch must stay valid and usable.
TEST_CASE("kll sketch allocation: failure at each allocation of the weighted updates", "[kll_sketch]") {
test_allocator_total_bytes = 0;
test_allocator_net_allocations = 0;
{
allocation_control control;
const failing_allocator<std::string> alloc(control);
const kll_string_failing_sketch fresh(8, std::less<std::string>(), alloc);
bool completed = false;
for (long fail_at = 0; !completed; ++fail_at) {
kll_string_failing_sketch sketch(8, std::less<std::string>(), alloc);
control.arm(fail_at);
try {
for (int i = 0; i < 300; ++i) sketch.update(item(i), static_cast<uint64_t>(i % 7) * 100 + 1);
completed = true;
REQUIRE(fail_at > 0);
} catch (const std::bad_alloc&) {
if (fail_at == 0) check_empty(sketch, fresh);
check_usable(sketch, control);
}
control.disarm();
}
}
REQUIRE(test_allocator_total_bytes == 0);
REQUIRE(test_allocator_net_allocations == 0);
}

namespace {

long copies_allowed = -1; // copies left before a copy throws; negative means no limit

// An item whose copy constructor throws once copies_allowed runs out.
// Each item owns memory from test_allocator, so an item that is never destroyed shows up as a leak.
struct fragile_item {
int* value;
explicit fragile_item(int v): value(test_allocator<int>(0).allocate(1)) { *value = v; }
fragile_item(const fragile_item& other): value(nullptr) {
if (copies_allowed == 0) throw std::runtime_error("copy failed");
if (copies_allowed > 0) --copies_allowed;
value = test_allocator<int>(0).allocate(1);
*value = *other.value;
}
fragile_item(fragile_item&& other) noexcept: value(other.value) { other.value = nullptr; }
fragile_item& operator=(fragile_item other) noexcept { std::swap(value, other.value); return *this; }
~fragile_item() { if (value != nullptr) test_allocator<int>(0).deallocate(value, 1); }
};

struct fragile_less {
bool operator()(const fragile_item& a, const fragile_item& b) const { return *a.value < *b.value; }
};

using kll_fragile_sketch = kll_sketch<fragile_item, fragile_less, test_allocator<fragile_item>>;

} // namespace

// Fail each item copy made by a weighted update in turn: nothing may leak,
// and the sketch must stay usable, with no stale sorted view.
TEST_CASE("kll sketch allocation: failure at each item copy of a weighted update", "[kll_sketch]") {
test_allocator_total_bytes = 0;
test_allocator_net_allocations = 0;
for (const uint64_t weight: {uint64_t(10), uint64_t(1000)}) { // copies into level zero, and a merge
CAPTURE(weight);
bool completed = false;
for (long fail_at = 0; !completed; ++fail_at) {
CAPTURE(fail_at);
kll_fragile_sketch sketch(200, fragile_less(), test_allocator<fragile_item>(0));
for (int i = 0; i < 5; ++i) sketch.update(fragile_item(i));
const fragile_item probe(2);
const double rank_before = sketch.get_rank(probe); // caches a sorted view
REQUIRE(rank_before > 0);
copies_allowed = fail_at;
try {
sketch.update(fragile_item(100), weight);
completed = true;
} catch (const std::runtime_error&) {}
copies_allowed = -1;
REQUIRE(sketch.get_n() >= 5);
REQUIRE(sketch.get_n() <= 5 + weight);
// a copy of the sketch builds its sorted view from the current items
const kll_fragile_sketch copy(sketch);
REQUIRE(sketch.get_rank(probe) == copy.get_rank(probe));
sketch.update(fragile_item(-1));
REQUIRE(*sketch.get_min_item().value == -1);
}
}
REQUIRE(test_allocator_total_bytes == 0);
REQUIRE(test_allocator_net_allocations == 0);
}

} /* namespace datasketches */
Loading
Loading