You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@datasketches.apache.org by al...@apache.org on 2021/10/06 23:18:40 UTC

[datasketches-cpp] 01/01: use provided allocator instance

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

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

commit ac1ff8ae5c0baa67f6128a5d0646b04d9542ec9f
Author: AlexanderSaydakov <Al...@users.noreply.github.com>
AuthorDate: Wed Oct 6 16:17:21 2021 -0700

    use provided allocator instance
---
 cpc/include/cpc_union_impl.hpp          | 14 ++++++++------
 cpc/test/cpc_sketch_allocation_test.cpp | 17 +++++++++++++++++
 2 files changed, 25 insertions(+), 6 deletions(-)

diff --git a/cpc/include/cpc_union_impl.hpp b/cpc/include/cpc_union_impl.hpp
index e5a1e5d..324d258 100644
--- a/cpc/include/cpc_union_impl.hpp
+++ b/cpc/include/cpc_union_impl.hpp
@@ -34,7 +34,7 @@ bit_matrix(allocator)
   if (lg_k < CPC_MIN_LG_K || lg_k > CPC_MAX_LG_K) {
     throw std::invalid_argument("lg_k must be >= " + std::to_string(CPC_MIN_LG_K) + " and <= " + std::to_string(CPC_MAX_LG_K) + ": " + std::to_string(lg_k));
   }
-  accumulator = new (AllocCpc().allocate(1)) cpc_sketch_alloc<A>(lg_k, seed, allocator);
+  accumulator = new (AllocCpc(allocator).allocate(1)) cpc_sketch_alloc<A>(lg_k, seed, allocator);
 }
 
 template<typename A>
@@ -45,7 +45,7 @@ accumulator(other.accumulator),
 bit_matrix(other.bit_matrix)
 {
   if (accumulator != nullptr) {
-    accumulator = new (AllocCpc().allocate(1)) cpc_sketch_alloc<A>(*other.accumulator);
+    accumulator = new (AllocCpc(accumulator->get_allocator()).allocate(1)) cpc_sketch_alloc<A>(*other.accumulator);
   }
 }
 
@@ -62,8 +62,9 @@ bit_matrix(std::move(other.bit_matrix))
 template<typename A>
 cpc_union_alloc<A>::~cpc_union_alloc() {
   if (accumulator != nullptr) {
+    AllocCpc allocator(accumulator->get_allocator());
     accumulator->~cpc_sketch_alloc<A>();
-    AllocCpc().deallocate(accumulator, 1);
+    allocator.deallocate(accumulator, 1);
   }
 }
 
@@ -181,7 +182,7 @@ template<typename A>
 cpc_sketch_alloc<A> cpc_union_alloc<A>::get_result_from_accumulator() const {
   if (lg_k != accumulator->get_lg_k()) throw std::logic_error("lg_k != accumulator->lg_k");
   if (accumulator->get_num_coupons() == 0) {
-    return cpc_sketch_alloc<A>(lg_k, seed);
+    return cpc_sketch_alloc<A>(lg_k, seed, accumulator->get_allocator());
   }
   if (accumulator->determine_flavor() != cpc_sketch_alloc<A>::flavor::SPARSE) throw std::logic_error("wrong flavor");
   cpc_sketch_alloc<A> copy(*accumulator);
@@ -242,8 +243,9 @@ cpc_sketch_alloc<A> cpc_union_alloc<A>::get_result_from_bit_matrix() const {
 template<typename A>
 void cpc_union_alloc<A>::switch_to_bit_matrix() {
   bit_matrix = accumulator->build_bit_matrix();
+  AllocCpc allocator(accumulator->get_allocator());
   accumulator->~cpc_sketch_alloc<A>();
-  AllocCpc().deallocate(accumulator, 1);
+  allocator.deallocate(accumulator, 1);
   accumulator = nullptr;
 }
 
@@ -324,7 +326,7 @@ void cpc_union_alloc<A>::reduce_k(uint8_t new_lg_k) {
     if (bit_matrix.size() > 0) throw std::logic_error("bit_matrix is not expected");
     if (!accumulator->is_empty()) {
       cpc_sketch_alloc<A> old_accumulator(*accumulator);
-      *accumulator = cpc_sketch_alloc<A>(new_lg_k, seed);
+      *accumulator = cpc_sketch_alloc<A>(new_lg_k, seed, old_accumulator.get_allocator());
       walk_table_updating_sketch(old_accumulator.surprising_value_table);
     }
     lg_k = new_lg_k;
diff --git a/cpc/test/cpc_sketch_allocation_test.cpp b/cpc/test/cpc_sketch_allocation_test.cpp
index 92e1a05..113bfc6 100644
--- a/cpc/test/cpc_sketch_allocation_test.cpp
+++ b/cpc/test/cpc_sketch_allocation_test.cpp
@@ -25,6 +25,7 @@
 #include <catch.hpp>
 
 #include "cpc_sketch.hpp"
+#include "cpc_union.hpp"
 #include "test_allocator.hpp"
 
 namespace datasketches {
@@ -234,4 +235,20 @@ TEST_CASE("cpc sketch allocation: serialize deserialize sliding, bytes", "[cpc_s
   REQUIRE(test_allocator_net_allocations == 0);
 }
 
+using cpc_union_test_alloc = cpc_union_alloc<test_allocator<uint8_t>>;
+
+TEST_CASE("cpc sketch allocation: union") {
+  cpc_sketch_test_alloc s1(11, DEFAULT_SEED, 0);
+  s1.update(1);
+
+  cpc_sketch_test_alloc s2(11, DEFAULT_SEED, 0);
+  s2.update(2);
+
+  cpc_union_test_alloc u(11, DEFAULT_SEED, 0);
+  u.update(s1);
+  u.update(s2);
+  auto s3 = u.get_result();
+  REQUIRE_FALSE(s3.is_empty());
+}
+
 } /* namespace datasketches */

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