You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@datasketches.apache.org by jm...@apache.org on 2020/04/29 02:23:58 UTC

[incubator-datasketches-cpp] 07/07: revert sampling changes unrelated to memory limits, add check sin kll, remove union serialization from python hll

This is an automated email from the ASF dual-hosted git repository.

jmalkin pushed a commit to branch check_buffer_bounds
in repository https://gitbox.apache.org/repos/asf/incubator-datasketches-cpp.git

commit c686e997094b0ac01bb24e03b62156315d94f5c9
Author: Jon Malkin <jm...@users.noreply.github.com>
AuthorDate: Tue Apr 28 19:23:26 2020 -0700

    revert sampling changes unrelated to memory limits, add check sin kll, remove union serialization from python hll
---
 kll/include/kll_sketch_impl.hpp          |  19 ++++--
 python/src/hll_wrapper.cpp               |  18 ------
 python/tests/hll_test.py                 |   9 ---
 sampling/include/var_opt_sketch.hpp      |  26 ++++----
 sampling/include/var_opt_sketch_impl.hpp | 106 +++++++++++++++----------------
 sampling/include/var_opt_union_impl.hpp  |   7 +-
 6 files changed, 79 insertions(+), 106 deletions(-)

diff --git a/kll/include/kll_sketch_impl.hpp b/kll/include/kll_sketch_impl.hpp
index 6a74fa0..822a4a6 100644
--- a/kll/include/kll_sketch_impl.hpp
+++ b/kll/include/kll_sketch_impl.hpp
@@ -23,6 +23,7 @@
 #include <iostream>
 #include <iomanip>
 
+#include "memory_operations.hpp"
 #include "kll_helper.hpp"
 
 namespace datasketches {
@@ -417,6 +418,7 @@ vector_u8<A> kll_sketch<T, C, S, A>::serialize(unsigned header_size_bytes) const
   const size_t size = header_size_bytes + get_serialized_size_bytes();
   vector_u8<A> bytes(size);
   uint8_t* ptr = bytes.data() + header_size_bytes;
+  uint8_t* end_ptr = ptr + size;
   const uint8_t preamble_ints(is_empty() || is_single_item ? PREAMBLE_INTS_SHORT : PREAMBLE_INTS_FULL);
   ptr += copy_to_mem(&preamble_ints, ptr, sizeof(preamble_ints));
   const uint8_t serial_version(is_single_item ? SERIAL_VERSION_2 : SERIAL_VERSION_1);
@@ -440,10 +442,11 @@ vector_u8<A> kll_sketch<T, C, S, A>::serialize(unsigned header_size_bytes) const
       ptr += copy_to_mem(&num_levels_, ptr, sizeof(num_levels_));
       ptr += copy_to_mem(&unused, ptr, sizeof(unused));
       ptr += copy_to_mem(levels_, ptr, sizeof(levels_[0]) * num_levels_);
-      ptr += S().serialize(ptr, min_value_, 1);
-      ptr += S().serialize(ptr, max_value_, 1);
+      ptr += S().serialize(ptr, end_ptr - ptr, min_value_, 1);
+      ptr += S().serialize(ptr, end_ptr - ptr, max_value_, 1);
     }
-    ptr += S().serialize(ptr, &items_[levels_[0]], get_num_retained());
+    const size_t bytes_remaining = end_ptr - ptr;
+    ptr += S().serialize(ptr, bytes_remaining, &items_[levels_[0]], get_num_retained());
   }
   const size_t delta = ptr - bytes.data();
   if (delta != size) throw std::logic_error("serialized size mismatch: " + std::to_string(delta) + " != " + std::to_string(size));
