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
1 change: 1 addition & 0 deletions common/include/optional.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ class optional {

template<typename... Args>
void emplace(Args&&... args) noexcept(std::is_nothrow_constructible<T, Args...>::value) {
reset(); // destroys the current value, if any, as std::optional::emplace does
new (&value_) T(args...);
initialized_ = true;
}
Expand Down
25 changes: 25 additions & 0 deletions common/test/optional_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,29 @@ TEST_CASE("optional conversion", "[common]") {
REQUIRE(*opt_d == static_cast<double>(*opt_f));
}

// counts live instances
struct counted {
static int live;
int val;
counted(int v): val(v) { ++live; }
counted(const counted& other): val(other.val) { ++live; }
~counted() { --live; }
};
int counted::live = 0;

TEST_CASE("optional emplace replaces the current value", "[common]") {
{
optional<counted> opt;
opt.emplace(1);
REQUIRE(counted::live == 1);
opt.emplace(2); // destroys the first value
REQUIRE(counted::live == 1);
REQUIRE(opt->val == 2);
opt.reset();
REQUIRE(counted::live == 0);
opt.emplace(3);
}
REQUIRE(counted::live == 0);
}

} /* namespace datasketches */
7 changes: 5 additions & 2 deletions req/include/req_compactor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ class req_compactor {

uint64_t compute_weight(const T& item, bool inclusive) const;

// allocates the initial storage for items, unless the compactor has storage already
void reserve();

template<typename FwdT>
void append(FwdT&& item);

Expand Down Expand Up @@ -113,8 +116,8 @@ class req_compactor {
uint8_t num_sections_;
uint64_t state_; // state of the deterministic compaction schedule
uint32_t num_items_;
uint32_t capacity_;
T* items_;
uint32_t capacity_; // 0 while items_ is nullptr
T* items_; // nullptr until first use, see reserve()

bool ensure_enough_sections();
std::pair<uint32_t, uint32_t> compute_compaction_range(uint32_t secs_to_compact) const;
Expand Down
16 changes: 13 additions & 3 deletions req/include/req_compactor_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ section_size_(section_size),
num_sections_(req_constants::INIT_NUM_SECTIONS),
state_(0),
num_items_(0),
capacity_(2 * get_nom_capacity()),
items_(allocator_.allocate(capacity_))
capacity_(0),
items_(nullptr) // allocated on first use, see reserve()
{}

template<typename T, typename C, typename A>
Expand Down Expand Up @@ -97,6 +97,8 @@ capacity_(other.capacity_),
items_(other.items_)
{
other.items_ = nullptr;
other.capacity_ = 0;
other.num_items_ = 0;
}

template<typename T, typename C, typename A>
Expand Down Expand Up @@ -193,9 +195,15 @@ uint64_t req_compactor<T, C, A>::compute_weight(const T& item, bool inclusive) c
return std::distance(begin(), it) << lg_weight_;
}

template<typename T, typename C, typename A>
void req_compactor<T, C, A>::reserve() {
if (items_ == nullptr) grow(2 * get_nom_capacity());
}

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));
Expand All @@ -211,13 +219,15 @@ void req_compactor<T, C, A>::grow(uint32_t new_capacity) {
new (new_items + new_i) T(std::move(*it));
(*it).~T();
}
allocator_.deallocate(items_, capacity_);
if (items_ != nullptr) allocator_.deallocate(items_, capacity_);
items_ = new_items;
capacity_ = new_capacity;
}

template<typename T, typename C, typename A>
void req_compactor<T, C, A>::ensure_space(uint32_t num) {
if (num == 0) return; // nothing to add, so an empty compactor stays unallocated
reserve();
Comment thread
proost marked this conversation as resolved.
Resolved
if (num_items_ + num > capacity_) grow(num_items_ + num + get_nom_capacity());
}

