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
8 changes: 4 additions & 4 deletions common/include/quantiles_sorted_view.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -196,21 +196,21 @@ class quantiles_sorted_view {
static inline Entry make_dummy_entry(uint64_t weight) { return Entry(nullptr, weight); }

template<typename TT = T, typename std::enable_if<std::is_floating_point<TT>::value, int>::type = 0>
static inline void check_split_points(const T* items, uint32_t size) {
inline void check_split_points(const T* items, uint32_t size) const {
for (uint32_t i = 0; i < size ; i++) {
if (std::isnan(items[i])) {
throw std::invalid_argument("Values must not be NaN");
}
if ((i < (size - 1)) && !(Comparator()(items[i], items[i + 1]))) {
if ((i < (size - 1)) && !(comparator_(items[i], items[i + 1]))) {
throw std::invalid_argument("Values must be unique and monotonically increasing");
}
}
}

template<typename TT = T, typename std::enable_if<!std::is_floating_point<TT>::value, int>::type = 0>
static inline void check_split_points(const T* items, uint32_t size) {
inline void check_split_points(const T* items, uint32_t size) const {
for (uint32_t i = 0; i < size ; i++) {
if ((i < (size - 1)) && !(Comparator()(items[i], items[i + 1]))) {
if ((i < (size - 1)) && !(comparator_(items[i], items[i + 1]))) {
throw std::invalid_argument("Items must be unique and monotonically increasing");
}
}
Expand Down
1 change: 1 addition & 0 deletions quantiles/include/quantiles_sketch.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@ class quantiles_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.
* A sketch can be merged into itself.
* If an allocation fails, this sketch is unchanged. If a copy of an item throws, this sketch
* stays valid and usable, but may hold part of other. If the comparator throws while items
* are sorted or merged, the state of this sketch is undefined.
Expand Down
6 changes: 6 additions & 0 deletions quantiles/include/quantiles_sketch_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,12 @@ void quantiles_sketch<T, C, A>::merge(FwdSk&& other) {
if (other.is_empty()) {
return; // nothing to do
}
if (static_cast<const void*>(&other) == static_cast<const void*>(this)) {
// merging reads other while it changes this, so merge a copy
quantiles_sketch copy(other);
merge(std::move(copy));
return;
}
reset_sorted_view();
try {
merge_unchecked(std::forward<FwdSk>(other));
Expand Down
25 changes: 25 additions & 0 deletions quantiles/test/quantiles_sketch_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -969,4 +969,29 @@ TEST_CASE("quantiles sketch: deserialize serial version 1 truncated after min an
REQUIRE_THROWS_AS(quantiles_sketch<int>::deserialize(bytes.data(), bytes.size()), std::out_of_range);
}

TEST_CASE("quantiles sketch: merge into itself", "[quantiles_sketch]") {
// exact and estimation mode, with a partly filled base buffer
for (int n: {5, 100, 1000}) {
CAPTURE(n);
quantiles_sketch<int> sketch(8);
for (int i = 0; i < n; ++i) sketch.update((i * 7919) % 10007);
quantiles_sketch<int> expected(sketch);
const quantiles_sketch<int> copy(sketch);
random_utils::rand.seed(1); random_utils::random_bit.seed(1);
expected.merge(copy);
const auto expected_bytes = expected.serialize();

quantiles_sketch<int> lvalue(sketch);
random_utils::rand.seed(1); random_utils::random_bit.seed(1);
lvalue.merge(lvalue);
REQUIRE(lvalue.get_n() == static_cast<uint64_t>(2 * n));
REQUIRE(lvalue.serialize() == expected_bytes);

quantiles_sketch<int> rvalue(sketch);
random_utils::rand.seed(1); random_utils::random_bit.seed(1);
rvalue.merge(std::move(rvalue));
REQUIRE(rvalue.serialize() == expected_bytes);
}
}

} /* namespace datasketches */
2 changes: 2 additions & 0 deletions req/include/req_common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ namespace req_constants {
const uint8_t INIT_NUM_SECTIONS = 3;
/// multiplier for nominal capacity
const unsigned MULTIPLIER = 2;
/// maximum stream length n, the largest signed 64-bit integer, as in datasketches-java
const uint64_t MAX_N = (1ULL << 63) - 1;
}

} /* namespace datasketches */
Expand Down
18 changes: 17 additions & 1 deletion req/include/req_compactor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,23 @@ class req_compactor {
uint32_t capacity_; // 0 while items_ is nullptr
T* items_; // nullptr until first use, see reserve()

bool ensure_enough_sections();
// the section parameters, which change as the state advances
struct sections {
float size_raw;
uint32_t size;
uint8_t num;
uint32_t nom_capacity() const { return req_constants::MULTIPLIER * num * size; }
};
sections get_sections() const;
void set_sections(const sections& s);
// one step of the section schedule for the given state: halves the section size (by sqrt(2)) and doubles
// the number of sections once the state reaches 2^(num - 1); returns false if the sections stay as they are
static bool next_sections(uint64_t state, sections& s);
// destroys the items and frees the storage
void destroy_items();
// allocates storage and copies the items of other, for a constructor; releases both if a copy throws
template<typename O>
void copy_items(const O& other);
std::pair<uint32_t, uint32_t> compute_compaction_range(uint32_t secs_to_compact) const;
void grow(uint32_t new_capacity);
void ensure_space(uint32_t num);
Expand Down
167 changes: 122 additions & 45 deletions req/include/req_compactor_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,34 @@ items_(nullptr) // allocated on first use, see reserve()

template<typename T, typename C, typename A>
req_compactor<T, C, A>::~req_compactor() {
destroy_items();
}

template<typename T, typename C, typename A>
void req_compactor<T, C, A>::destroy_items() {
if (items_ != nullptr) {
for (auto it = begin(); it != end(); ++it) (*it).~T();
allocator_.deallocate(items_, capacity_);
items_ = nullptr;
}
}

template<typename T, typename C, typename A>
template<typename O>
void req_compactor<T, C, A>::copy_items(const O& other) {
if (other.items_ == nullptr) return;
items_ = allocator_.allocate(capacity_);
const uint32_t from = hra_ ? capacity_ - num_items_ : 0;
const uint32_t to = hra_ ? capacity_ : num_items_;
uint32_t i = from;
try {
for (; i < to; ++i) new (items_ + i) T(other.items_[i]);
} catch (...) {
// the destructor doesn't run if a constructor throws, so release the items here
for (uint32_t j = from; j < i; ++j) items_[j].~T();
allocator_.deallocate(items_, capacity_);
items_ = nullptr;
throw;
}
}

Expand All @@ -72,12 +97,7 @@ num_items_(other.num_items_),
capacity_(other.capacity_),
items_(nullptr)
{
if (other.items_ != nullptr) {
items_ = allocator_.allocate(capacity_);
const uint32_t from = hra_ ? capacity_ - num_items_ : 0;
const uint32_t to = hra_ ? capacity_ : num_items_;
for (uint32_t i = from; i < to; ++i) new (items_ + i) T(other.items_[i]);
}
copy_items(other);
}

template<typename T, typename C, typename A>
Expand Down Expand Up @@ -155,14 +175,18 @@ num_items_(other.num_items_),
capacity_(other.capacity_),
items_(nullptr)
{
if (other.items_ != nullptr) {
items_ = allocator_.allocate(capacity_);
const uint32_t from = hra_ ? capacity_ - num_items_ : 0;
const uint32_t to = hra_ ? capacity_ : num_items_;
for (uint32_t i = from; i < to; ++i) new (items_ + i) T(other.items_[i]);
if (sorted_ && !std::is_sorted(items_ + from, items_ + to, comparator_)) {
throw std::logic_error("items must be sorted");
}
copy_items(other);
// the destructor doesn't run if a constructor throws, and the comparator may throw too
bool items_sorted = true;
try {
items_sorted = !sorted_ || std::is_sorted(begin(), end(), comparator_);
} catch (...) {
destroy_items();
throw;
}
if (!items_sorted) {
destroy_items();
throw std::logic_error("items must be sorted");
}
}

Expand Down Expand Up @@ -204,9 +228,17 @@ template<typename T, typename C, typename A>
template<typename FwdT>
void req_compactor<T, C, A>::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<FwdT>(item));
if (num_items_ == capacity_) {
// the item may be one of the items of this compactor, which growing destroys,
// so build it first; a failed copy then leaves the compactor as it was
T copy(std::forward<FwdT>(item));
grow(capacity_ + get_nom_capacity());
const uint32_t i = hra_ ? capacity_ - num_items_ - 1 : num_items_;
new (items_ + i) T(std::move(copy));
} else {
const uint32_t i = hra_ ? capacity_ - num_items_ - 1 : num_items_;
new (items_ + i) T(std::forward<FwdT>(item));
}
++num_items_;
if (num_items_ > 1) sorted_ = false;
}
Expand Down Expand Up @@ -256,18 +288,43 @@ template<typename FwdC>
void req_compactor<T, C, A>::merge(FwdC&& other) {
// TODO: swap if other is larger?
if (lg_weight_ != other.lg_weight_) throw std::logic_error("weight mismatch");
state_ |= other.state_;
while (ensure_enough_sections()) {}
ensure_space(other.get_num_items());
const uint32_t num = other.get_num_items();
const bool other_sorted = other.sorted_;
// the sections once the states are combined
const uint64_t new_state = state_ | other.state_;
sections new_sections = get_sections();
bool more_sections = false;
while (next_sections(new_state, new_sections)) more_sections = true;

// allocate first, so that a failure changes nothing:
// the capacity the sections and the items need, grown the same way as before this was done up front
uint32_t new_capacity = capacity_;
if (more_sections && new_capacity < 2 * new_sections.nom_capacity()) new_capacity = 2 * new_sections.nom_capacity();
if (num > 0) {
if (new_capacity == 0) new_capacity = 2 * new_sections.nom_capacity(); // as reserve() would
if (num_items_ + num > new_capacity) new_capacity = num_items_ + num + new_sections.nom_capacity();
}
if (new_capacity != capacity_) grow(new_capacity);

// copy (or move) the items of other into the free space; if a copy throws, destroy the copies
T* from = hra_ ? begin() - num : end();
uint32_t i = 0;
try {
auto other_it = other.begin();
for (; i < num; ++i, ++other_it) new (from + i) T(conditional_forward<FwdC>(*other_it));
} catch (...) {
for (uint32_t j = 0; j < i; ++j) from[j].~T();
throw;
}

// nothing below allocates or copies
state_ = new_state;
if (more_sections) set_sections(new_sections);
sort();
auto offset = hra_ ? capacity_ - num_items_ : num_items_;
auto from = hra_ ? begin() - other.get_num_items() : end();
auto to = from + other.get_num_items();
auto other_it = other.begin();
for (auto it = from; it != to; ++it, ++other_it) new (it) T(conditional_forward<FwdC>(*other_it));
if (!other.sorted_) std::sort(from, to, comparator_);
if (num_items_ > 0) std::inplace_merge(hra_ ? from : begin(), items_ + offset, hra_ ? end() : to, C());
num_items_ += other.get_num_items();
if (!other_sorted) std::sort(from, from + num, comparator_);
const auto offset = hra_ ? capacity_ - num_items_ : num_items_;
if (num_items_ > 0) std::inplace_merge(hra_ ? from : begin(), items_ + offset, hra_ ? end() : from + num, comparator_);
num_items_ += num;
}

template<typename T, typename C, typename A>
Expand All @@ -285,12 +342,18 @@ std::pair<uint32_t, uint32_t> req_compactor<T, C, A>::compact(req_compactor& nex
const uint32_t secs_to_compact = std::min<uint32_t>(count_trailing_zeros_in_u64(~state_) + 1, num_sections_);
auto compaction_range = compute_compaction_range(secs_to_compact);
if (compaction_range.second - compaction_range.first < 2) throw std::logic_error("compaction range error");
const auto num = (compaction_range.second - compaction_range.first) / 2;

// allocate first, so that a failure changes nothing: space in the next level,
// and the growth that more sections need once the state advances
next.ensure_space(num);
sections new_sections = get_sections();
const bool more_sections = next_sections(state_ + 1, new_sections);
if (more_sections && capacity_ < 2 * new_sections.nom_capacity()) grow(2 * new_sections.nom_capacity());

if ((state_ & 1) == 1) { coin_ = !coin_; } // for odd flip coin;
else { coin_ = random_utils::random_bit(); } // random coin flip

const auto num = (compaction_range.second - compaction_range.first) / 2;
next.ensure_space(num);
auto next_middle = hra_ ? next.begin() : next.end();
auto next_empty = hra_ ? next.begin() - num : next.end();
promote_evens_or_odds(begin() + compaction_range.first, begin() + compaction_range.second, coin_, next_empty);
Expand All @@ -300,24 +363,36 @@ std::pair<uint32_t, uint32_t> req_compactor<T, C, A>::compact(req_compactor& nex
num_items_ -= compaction_range.second - compaction_range.first;

++state_;
ensure_enough_sections();
if (more_sections) set_sections(new_sections);
return std::pair<uint32_t, uint32_t>(
num,
get_nom_capacity() - starting_nom_capacity
);
}

template<typename T, typename C, typename A>
bool req_compactor<T, C, A>::ensure_enough_sections() {
const float ssr = section_size_raw_ / sqrtf(2);
auto req_compactor<T, C, A>::get_sections() const -> sections {
return sections{section_size_raw_, section_size_, num_sections_};
}

template<typename T, typename C, typename A>
void req_compactor<T, C, A>::set_sections(const sections& s) {
section_size_raw_ = s.size_raw;
section_size_ = s.size;
num_sections_ = s.num;
}

template<typename T, typename C, typename A>
bool req_compactor<T, C, A>::next_sections(uint64_t state, sections& s) {
const float ssr = s.size_raw / sqrtf(2);
const uint32_t ne = nearest_even(ssr);
if (state_ >= static_cast<uint64_t>(1ULL << (num_sections_ - 1))
&& section_size_ > req_constants::MIN_K
if (s.num <= 32 // at most 48 sections, so the shift below stays in range
&& state >= static_cast<uint64_t>(1ULL << (s.num - 1))
&& s.size > req_constants::MIN_K
&& ne >= req_constants::MIN_K) {
section_size_raw_ = ssr;
section_size_ = ne;
num_sections_ <<= 1;
if (capacity_ < 2 * get_nom_capacity()) grow(2 * get_nom_capacity());
s.size_raw = ssr;
s.size = ne;
s.num <<= 1;
return true;
}
return false;
Expand Down Expand Up @@ -408,7 +483,7 @@ void req_compactor<T, C, A>::serialize(std::ostream& os, const S& serde) const {
const uint16_t padding = 0;
write(os, padding);
write(os, num_items_);
serde.serialize(os, begin(), num_items_);
if (num_items_ > 0) serde.serialize(os, begin(), num_items_); // an empty compactor may have no storage

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.

Symmetric case on the read side: deserialize_items (L518, L575) still calls alloc.allocate(0) and serde.deserialize(..., 0) when num == 0, leaving items_ != nullptr while capacity_ == 0 (and passing (nullptr, 0) to memcpy if an allocator returns nullptr for zero-sized allocations).

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Fixed in a73df7c: an empty compactor now gets no storage on deserialize, and the serde is not called for it. The test deserializes an empty compactor with an allocator that throws on any allocation.

}

template<typename T, typename C, typename A>
Expand All @@ -423,7 +498,7 @@ size_t req_compactor<T, C, A>::serialize(void* dst, size_t capacity, const S& se
const uint16_t padding = 0;
ptr += copy_to_mem(padding, ptr);
ptr += copy_to_mem(num_items_, ptr);
ptr += serde.serialize(ptr, end_ptr - ptr, begin(), num_items_);
if (num_items_ > 0) ptr += serde.serialize(ptr, end_ptr - ptr, begin(), num_items_); // an empty compactor may have no storage
return ptr - static_cast<uint8_t*>(dst);
}

Expand Down Expand Up @@ -458,8 +533,9 @@ template<typename S>
auto req_compactor<T, C, A>::deserialize_items(std::istream& is, const S& serde, const A& allocator, uint32_t num)
-> std::unique_ptr<T, items_deleter> {
A alloc(allocator);
std::unique_ptr<T, items_deleter> items(alloc.allocate(num), items_deleter(allocator, false, num));
serde.deserialize(is, items.get(), num);
// an empty compactor has no storage
std::unique_ptr<T, items_deleter> items(num > 0 ? alloc.allocate(num) : nullptr, items_deleter(allocator, false, num));
if (num > 0) serde.deserialize(is, items.get(), num);
// serde did not throw, enable destructors
items.get_deleter().set_destroy(true);
if (!is.good()) throw std::runtime_error("error reading from std::istream");
Expand Down Expand Up @@ -515,8 +591,9 @@ auto req_compactor<T, C, A>::deserialize_items(const void* bytes, size_t size, c
const char* ptr = static_cast<const char*>(bytes);
const char* end_ptr = static_cast<const char*>(bytes) + size;
A alloc(allocator);
std::unique_ptr<T, items_deleter> items(alloc.allocate(num), items_deleter(allocator, false, num));
ptr += serde.deserialize(ptr, end_ptr - ptr, items.get(), num);
// an empty compactor has no storage
std::unique_ptr<T, items_deleter> items(num > 0 ? alloc.allocate(num) : nullptr, items_deleter(allocator, false, num));
if (num > 0) ptr += serde.deserialize(ptr, end_ptr - ptr, items.get(), num);
// serde did not throw, enable destructors
items.get_deleter().set_destroy(true);
return std::pair<std::unique_ptr<T, items_deleter>, size_t>(
Expand Down
Loading
Loading