Skip to content
14 changes: 11 additions & 3 deletions kll/include/kll_sketch.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,8 @@ class kll_sketch {
/**
* Constructor.
* Storage for items is allocated on first use, see reserve().
* The items buffer starts with space for min(k, DEFAULT_K) items (k items if RESIZE_FACTOR is X1),
* and grows by RESIZE_FACTOR up to k items before the first compaction.
* @param k affects the size of the sketch and its estimation error
* @param comparator strict weak ordering function (see C++ named requirements: Compare)
* @param allocator used by this sketch to allocate memory
Expand Down Expand Up @@ -259,10 +261,11 @@ class kll_sketch {
void merge(FwdSk&& other);

/**
* Allocates storage for items now, rather than on the first update or merge.
* Allocates storage for k items now, rather than on the first update or merge,
* or as the buffer grows. The buffer then does not need to grow before the first compaction.
* 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.
* or if updates should not pay for the allocation.
* Does nothing if the storage for k items is already allocated.
* @throw std::bad_alloc if the allocation fails, in which case the sketch is unchanged
*/
void reserve();
Expand Down Expand Up @@ -571,6 +574,10 @@ class kll_sketch {
static const uint8_t PREAMBLE_INTS_SHORT = 2; // for empty and single item
static const uint8_t PREAMBLE_INTS_FULL = 5;

// growth factor of the items buffer until it holds k items
static const resize_factor RESIZE_FACTOR = resize_factor::X8;
static_assert(RESIZE_FACTOR >= resize_factor::X1, "RESIZE_FACTOR must be at least X1");

C comparator_;
A allocator_;
uint16_t k_;
Expand Down Expand Up @@ -609,6 +616,7 @@ class kll_sketch {

uint8_t find_level_to_compact() const;
void add_empty_top_level_to_completely_full_sketch();
void grow_items(uint32_t new_size); // only while there is one level
void sort_level_zero();

template<typename O> void merge_higher_levels(O&& other, uint64_t final_n);
Expand Down
51 changes: 44 additions & 7 deletions kll/include/kll_sketch_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ is_level_zero_sorted_(false),
n_(0),
levels_(2, 0, allocator),
items_(nullptr),
items_size_(k_),
items_size_(RESIZE_FACTOR == resize_factor::X1 ? k_ : std::min(k_, kll_constants::DEFAULT_K)),
min_item_(),
max_item_(),
sorted_view_(nullptr)
Expand All @@ -54,7 +54,7 @@ sorted_view_(nullptr)
throw std::invalid_argument("K must be >= " + std::to_string(kll_constants::MIN_K) + " and <= "
+ std::to_string(kll_constants::MAX_K) + ": " + std::to_string(k));
}
levels_[0] = levels_[1] = k;
levels_[0] = levels_[1] = items_size_;
// items_ is allocated by the first update or merge: many sketches (e.g. per-group aggregation states)
// stay empty or are only ever merged into
}
Expand Down Expand Up @@ -206,7 +206,15 @@ void kll_sketch<T, C, A>::update_min_max(const T& item) {

template<typename T, typename C, typename A>
void kll_sketch<T, C, A>::reserve() {
allocate_items();
if (items_ == nullptr) {
// nothing is stored yet, so the buffer can start at its full size
items_ = allocator_.allocate(k_);
items_size_ = k_;
levels_[0] = levels_[1] = k_;
} else if (num_levels_ == 1 && items_size_ < k_) {
grow_items(k_); // the buffer is still growing: grow it to its full size in one step

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

After this, we should reset sorted view.

Considering get_rank -> reserve -> get_rank case. At the first get_rank, we cached view. But at reserve call we destroy. And second get_rank derefer dangling pointer.

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.

Done, also added test that would have caught this.

reset_sorted_view(); // the cached view points into the old buffer
}
}

template<typename T, typename C, typename A>
Expand All @@ -230,7 +238,13 @@ T* kll_sketch<T, C, A>::item_ptr(uint32_t index) const {
template<typename T, typename C, typename A>
uint32_t kll_sketch<T, C, A>::internal_update() {
allocate_items();
if (levels_[0] == 0) { compress_while_updating(); }
if (levels_[0] == 0) {
if (items_size_ < k_) {
grow_items(std::min<uint32_t>(items_size_ << static_cast<uint8_t>(RESIZE_FACTOR), k_));
} else {
compress_while_updating();
}
}
n_++;
is_level_zero_sorted_ = false;
return --levels_[0];
Expand Down Expand Up @@ -422,7 +436,11 @@ void kll_sketch<T, C, A>::serialize(std::ostream& os, const SerDe& sd) const {
write(os, min_k_);
write(os, num_levels_);
write(os, unused);
write(os, levels_.data(), sizeof(levels_[0]) * num_levels_);
if (num_levels_ == 1 && items_size_ < k_) {
write(os, levels_[0] + (k_ - items_size_));
} else {
write(os, levels_.data(), sizeof(levels_[0]) * num_levels_);
}
sd.serialize(os, &*min_item_, 1);
sd.serialize(os, &*max_item_, 1);
}
Expand Down Expand Up @@ -458,7 +476,11 @@ auto kll_sketch<T, C, A>::serialize(unsigned header_size_bytes, const SerDe& sd)
ptr += copy_to_mem(min_k_, ptr);
ptr += copy_to_mem(num_levels_, ptr);
ptr += sizeof(uint8_t); // unused
ptr += copy_to_mem(levels_.data(), ptr, sizeof(levels_[0]) * num_levels_);
if (num_levels_ == 1 && items_size_ < k_) {
ptr += copy_to_mem(levels_[0] + (k_ - items_size_), ptr);
} else {
ptr += copy_to_mem(levels_.data(), ptr, sizeof(levels_[0]) * num_levels_);
}
ptr += sd.serialize(ptr, end_ptr - ptr, &*min_item_, 1);
ptr += sd.serialize(ptr, end_ptr - ptr, &*max_item_, 1);
}
Expand Down Expand Up @@ -786,6 +808,20 @@ void kll_sketch<T, C, A>::add_empty_top_level_to_completely_full_sketch() {
levels_[num_levels_] = new_total_cap; // initialize the new "extra" index at the top
}

// Grows the items buffer of a one-level sketch when it is full and smaller than k.
// Compaction starts only when the buffer is full at k items, so growing does not change the result.
template<typename T, typename C, typename A>
void kll_sketch<T, C, A>::grow_items(uint32_t new_size) {
const uint32_t delta = new_size - items_size_;
T* new_items = allocator_.allocate(new_size);
kll_helper::move_construct<T>(items_, levels_[0], levels_[1], new_items, levels_[0] + delta, true);
allocator_.deallocate(items_, items_size_);
items_ = new_items;
items_size_ = new_size;
levels_[0] += delta;
levels_[1] += delta;
}

template<typename T, typename C, typename A>
void kll_sketch<T, C, A>::sort_level_zero() {
if (!is_level_zero_sorted_) {
Expand Down Expand Up @@ -1011,7 +1047,8 @@ string<A> kll_sketch<T, C, A>::to_string(bool print_levels, bool print_items) co
os << " Estimation mode: " << (is_estimation_mode() ? "true" : "false") << std::endl;
os << " Levels : " << (unsigned int) num_levels_ << std::endl;
os << " Sorted : " << (is_level_zero_sorted_ ? "true" : "false") << std::endl;
os << " Capacity items : " << items_size_ << std::endl;
// a buffer still growing to k items shows the capacity of a full one
os << " Capacity items : " << std::max<uint32_t>(items_size_, k_) << std::endl;
os << " Retained items : " << get_num_retained() << std::endl;
if (!is_empty()) {
os << " Min item : " << *min_item_ << std::endl;
Expand Down
82 changes: 82 additions & 0 deletions kll/test/kll_sketch_serialize_for_java.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,86 @@ TEST_CASE("kll sketch string generate", "[serialize_for_java]") {
}
}

// with k = 1000, the buffer holds 200 items up to n = 200, and n = 201 caps its growth at k
TEST_CASE("kll sketch float generate, k = 1000", "[serialize_for_java]") {
const unsigned n_arr[] = {0, 1, 10, 100, 200, 201, 1000};
for (const unsigned n: n_arr) {
kll_sketch<float> sketch(1000);
for (unsigned i = 1; i <= n; ++i) sketch.update(i);
std::ofstream os("kll_float_k1000_n" + std::to_string(n) + "_cpp.sk", std::ios::binary);
sketch.serialize(os);
}
}

TEST_CASE("kll sketch double generate, k = 1000", "[serialize_for_java]") {
const unsigned n_arr[] = {0, 1, 10, 100, 200, 201, 1000};
for (const unsigned n: n_arr) {
kll_sketch<double> sketch(1000);
for (unsigned i = 1; i <= n; ++i) sketch.update(i);
std::ofstream os("kll_double_k1000_n" + std::to_string(n) + "_cpp.sk", std::ios::binary);
sketch.serialize(os);
}
}

TEST_CASE("kll sketch long generate, k = 1000", "[serialize_for_java]") {
const unsigned n_arr[] = {0, 1, 10, 100, 200, 201, 1000};
for (const unsigned n: n_arr) {
kll_sketch<long> sketch(1000);
for (unsigned i = 1; i <= n; ++i) sketch.update(i);
std::ofstream os("kll_long_k1000_n" + std::to_string(n) + "_cpp.sk", std::ios::binary);
sketch.serialize(os);
}
}

TEST_CASE("kll sketch string generate, k = 1000", "[serialize_for_java]") {
const unsigned n_arr[] = {0, 1, 10, 100, 200, 201, 1000};
for (const unsigned n: n_arr) {
kll_sketch<std::string, compare_as_number> sketch(1000);
for (unsigned i = 1; i <= n; ++i) sketch.update(std::to_string(i));
std::ofstream os("kll_string_k1000_n" + std::to_string(n) + "_cpp.sk", std::ios::binary);
sketch.serialize(os);
}
}

// with k = 65535, the buffer is still growing up to n = 10000, and n = 12801 caps its growth at k
TEST_CASE("kll sketch float generate, k = 65535", "[serialize_for_java]") {
const unsigned n_arr[] = {0, 1, 10, 100, 1000, 10000, 12801};
for (const unsigned n: n_arr) {
kll_sketch<float> sketch(kll_constants::MAX_K);
for (unsigned i = 1; i <= n; ++i) sketch.update(i);
std::ofstream os("kll_float_k65535_n" + std::to_string(n) + "_cpp.sk", std::ios::binary);
sketch.serialize(os);
}
}

TEST_CASE("kll sketch double generate, k = 65535", "[serialize_for_java]") {
const unsigned n_arr[] = {0, 1, 10, 100, 1000, 10000, 12801};
for (const unsigned n: n_arr) {
kll_sketch<double> sketch(kll_constants::MAX_K);
for (unsigned i = 1; i <= n; ++i) sketch.update(i);
std::ofstream os("kll_double_k65535_n" + std::to_string(n) + "_cpp.sk", std::ios::binary);
sketch.serialize(os);
}
}

TEST_CASE("kll sketch long generate, k = 65535", "[serialize_for_java]") {
const unsigned n_arr[] = {0, 1, 10, 100, 1000, 10000, 12801};
for (const unsigned n: n_arr) {
kll_sketch<long> sketch(kll_constants::MAX_K);
for (unsigned i = 1; i <= n; ++i) sketch.update(i);
std::ofstream os("kll_long_k65535_n" + std::to_string(n) + "_cpp.sk", std::ios::binary);
sketch.serialize(os);
}
}

TEST_CASE("kll sketch string generate, k = 65535", "[serialize_for_java]") {
const unsigned n_arr[] = {0, 1, 10, 100, 1000, 10000, 12801};
for (const unsigned n: n_arr) {
kll_sketch<std::string, compare_as_number> sketch(kll_constants::MAX_K);
for (unsigned i = 1; i <= n; ++i) sketch.update(std::to_string(i));
std::ofstream os("kll_string_k65535_n" + std::to_string(n) + "_cpp.sk", std::ios::binary);
sketch.serialize(os);
}
}

} /* namespace datasketches */
Loading
Loading