Expand Down
28 changes: 28 additions & 0 deletions req/include/req_sketch.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,13 @@ namespace datasketches {
* two levels of detail output. This is not only useful for debugging, but is a powerful tool to
* help users understand how the sketch works.</li>
* </ul>
*
* <p>Memory allocation: a new sketch does not allocate storage for items until it first needs it,
* on the first update or the first merge of a non-empty sketch. Sketches that stay empty use
* little memory. As a result, the first update or merge can throw std::bad_alloc while allocating
* this storage, rather than the constructor. If the first update fails this way, the sketch is
* unchanged and can still be used. Call reserve() right after construction to allocate the storage
* up front.</p>
*/
template<
typename T,
Expand Down Expand Up @@ -106,6 +113,7 @@ class req_sketch {
* @param comparator strict weak ordering function (see C++ named requirements: Compare)
* @param allocator used by this sketch to allocate memory
* @throws std::invalid_argument if k is greater than 1024
* Storage for items is allocated on first use, see reserve().
*/
explicit req_sketch(uint16_t k, bool hra = true, const Comparator& comparator = Comparator(),
const Allocator& allocator = Allocator());
Expand Down Expand Up @@ -188,6 +196,7 @@ class req_sketch {
* Updates this sketch with the given data item.
* 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
*/
template<typename FwdT>
Expand All @@ -197,11 +206,22 @@ class req_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.
* The first merge of a non-empty sketch allocates storage for items,
* unless reserve() or an update already did.
* @param other sketch to merge into this one
*/
template<typename FwdSk>
void merge(FwdSk&& other);

/**
* Allocates storage for items now, rather than on the first update or merge.
* 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.
* @throw std::bad_alloc if the allocation fails, in which case the sketch is unchanged
*/
void reserve();

/**
* Returns the min item of the stream.
* If the sketch is empty this throws std::runtime_error.
Expand Down Expand Up @@ -454,7 +474,13 @@ class req_sketch {
void grow();
void update_max_nom_size();
void update_num_retained();
// recomputes n, the number of retained items and the nominal size from the compactors,
// after an allocation failed partway through an update or a merge
void restore_counts();
void compress();
void compress_levels();
// removes the levels above the given number that hold no items, after a failed merge or compression
void remove_empty_levels_above(size_t num_levels);

static double get_rank_lb(uint16_t k, uint8_t num_levels, double rank, uint8_t num_std_dev, uint64_t n, bool hra);
static double get_rank_ub(uint16_t k, uint8_t num_levels, double rank, uint8_t num_std_dev, uint64_t n, bool hra);
Expand Down Expand Up @@ -507,6 +533,8 @@ class req_sketch<T, C, A>::const_iterator {
const T* compactor_it_;
friend class req_sketch<T, C, A>;
const_iterator(LevelsIterator begin, LevelsIterator end);
// moves to the first item at or above the current level, skipping levels without items
void skip_empty_levels();
};

} /* namespace datasketches */
Expand Down
81 changes: 64 additions & 17 deletions req/include/req_sketch_impl.hpp
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ template<typename T, typename C, typename A>
template<typename FwdT>
void req_sketch<T, C, A>::update(FwdT&& item) {
if (!check_update_item(item)) { return; }
compactors_[0].reserve(); // first, so that a failure leaves the sketch unchanged
if (is_empty()) {
min_item_.emplace(item);
max_item_.emplace(item);
Expand All @@ -183,33 +184,45 @@ void req_sketch<T, C, A>::update(FwdT&& item) {
compactors_[0].append(std::forward<FwdT>(item));
++num_retained_;
++n_;
if (num_retained_ == max_nom_size_) compress();
reset_sorted_view();
if (num_retained_ >= max_nom_size_) compress();
}

template<typename T, typename C, typename A>
template<typename FwdSk>
void req_sketch<T, C, A>::merge(FwdSk&& other) {
if (is_HRA() != other.is_HRA()) throw std::invalid_argument("merging HRA and LRA is not valid");
if (other.is_empty()) return;
if (is_empty()) {
min_item_.emplace(conditional_forward<FwdSk>(*other.min_item_));
max_item_.emplace(conditional_forward<FwdSk>(*other.max_item_));
} else {
if (comparator_(*other.min_item_, *min_item_)) *min_item_ = conditional_forward<FwdSk>(*other.min_item_);
if (comparator_(*max_item_, *other.max_item_)) *max_item_ = conditional_forward<FwdSk>(*other.max_item_);
}
// grow until this has at least as many compactors as other
while (get_num_levels() < other.get_num_levels()) grow();
// merge the items in all height compactors
for (size_t i = 0; i < other.get_num_levels(); ++i) {
compactors_[i].merge(conditional_forward<FwdSk>(other.compactors_[i]));
reset_sorted_view();
const size_t num_levels = compactors_.size();
try {
// grow until this has at least as many compactors as other
while (get_num_levels() < other.get_num_levels()) grow();
if (is_empty()) {
min_item_.emplace(conditional_forward<FwdSk>(*other.min_item_));
max_item_.emplace(conditional_forward<FwdSk>(*other.max_item_));
} else {
if (comparator_(*other.min_item_, *min_item_)) *min_item_ = conditional_forward<FwdSk>(*other.min_item_);
if (comparator_(*max_item_, *other.max_item_)) *max_item_ = conditional_forward<FwdSk>(*other.max_item_);
}
// merge the items in all height compactors
for (size_t i = 0; i < other.get_num_levels(); ++i) {
compactors_[i].merge(conditional_forward<FwdSk>(other.compactors_[i]));
}
} catch (...) {
remove_empty_levels_above(num_levels);
restore_counts(); // some levels may be merged already

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.

In here, we should remove last compactor. When grow is success and merge failed, then sketch's state is invalid.

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.

You're right, thanks. If grow() succeeded and a compactor merge then failed, the added levels stayed behind empty. The sketch then reported estimation mode with nothing above level 0, and its rank bounds used too many levels. For example, an empty target merging a 1,000-item source and failing at its second allocation was left with n = 0 but in estimation mode.

Fixed in ccbcf6a: on failure, merge() now removes the levels it added that received no items. It also grows before it widens min and max, so a failure to grow leaves the sketch unchanged. The test "failure at each allocation of a merge" now checks that a failed merge which merged no items leaves the target's estimation mode as it was; it fails without the change.

throw;
}
n_ += other.n_;
update_max_nom_size();
update_num_retained();
if (num_retained_ >= max_nom_size_) compress();
reset_sorted_view();
}

template<typename T, typename C, typename A>
void req_sketch<T, C, A>::reserve() {
compactors_[0].reserve();
}

template<typename T, typename C, typename A>
Expand Down Expand Up @@ -631,8 +644,34 @@ void req_sketch<T, C, A>::update_num_retained() {
for (const auto& compactor: compactors_) num_retained_ += compactor.get_num_items();
}

template<typename T, typename C, typename A>
void req_sketch<T, C, A>::restore_counts() {
// the total weight of the retained items is always n
n_ = 0;
for (const auto& compactor: compactors_) n_ += static_cast<uint64_t>(compactor.get_num_items()) << compactor.get_lg_weight();
update_num_retained();
update_max_nom_size();
}

template<typename T, typename C, typename A>
void req_sketch<T, C, A>::compress() {
const size_t num_levels = compactors_.size();
try {
compress_levels();
} catch (...) {
remove_empty_levels_above(num_levels); // a level can be added, and then the compaction into it fail
restore_counts(); // a compactor can fail to grow after a compaction moved its items

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.

In here we should also reset compactors. Because compress_levels call grow

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.

Right, same problem as in merge(): compress_levels() can add a level and then fail to compact into it, when the new level fails to allocate space for the promoted items. That left the sketch in estimation mode with an empty top level.

Fixed in 1267c75: on failure compress() now removes the levels it added that hold no items, the same way merge() does. Both use a new helper, remove_empty_levels_above(). The allocation tests now also check that a sketch in estimation mode retains an item with weight above 1. "failure at each allocation of the updates" fails that check without the change.

throw;
}
}

template<typename T, typename C, typename A>
void req_sketch<T, C, A>::remove_empty_levels_above(size_t num_levels) {
while (compactors_.size() > num_levels && compactors_.back().get_num_items() == 0) compactors_.pop_back();
}

template<typename T, typename C, typename A>
void req_sketch<T, C, A>::compress_levels() {
for (size_t h = 0; h < compactors_.size(); ++h) {
if (compactors_[h].get_num_items() >= compactors_[h].get_nom_capacity()) {
if (h == 0) compactors_[0].sort();
Expand Down Expand Up @@ -791,15 +830,23 @@ template<typename T, typename C, typename A>
req_sketch<T, C, A>::const_iterator::const_iterator(LevelsIterator begin, LevelsIterator end):
levels_it_(begin),
levels_end_(end),
compactor_it_(begin == end ? nullptr : (*levels_it_).begin())
{}
compactor_it_(nullptr)
{
skip_empty_levels();
}

template<typename T, typename C, typename A>
void req_sketch<T, C, A>::const_iterator::skip_empty_levels() {
while (levels_it_ != levels_end_ && (*levels_it_).get_num_items() == 0) ++levels_it_;
if (levels_it_ != levels_end_) compactor_it_ = (*levels_it_).begin();
}

template<typename T, typename C, typename A>
auto req_sketch<T, C, A>::const_iterator::operator++() -> const_iterator& {
++compactor_it_;
if (compactor_it_ == (*levels_it_).end()) {
++levels_it_;
if (levels_it_ != levels_end_) compactor_it_ = (*levels_it_).begin();
skip_empty_levels();
}
return *this;
}
Expand Down
1 change: 1 addition & 0 deletions req/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ target_sources(req_test
PRIVATE
req_sketch_test.cpp
req_sketch_custom_type_test.cpp
req_sketch_allocation_test.cpp
)

if (SERDE_COMPAT)
Expand Down
Loading
Loading