@@ -478,6 +481,7 @@ kll_sketch<T, C, S, A> kll_sketch<T, C, S, A>::deserialize(std::istream& is) {
 
 template<typename T, typename C, typename S, typename A>
 kll_sketch<T, C, S, A> kll_sketch<T, C, S, A>::deserialize(const void* bytes, size_t size) {
+  ensure_minimum_memory(size, 8);
   const char* ptr = static_cast<const char*>(bytes);
   uint8_t preamble_ints;
   ptr += copy_from_mem(ptr, &preamble_ints, sizeof(preamble_ints));
@@ -496,6 +500,7 @@ kll_sketch<T, C, S, A> kll_sketch<T, C, S, A>::deserialize(const void* bytes, si
   check_preamble_ints(preamble_ints, flags_byte);
   check_serial_version(serial_version);
   check_family_id(family_id);
+  ensure_minimum_memory(size, 1 << preamble_ints);
 
   const bool is_empty(flags_byte & (1 << flags::IS_EMPTY));
   return is_empty ? kll_sketch<T, C, S, A>(k) : kll_sketch<T, C, S, A>(k, flags_byte, bytes, size);
@@ -562,12 +567,14 @@ kll_sketch<T, C, S, A>::kll_sketch(uint16_t k, uint8_t flags_byte, std::istream&
 
 // for deserialization
 // the common part of the preamble was read and compatibility checks were done
+// we also assume we have already checked that the preamble information fits within the buffer
 template<typename T, typename C, typename S, typename A>
 kll_sketch<T, C, S, A>::kll_sketch(uint16_t k, uint8_t flags_byte, const void* bytes, size_t size) {
   k_ = k;
   m_ = DEFAULT_M;
   const bool is_single_item(flags_byte & (1 << flags::IS_SINGLE_ITEM)); // used in serial version 2
   const char* ptr = static_cast<const char*>(bytes) + DATA_START_SINGLE_ITEM;
+  const char* end_ptr = static_cast<const char*>(bytes) + size;
   if (is_single_item) {
     n_ = 1;
     min_k_ = k_;
@@ -591,13 +598,13 @@ kll_sketch<T, C, S, A>::kll_sketch(uint16_t k, uint8_t flags_byte, const void* b
   min_value_ = A().allocate(1);
   max_value_ = A().allocate(1);
   if (!is_single_item) {
-    ptr += S().deserialize(ptr, min_value_, 1);
-    ptr += S().deserialize(ptr, max_value_, 1);
+    ptr += S().deserialize(ptr, end_ptr - ptr, min_value_, 1);
+    ptr += S().deserialize(ptr, end_ptr - ptr, max_value_, 1);
   }
   items_ = A().allocate(capacity);
   items_size_ = capacity;
   const auto num_items(levels_[num_levels_] - levels_[0]);
-  ptr += S().deserialize(ptr, &items_[levels_[0]], num_items);
+  ptr += S().deserialize(ptr, end_ptr - ptr, &items_[levels_[0]], num_items);
   if (is_single_item) {
     new (min_value_) T(items_[levels_[0]]);
     new (max_value_) T(items_[levels_[0]]);
diff --git a/python/src/hll_wrapper.cpp b/python/src/hll_wrapper.cpp
index cd57d15..c3fdaf9 100644
--- a/python/src/hll_wrapper.cpp
+++ b/python/src/hll_wrapper.cpp
@@ -41,21 +41,6 @@ py::object hll_sketch_serialize_updatable(const hll_sketch& sk) {
   return py::bytes((char*)serResult.data(), serResult.size());
 }
 
-hll_union hll_union_deserialize(py::bytes uBytes) {
-  std::string uStr = uBytes; // implicit cast
-  return hll_union::deserialize(uStr.c_str(), uStr.length());
-}
-
-py::object hll_union_serialize_compact(const hll_union& u) {
-  auto serResult = u.serialize_compact();
-  return py::bytes((char*)serResult.data(), serResult.size());
-}
-
-py::object hll_union_serialize_updatable(const hll_union& u) {
-  auto serResult = u.serialize_updatable();
-  return py::bytes((char*)serResult.data(), serResult.size());
-}
-
 }
 }
 
@@ -102,9 +87,6 @@ void init_hll(py::module &m) {
 
   py::class_<hll_union>(m, "hll_union")
     .def(py::init<int>(), py::arg("lg_max_k"))
-    .def_static("deserialize", &dspy::hll_union_deserialize)
-    .def("serialize_compact", &dspy::hll_union_serialize_compact)
-    .def("serialize_updatable", &dspy::hll_union_serialize_updatable)
     .def("to_string", (std::string (hll_union::*)(bool,bool,bool,bool) const) &hll_union::to_string,
          py::arg("summary")=true, py::arg("detail")=false, py::arg("aux_detail")=false, py::arg("all")=false)
     .def("__str__", (std::string (hll_union::*)(bool,bool,bool,bool) const) &hll_union::to_string,
diff --git a/python/tests/hll_test.py b/python/tests/hll_test.py
index 9403362..4d7aa75 100644
--- a/python/tests/hll_test.py
+++ b/python/tests/hll_test.py
@@ -119,15 +119,6 @@ class HllTest(unittest.TestCase):
         sk = union.get_result()
         self.assertTrue(isinstance(sk, hll_sketch))
         self.assertEqual(sk.tgt_type, tgt_hll_type.HLL_4)
-
-        bytes_compact = union.serialize_compact()
-        bytes_update = union.serialize_updatable()
-        self.assertEqual(len(bytes_compact), union.get_compact_serialization_bytes())
-        self.assertEqual(len(bytes_update), union.get_updatable_serialization_bytes())
-
-        self.assertTrue(isinstance(hll_union.deserialize(bytes_compact), hll_union))
-        self.assertTrue(isinstance(hll_union.deserialize(bytes_update), hll_union))
-
         
     def generate_sketch(self, n, k, sk_type=tgt_hll_type.HLL_4, st_idx=0):
         sk = hll_sketch(k, sk_type)
diff --git a/sampling/include/var_opt_sketch.hpp b/sampling/include/var_opt_sketch.hpp
index 5edfc92..9fc8dbd 100644
--- a/sampling/include/var_opt_sketch.hpp
+++ b/sampling/include/var_opt_sketch.hpp
@@ -144,7 +144,7 @@ class var_opt_sketch {
     // purpose is storing the state of the unioning algorithm) but for reasons of programming
     // convenience we are currently declaring them here. However, that could change in the future.
 
-    // Following value is:
+    // Following int is:
     //  1. Zero (for a varopt sketch)
     //  2. Count of marked items in H region, if part of a unioning algo's gadget
     uint32_t num_marks_in_h_;
@@ -172,24 +172,24 @@ class var_opt_sketch {
 
     inline double get_tau() const;
     inline double peek_min() const;
-    inline bool is_marked(uint32_t idx) const;
+    inline bool is_marked(int idx) const;
     
     inline uint32_t pick_random_slot_in_r() const;
-    inline uint32_t choose_delete_slot(double wt_cand, uint32_t num_cand) const;
-    inline uint32_t choose_weighted_delete_slot(double wt_cand, uint32_t num_cand) const;
+    inline uint32_t choose_delete_slot(double wt_cand, int num_cand) const;
+    inline uint32_t choose_weighted_delete_slot(double wt_cand, int num_cand) const;
 
     inline void transition_from_warmup();
     inline void convert_to_heap();
-    inline void restore_towards_leaves(uint32_t slot_in);
-    inline void restore_towards_root(uint32_t slot_in);
+    inline void restore_towards_leaves(int slot_in);
+    inline void restore_towards_root(int slot_in);
     inline void push(const T& item, double wt, bool mark);
     inline void pop_min_to_m_region();
-    void grow_candidate_set(double wt_cands, uint32_t num_cands);    
+    void grow_candidate_set(double wt_cands, int num_cands);    
     void decrease_k_by_1();
     void strip_marks();
-    void force_set_k(uint32_t k); // used to resolve union gadget into sketch
-    void downsample_candidate_set(double wt_cands, uint32_t num_cands);
-    inline void swap_values(uint32_t src, uint32_t dst);
+    void force_set_k(int k); // used to resolve union gadget into sketch
+    void downsample_candidate_set(double wt_cands, int num_cands);
+    inline void swap_values(int src, int dst);
     void grow_data_arrays();
     void allocate_data_arrays(uint32_t tgt_size, bool use_marks);
 
@@ -197,11 +197,11 @@ class var_opt_sketch {
     static void check_preamble_longs(uint8_t preamble_longs, uint8_t flags);
     static void check_family_and_serialization_version(uint8_t family_id, uint8_t ser_ver);
     // next method sets current_items_alloc_
-    void validate_and_set_current_size(uint32_t preamble_longs);
+    void validate_and_set_current_size(int preamble_longs);
 
     // things to move to common and be shared among sketches
-    static uint32_t get_adjusted_size(uint32_t max_size, uint32_t resize_target);
-    static uint32_t starting_sub_multiple(uint32_t lg_target, uint32_t lg_rf, uint32_t lg_min);
+    static uint32_t get_adjusted_size(int max_size, int resize_target);
+    static uint32_t starting_sub_multiple(int lg_target, int lg_rf, int lg_min);
     static inline double pseudo_hypergeometric_ub_on_p(uint64_t n, uint32_t k, double sampling_rate);
     static inline double pseudo_hypergeometric_lb_on_p(uint64_t n, uint32_t k, double sampling_rate);
     static bool is_power_of_2(uint32_t v);
diff --git a/sampling/include/var_opt_sketch_impl.hpp b/sampling/include/var_opt_sketch_impl.hpp
index 4292d08..e2438e2 100644
--- a/sampling/include/var_opt_sketch_impl.hpp
+++ b/sampling/include/var_opt_sketch_impl.hpp
@@ -29,9 +29,9 @@
 
 #include "var_opt_sketch.hpp"
 #include "serde.hpp"
-#include "memory_operations.hpp"
 #include "bounds_binomial_proportions.hpp"
 #include "count_zeros.hpp"
+#include "memory_operations.hpp"
 
 namespace datasketches {
 
@@ -170,7 +170,7 @@ var_opt_sketch<T,S,A>::var_opt_sketch(uint32_t k, resize_factor rf, bool is_gadg
   }
 
   uint32_t ceiling_lg_k = to_log_2(ceiling_power_of_2(k_));
-  uint32_t initial_lg_size = starting_sub_multiple(ceiling_lg_k, rf_, MIN_LG_ARR_ITEMS);
+  int initial_lg_size = starting_sub_multiple(ceiling_lg_k, rf_, MIN_LG_ARR_ITEMS);
   curr_items_alloc_ = get_adjusted_size(k_, 1 << initial_lg_size);
   if (curr_items_alloc_ == k_) { // if full size, need to leave 1 for the gap
     ++curr_items_alloc_;
@@ -306,11 +306,11 @@ var_opt_sketch<T,S,A>::var_opt_sketch(uint32_t k, resize_factor rf, bool is_gadg
 template<typename T, typename S, typename A>
 var_opt_sketch<T,S,A>::var_opt_sketch(uint32_t k, resize_factor rf, bool is_gadget, uint8_t preamble_longs,
                                       const void* bytes, size_t size) : k_(k), m_(0), rf_(rf) {
-  // private constructor so we assume not called if sketch is empty
-  // and already checked that the array can hold the preamble
+  // private constructor so we assume not called if sketch is empty,
+  // and that the array is large enough to hold the preamble
   const char* base = static_cast<const char*>(bytes);
-  const char* end_ptr = base + size;
   const char* ptr = static_cast<const char*>(bytes) + sizeof(uint64_t);
+  const char* end_ptr = static_cast<const char*>(bytes) + size;
 
   // second and third prelongs
   ptr += copy_from_mem(ptr, &n_, sizeof(n_));
@@ -350,7 +350,7 @@ var_opt_sketch<T,S,A>::var_opt_sketch(uint32_t k, resize_factor rf, bool is_gadg
   num_marks_in_h_ = 0;
   if (is_gadget) {
     uint8_t val = 0;
-    const size_t size_marks = h_ / 8 + (h_ % 8 > 0 ? 1 : 0);
+    const size_t size_marks = (h_ / 8) + (h_ % 8 > 0 ? 1 : 0);
     check_memory_size(ptr - base + size_marks, size);
     for (uint32_t i = 0; i < h_; ++i) {
      if ((i & 0x7) == 0x0) { // should trigger on first iteration
@@ -362,10 +362,8 @@ var_opt_sketch<T,S,A>::var_opt_sketch(uint32_t k, resize_factor rf, bool is_gadg
   }
 
   // read the sample items, skipping the gap. Either h_ or r_ may be 0
-  size_t bytes_remaining = end_ptr - ptr;
-  ptr += S().deserialize(ptr, bytes_remaining, data_, h_);
-  bytes_remaining = end_ptr - ptr;
-  ptr += S().deserialize(ptr, bytes_remaining, &data_[h_ + 1], r_);
+  ptr += S().deserialize(ptr, end_ptr - ptr, data_, h_);
+  ptr += S().deserialize(ptr, end_ptr - ptr, &data_[h_ + 1], r_);
 }
 
 /*
@@ -498,13 +496,11 @@ std::vector<uint8_t, AllocU8<A>> var_opt_sketch<T,S,A>::serialize(unsigned heade
     }
 
     // write the sample items, skipping the gap. Either h_ or r_ may be 0
-    size_t bytes_remaining = end_ptr - ptr;
-    ptr += S().serialize(ptr, bytes_remaining, data_, h_);
-    bytes_remaining = end_ptr - ptr;
-    ptr += S().serialize(ptr, bytes_remaining, &data_[h_ + 1], r_);
+    ptr += S().serialize(ptr, end_ptr - ptr, data_, h_);
+    ptr += S().serialize(ptr, end_ptr - ptr, &data_[h_ + 1], r_);
   }
   
-  size_t bytes_written = ptr - bytes.data() + header_size_bytes;
+  size_t bytes_written = ptr - bytes.data();
   if (bytes_written != size) {
     throw std::logic_error("serialized size mismatch: " + std::to_string(bytes_written) + " != " + std::to_string(size));
   }
@@ -593,7 +589,7 @@ var_opt_sketch<T,S,A> var_opt_sketch<T,S,A>::deserialize(const void* bytes, size
 
   check_preamble_longs(preamble_longs, flags);
   check_family_and_serialization_version(family_id, serial_version);
-  ensure_minimum_memory(size, (size_t) (preamble_longs << 3));
+  ensure_minimum_memory(size, preamble_longs << 3);
 
   const bool is_empty = flags & EMPTY_FLAG_MASK;
   const bool is_gadget = flags & GADGET_FLAG_MASK;
@@ -634,7 +630,7 @@ template<typename T, typename S, typename A>
 void var_opt_sketch<T,S,A>::reset() {
   const uint32_t prev_alloc = curr_items_alloc_;
   const uint32_t ceiling_lg_k = to_log_2(ceiling_power_of_2(k_));
-  const uint32_t initial_lg_size = starting_sub_multiple(ceiling_lg_k, rf_, MIN_LG_ARR_ITEMS);
+  const int initial_lg_size = starting_sub_multiple(ceiling_lg_k, rf_, MIN_LG_ARR_ITEMS);
   curr_items_alloc_ = get_adjusted_size(k_, 1 << initial_lg_size);
   if (curr_items_alloc_ == k_) { // if full size, need to leave 1 for the gap
     ++curr_items_alloc_;
@@ -803,7 +799,7 @@ void var_opt_sketch<T,S,A>::update_warmup_phase(const T& item, double weight, bo
 
   // check if need to heapify
   if (h_ > k_) {
-    if (h_ == curr_items_alloc_) { filled_data_ = true; }
+    filled_data_ = true;
     transition_from_warmup();
   }
 }
@@ -817,7 +813,7 @@ void var_opt_sketch<T,S,A>::update_light(const T& item, double weight, bool mark
   assert(r_ >= 1);
   assert((r_ + h_) == k_);
 
-  const uint32_t m_slot = h_; // index of the gap, which becomes the M region
+  const int m_slot = h_; // index of the gap, which becomes the M region
   if (filled_data_) {
     data_[m_slot] = item;
   } else {
@@ -865,7 +861,7 @@ void var_opt_sketch<T,S,A>::update_heavy_r_eq1(const T& item, double weight, boo
 
   // Any set of two items is downsample-able to one item,
   // so the two lightest items are a valid starting point for the following
-  const uint32_t m_slot = k_ - 1; // array is k+1, 1 in R, so slot before is M
+  const int m_slot = k_ - 1; // array is k+1, 1 in R, so slot before is M
   grow_candidate_set(weights_[m_slot] + total_wt_r_, 2);
 }
 
@@ -1014,31 +1010,31 @@ void var_opt_sketch<T,S,A>::convert_to_heap() {
     return; // nothing to do
   }
 
-  const uint32_t last_slot = h_ - 1;
-  const uint32_t last_non_leaf = ((last_slot + 1) / 2) - 1;
+  const int last_slot = h_ - 1;
+  const int last_non_leaf = ((last_slot + 1) / 2) - 1;
   
   for (int j = last_non_leaf; j >= 0; --j) {
-    restore_towards_leaves(static_cast<uint32_t>(j));
+    restore_towards_leaves(j);
   }
 
   // validates heap, used for initial debugging
-  //for (uint32_t j = h_ - 1; j > 0; --j) {
+  //for (int j = h_ - 1; j >= 1; --j) {
   //  int p = ((j + 1) / 2) - 1;
   //  assert(weights_[p] <= weights_[j]);
   //}
 }
 
 template<typename T, typename S, typename A>
-void var_opt_sketch<T,S,A>::restore_towards_leaves(uint32_t slot_in) {
+void var_opt_sketch<T,S,A>::restore_towards_leaves(int slot_in) {
   assert(h_ > 0);
-  const uint32_t last_slot = h_ - 1;
+  const int last_slot = h_ - 1;
   assert(slot_in <= last_slot);
 
-  uint32_t slot = slot_in;
-  uint32_t child = (2 * slot_in) + 1; // might be invalid, need to check
+  int slot = slot_in;
+  int child = (2 * slot_in) + 1; // might be invalid, need to check
 
   while (child <= last_slot) {
-    uint32_t child2 = child + 1; // might also be invalid
+    int child2 = child + 1; // might also be invalid
     if ((child2 <= last_slot) && (weights_[child2] < weights_[child])) {
       // siwtch to other child if it's both valid and smaller
       child = child2;
@@ -1058,9 +1054,9 @@ void var_opt_sketch<T,S,A>::restore_towards_leaves(uint32_t slot_in) {
 }
 
 template<typename T, typename S, typename A>
-void var_opt_sketch<T,S,A>::restore_towards_root(uint32_t slot_in) {
-  uint32_t slot = slot_in;
-  uint32_t p = (((slot + 1) / 2) - 1); // valid if slot >= 1
+void var_opt_sketch<T,S,A>::restore_towards_root(int slot_in) {
+  int slot = slot_in;
+  int p = (((slot + 1) / 2) - 1); // valid if slot >= 1
   while ((slot > 0) && (weights_[slot] < weights_[p])) {
     swap_values(slot, p);
     slot = p;
@@ -1097,7 +1093,7 @@ void var_opt_sketch<T,S,A>::pop_min_to_m_region() {
     --h_;
   } else {
     // main case
-    uint32_t tgt = h_ - 1; // last slot, will swap with root
+    int tgt = h_ - 1; // last slot, will swap with root
     swap_values(0, tgt);
     ++m_;
     --h_;
@@ -1112,7 +1108,7 @@ void var_opt_sketch<T,S,A>::pop_min_to_m_region() {
 
 
 template<typename T, typename S, typename A>
-void var_opt_sketch<T,S,A>::swap_values(uint32_t src, uint32_t dst) {
+void var_opt_sketch<T,S,A>::swap_values(int src, int dst) {
   std::swap(data_[src], data_[dst]);
   std::swap(weights_[src], weights_[dst]);
 
@@ -1130,7 +1126,7 @@ void var_opt_sketch<T,S,A>::swap_values(uint32_t src, uint32_t dst) {
    by pulling sufficiently light items from h to m.
 */
 template<typename T, typename S, typename A>
-void var_opt_sketch<T,S,A>::grow_candidate_set(double wt_cands, uint32_t num_cands) {
+void var_opt_sketch<T,S,A>::grow_candidate_set(double wt_cands, int num_cands) {
   assert(h_ + m_ + r_ == k_ + 1);
   assert(num_cands >= 2);
   assert(num_cands == m_ + r_);
@@ -1156,21 +1152,21 @@ void var_opt_sketch<T,S,A>::grow_candidate_set(double wt_cands, uint32_t num_can
 }
 
 template<typename T, typename S, typename A>
-void var_opt_sketch<T,S,A>::downsample_candidate_set(double wt_cands, uint32_t num_cands) {
+void var_opt_sketch<T,S,A>::downsample_candidate_set(double wt_cands, int num_cands) {
   assert(num_cands >= 2);
   assert(h_ + num_cands == k_ + 1);
 
   // need this before overwriting anything
-  const uint32_t delete_slot = choose_delete_slot(wt_cands, num_cands);
-  const uint32_t leftmost_cand_slot = h_;
+  const int delete_slot = choose_delete_slot(wt_cands, num_cands);
+  const int leftmost_cand_slot = h_;
   assert(delete_slot >= leftmost_cand_slot);
   assert(delete_slot <= k_);
 
   // Overwrite weights for items from M moving into R,
   // to make bugs more obvious. Also needed so anyone reading the
   // weight knows if it's invalid without checking h_ and m_
-  const uint32_t stop_idx = leftmost_cand_slot + m_;
-  for (uint32_t j = leftmost_cand_slot; j < stop_idx; ++j) {
+  const int stop_idx = leftmost_cand_slot + m_;
+  for (int j = leftmost_cand_slot; j < stop_idx; ++j) {
     weights_[j] = -1.0;
   }
 
@@ -1184,7 +1180,7 @@ void var_opt_sketch<T,S,A>::downsample_candidate_set(double wt_cands, uint32_t n
 }
 
 template<typename T, typename S, typename A>
-uint32_t var_opt_sketch<T,S,A>::choose_delete_slot(double wt_cands, uint32_t num_cands) const {
+uint32_t var_opt_sketch<T,S,A>::choose_delete_slot(double wt_cands, int num_cands) const {
   assert(r_ > 0);
 
   if (m_ == 0) {
@@ -1201,8 +1197,8 @@ uint32_t var_opt_sketch<T,S,A>::choose_delete_slot(double wt_cands, uint32_t num
     }
   } else {
     // general case
-    const uint32_t delete_slot = choose_weighted_delete_slot(wt_cands, num_cands);
-    const uint32_t first_r_slot = h_ + m_;
+    const int delete_slot = choose_weighted_delete_slot(wt_cands, num_cands);
+    const int first_r_slot = h_ + m_;
     if (delete_slot == first_r_slot) {
       return pick_random_slot_in_r();
     } else {
@@ -1212,17 +1208,17 @@ uint32_t var_opt_sketch<T,S,A>::choose_delete_slot(double wt_cands, uint32_t num
 }
 
 template<typename T, typename S, typename A>
-uint32_t var_opt_sketch<T,S,A>::choose_weighted_delete_slot(double wt_cands, uint32_t num_cands) const {
+uint32_t var_opt_sketch<T,S,A>::choose_weighted_delete_slot(double wt_cands, int num_cands) const {
   assert(m_ >= 1);
 
-  const uint32_t offset = h_;
-  const uint32_t final_m = (offset + m_) - 1;
-  const uint32_t num_to_keep = num_cands - 1;
+  const int offset = h_;
+  const int final_m = (offset + m_) - 1;
+  const int num_to_keep = num_cands - 1;
 
   double left_subtotal = 0.0;
   double right_subtotal = -1.0 * wt_cands * next_double_exclude_zero();
 
-  for (uint32_t i = offset; i <= final_m; ++i) {
+  for (int i = offset; i <= final_m; ++i) {
     left_subtotal += num_to_keep * weights_[i];
     right_subtotal += wt_cands;
 
@@ -1238,7 +1234,7 @@ uint32_t var_opt_sketch<T,S,A>::choose_weighted_delete_slot(double wt_cands, uin
 template<typename T, typename S, typename A>
 uint32_t var_opt_sketch<T,S,A>::pick_random_slot_in_r() const {
   assert(r_ > 0);
-  const uint32_t offset = h_ + m_;
+  const int offset = h_ + m_;
   if (r_ == 1) {
     return offset;
   } else {
@@ -1253,7 +1249,7 @@ double var_opt_sketch<T,S,A>::peek_min() const {
 }
 
 template<typename T, typename S, typename A>
-inline bool var_opt_sketch<T,S,A>::is_marked(uint32_t idx) const {
+inline bool var_opt_sketch<T,S,A>::is_marked(int idx) const {
   return marks_ == nullptr ? false : marks_[idx];
 }
 
@@ -1307,7 +1303,7 @@ void var_opt_sketch<T,S,A>::check_family_and_serialization_version(uint8_t famil
 }
 
 template<typename T, typename S, typename A>
-void var_opt_sketch<T, S, A>::validate_and_set_current_size(uint32_t preamble_longs) {
+void var_opt_sketch<T, S, A>::validate_and_set_current_size(int preamble_longs) {
   if (k_ == 0 || k_ > MAX_K) {
     throw std::invalid_argument("k must be at least 1 and less than 2^31 - 1");
   }
@@ -1328,7 +1324,7 @@ void var_opt_sketch<T, S, A>::validate_and_set_current_size(uint32_t preamble_lo
 
     const uint32_t ceiling_lg_k = to_log_2(ceiling_power_of_2(k_));
     const uint32_t min_lg_size = to_log_2(ceiling_power_of_2(h_));
-    const uint32_t initial_lg_size = starting_sub_multiple(ceiling_lg_k, rf_, min_lg_size);
+    const int initial_lg_size = starting_sub_multiple(ceiling_lg_k, rf_, min_lg_size);
     curr_items_alloc_ = get_adjusted_size(k_, 1 << initial_lg_size);
     if (curr_items_alloc_ == k_) { // if full size, need to leave 1 for the gap
       ++curr_items_alloc_;
@@ -1523,15 +1519,15 @@ namespace random_utils {
  * If so, returns max sampling size, otherwise passes through target size.
  */
 template<typename T, typename S, typename A>
-uint32_t var_opt_sketch<T,S,A>::get_adjusted_size(uint32_t max_size, uint32_t resize_target) {
-  if (static_cast<int32_t>(max_size) - (resize_target << 1) < 0L) {
+uint32_t var_opt_sketch<T,S,A>::get_adjusted_size(int max_size, int resize_target) {
+  if (max_size - (resize_target << 1) < 0L) {
     return max_size;
   }
   return resize_target;
 }
 
 template<typename T, typename S, typename A>
-uint32_t var_opt_sketch<T,S,A>::starting_sub_multiple(uint32_t lg_target, uint32_t lg_rf, uint32_t lg_min) {
+uint32_t var_opt_sketch<T,S,A>::starting_sub_multiple(int lg_target, int lg_rf, int lg_min) {
   return (lg_target <= lg_min)
           ? lg_min : (lg_rf == 0) ? lg_target
           : (lg_target - lg_min) % lg_rf + lg_min;
diff --git a/sampling/include/var_opt_union_impl.hpp b/sampling/include/var_opt_union_impl.hpp
index b6b7373..f2098dd 100644
--- a/sampling/include/var_opt_union_impl.hpp
+++ b/sampling/include/var_opt_union_impl.hpp
@@ -20,12 +20,11 @@
 #ifndef _VAR_OPT_UNION_IMPL_HPP_
 #define _VAR_OPT_UNION_IMPL_HPP_
 
+#include "var_opt_union.hpp"
+
 #include <cmath>
 #include <sstream>
 
-#include "var_opt_union.hpp"
-#include "memory_operations.hpp"
-
 namespace datasketches {
 
 template<typename T, typename S, typename A>
@@ -168,7 +167,6 @@ var_opt_union<T,S,A> var_opt_union<T,S,A>::deserialize(std::istream& is) {
 
 template<typename T, typename S, typename A>
 var_opt_union<T,S,A> var_opt_union<T,S,A>::deserialize(const void* bytes, size_t size) {
-  ensure_minimum_memory(size, 8);
   const char* ptr = static_cast<const char*>(bytes);
   uint8_t preamble_longs;
   ptr += copy_from_mem(ptr, &preamble_longs, sizeof(preamble_longs));
@@ -183,7 +181,6 @@ var_opt_union<T,S,A> var_opt_union<T,S,A>::deserialize(const void* bytes, size_t
 
   check_preamble_longs(preamble_longs, flags);
   check_family_and_serialization_version(family_id, serial_version);
-  ensure_minimum_memory(size, (size_t) (preamble_longs << 3));
 
   if (max_k == 0 || max_k > MAX_K) {
     throw std::invalid_argument("k must be at least 1 and less than 2^31 - 1");


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@datasketches.apache.org
For additional commands, e-mail: commits-help@datasketches.apache.org