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 2020/08/14 23:46:44 UTC

[incubator-datasketches-cpp] 03/07: better allocation test

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

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

commit b3f5439ee85e6d0e210c3fb768209dbfc26245f0
Author: AlexanderSaydakov <Al...@users.noreply.github.com>
AuthorDate: Wed Aug 12 15:33:04 2020 -0700

    better allocation test
---
 common/test/test_type.hpp                   | 44 +++++++++-----
 tuple/test/tuple_sketch_allocation_test.cpp | 91 +++++++++++++++++------------
 tuple/test/tuple_sketch_test.cpp            | 42 +------------
 3 files changed, 87 insertions(+), 90 deletions(-)

diff --git a/common/test/test_type.hpp b/common/test/test_type.hpp
index cbd1a2e..18be598 100644
--- a/common/test/test_type.hpp
+++ b/common/test/test_type.hpp
@@ -24,38 +24,51 @@
 
 namespace datasketches {
 
-class test_type {
-  static const bool DEBUG = true;
+template<typename A>
+class test_type_alloc {
+  static const bool DEBUG = false;
 public:
   // no default constructor should be required
-  test_type(int value): value(value) {
+  test_type_alloc(int value): value_ptr(A().allocate(1)) {
     if (DEBUG) std::cerr << "test_type constructor" << std::endl;
+    *value_ptr = value;
   }
-  ~test_type() {
+  ~test_type_alloc() {
     if (DEBUG) std::cerr << "test_type destructor" << std::endl;
+    if (value_ptr != nullptr) A().deallocate(value_ptr, 1);
   }
-  test_type(const test_type& other): value(other.value) {
+  test_type_alloc(const test_type_alloc& other): value_ptr(A().allocate(1)) {
     if (DEBUG) std::cerr << "test_type copy constructor" << std::endl;
+    *value_ptr = *other.value_ptr;
   }
   // noexcept is important here so that, for instance, std::vector could move this type
-  test_type(test_type&& other) noexcept : value(other.value) {
+  test_type_alloc(test_type_alloc&& other) noexcept : value_ptr(nullptr) {
     if (DEBUG) std::cerr << "test_type move constructor" << std::endl;
+    if (DEBUG && other.value_ptr == nullptr) std::cerr << "moving null" << std::endl;
+    std::swap(value_ptr, other.value_ptr);
   }
-  test_type& operator=(const test_type& other) {
+  test_type_alloc& operator=(const test_type_alloc& other) {
     if (DEBUG) std::cerr << "test_type copy assignment" << std::endl;
-    value = other.value;
+    if (DEBUG && value_ptr == nullptr) std::cerr << "nullptr" << std::endl;
+    *value_ptr = *other.value_ptr;
     return *this;
   }
-  test_type& operator=(test_type&& other) {
+  test_type_alloc& operator=(test_type_alloc&& other) {
     if (DEBUG) std::cerr << "test_type move assignment" << std::endl;
-    value = other.value;
+    if (DEBUG && other.value_ptr == nullptr) std::cerr << "moving null" << std::endl;
+    std::swap(value_ptr, other.value_ptr);
     return *this;
   }
-  int get_value() const { return value; }
+  int get_value() const {
+    if (value_ptr == nullptr) std::cerr << "null" << std::endl;
+    return *value_ptr;
+  }
 private:
-  int value;
+  int* value_ptr;
 };
 
+using test_type = test_type_alloc<std::allocator<int>>;
+
 struct test_type_hash {
   std::size_t operator()(const test_type& a) const {
     return std::hash<int>()(a.get_value());
@@ -95,7 +108,8 @@ struct test_type_serde {
     const size_t bytes_written = sizeof(int) * num;
     check_memory_size(bytes_written, capacity);
     for (unsigned i = 0; i < num; ++i) {
-      memcpy(ptr, &items[i], sizeof(int));
+      const int value = items[i].get_value();
+      memcpy(ptr, &value, sizeof(int));
       ptr = static_cast<char*>(ptr) + sizeof(int);
     }
     return bytes_written;
@@ -104,7 +118,9 @@ struct test_type_serde {
     const size_t bytes_read = sizeof(int) * num;
     check_memory_size(bytes_read, capacity);
     for (unsigned i = 0; i < num; ++i) {
-      memcpy(&items[i], ptr, sizeof(int));
+      int value;
+      memcpy(&value, ptr, sizeof(int));
+      new (&items[i]) test_type(value);
       ptr = static_cast<const char*>(ptr) + sizeof(int);
     }
     return bytes_read;
diff --git a/tuple/test/tuple_sketch_allocation_test.cpp b/tuple/test/tuple_sketch_allocation_test.cpp
index 42a1f3e..4858d3a 100644
--- a/tuple/test/tuple_sketch_allocation_test.cpp
+++ b/tuple/test/tuple_sketch_allocation_test.cpp
@@ -22,59 +22,76 @@
 #include <catch.hpp>
 #include <tuple_sketch.hpp>
 #include <test_allocator.hpp>
+#include <test_type.hpp>
 
 namespace datasketches {
 
-using update_tuple_sketch_int_alloc =
-    update_tuple_sketch<int, int, default_update_policy<int, int>, test_allocator<int>>;
-using compact_tuple_sketch_int_alloc =
-    compact_tuple_sketch<int, test_allocator<int>>;
+struct test_type_replace_policy {
+  test_type create() const { return test_type(0); }
+  void update(test_type& summary, const test_type& update) const {
+    //std::cerr << "policy::update lvalue begin" << std::endl;
+    summary = update;
+    //std::cerr << "policy::update lvalue end" << std::endl;
+  }
+  void update(test_type& summary, test_type&& update) const {
+    //std::cerr << "policy::update rvalue begin" << std::endl;
+    summary = std::move(update);
+    //std::cerr << "policy::update rvalue end" << std::endl;
+  }
+};
 
-TEST_CASE("tuple sketch with test allocator: exact mode", "[tuple_sketch]") {
+using update_tuple_sketch_test =
+    update_tuple_sketch<test_type, test_type, test_type_replace_policy, test_allocator<test_type>>;
+using compact_tuple_sketch_test =
+    compact_tuple_sketch<test_type, test_allocator<test_type>>;
+
+TEST_CASE("tuple sketch with test allocator: estimation mode", "[tuple_sketch]") {
   test_allocator_total_bytes = 0;
   test_allocator_net_allocations = 0;
   {
-    auto update_sketch = update_tuple_sketch_int_alloc::builder().build();
-    for (int i = 0; i < 10000; ++i) update_sketch.update(i, 1);
+    auto update_sketch = update_tuple_sketch_test::builder().build();
     for (int i = 0; i < 10000; ++i) update_sketch.update(i, 1);
-    REQUIRE(!update_sketch.is_empty());
-    REQUIRE(update_sketch.is_estimation_mode());
-    unsigned count = 0;
-    for (const auto& entry: update_sketch) {
-      REQUIRE(entry.second == 2);
-      ++count;
-    }
-    REQUIRE(count == update_sketch.get_num_retained());
+//    for (int i = 0; i < 10000; ++i) update_sketch.update(i, 2);
+//    REQUIRE(!update_sketch.is_empty());
+//    REQUIRE(update_sketch.is_estimation_mode());
+//    unsigned count = 0;
+//    for (const auto& entry: update_sketch) {
+//      REQUIRE(entry.second.get_value() == 2);
+//      ++count;
+//    }
+//    REQUIRE(count == update_sketch.get_num_retained());
 
-    update_sketch.trim();
-    REQUIRE(update_sketch.get_num_retained() == (1 << update_sketch.get_lg_k()));
+//    update_sketch.trim();
+//    REQUIRE(update_sketch.get_num_retained() == (1 << update_sketch.get_lg_k()));
 
-    auto compact_sketch = update_sketch.compact();
-    REQUIRE(!compact_sketch.is_empty());
-    REQUIRE(compact_sketch.is_estimation_mode());
-    count = 0;
-    for (const auto& entry: compact_sketch) {
-      REQUIRE(entry.second == 2);
-      ++count;
-    }
-    REQUIRE(count == update_sketch.get_num_retained());
+//    auto compact_sketch = update_sketch.compact();
+//    REQUIRE(!compact_sketch.is_empty());
+//    REQUIRE(compact_sketch.is_estimation_mode());
+//    count = 0;
+//    for (const auto& entry: compact_sketch) {
+//      REQUIRE(entry.second.get_value() == 2);
+//      ++count;
+//    }
+//    REQUIRE(count == update_sketch.get_num_retained());
 
-    auto bytes = compact_sketch.serialize();
-    auto deserialized_sketch = compact_tuple_sketch_int_alloc::deserialize(bytes.data(), bytes.size());
-    REQUIRE(deserialized_sketch.get_estimate() == compact_sketch.get_estimate());
+//    auto bytes = compact_sketch.serialize(0, test_type_serde());
+//    auto deserialized_sketch = compact_tuple_sketch_test::deserialize(bytes.data(), bytes.size(), DEFAULT_SEED, test_type_serde());
+//    REQUIRE(deserialized_sketch.get_estimate() == compact_sketch.get_estimate());
 
     // update sketch copy
-    update_tuple_sketch_int_alloc update_sketch_copy(update_sketch);
-    update_sketch_copy = update_sketch;
+//    std::cout << update_sketch.to_string();
+//    update_tuple_sketch_test update_sketch_copy(update_sketch);
+//    update_sketch_copy = update_sketch;
     // update sketch move
-    update_tuple_sketch_int_alloc update_sketch_moved(std::move(update_sketch_copy));
-    update_sketch_moved = std::move(update_sketch);
+//    update_tuple_sketch_test update_sketch_moved(std::move(update_sketch_copy));
+//    update_sketch_moved = std::move(update_sketch);
 
     // compact sketch copy
-    compact_tuple_sketch_int_alloc compact_sketch_copy(compact_sketch);
-    compact_sketch_copy = compact_sketch;
-    compact_tuple_sketch_int_alloc compact_sketch_moved(std::move(compact_sketch_copy));
-    compact_sketch_moved = std::move(compact_sketch);
+//    compact_tuple_sketch_test compact_sketch_copy(compact_sketch);
+//    compact_sketch_copy = compact_sketch;
+    // compact sketch move
+//    compact_tuple_sketch_test compact_sketch_moved(std::move(compact_sketch_copy));
+//    compact_sketch_moved = std::move(compact_sketch);
   }
   REQUIRE(test_allocator_total_bytes == 0);
   REQUIRE(test_allocator_net_allocations == 0);
diff --git a/tuple/test/tuple_sketch_test.cpp b/tuple/test/tuple_sketch_test.cpp
index b4773aa..11300a3 100644
--- a/tuple/test/tuple_sketch_test.cpp
+++ b/tuple/test/tuple_sketch_test.cpp
@@ -34,7 +34,7 @@ std::ostream& operator<<(std::ostream& os, const three_doubles& tuple) {
 
 #include <catch.hpp>
 #include <tuple_sketch.hpp>
-#include <test_type.hpp>
+//#include <test_type.hpp>
 
 namespace datasketches {
 
@@ -122,8 +122,8 @@ TEST_CASE("tuple sketch float: exact mode", "[tuple_sketch]") {
     REQUIRE(deserialized_sketch.get_theta() == 1);
     REQUIRE(deserialized_sketch.get_num_retained() == 2);
     REQUIRE(deserialized_sketch.is_ordered());
-    std::cout << "deserialized sketch:" << std::endl;
-    std::cout << deserialized_sketch.to_string(true);
+//    std::cout << "deserialized sketch:" << std::endl;
+//    std::cout << deserialized_sketch.to_string(true);
   }
   { // bytes
     auto bytes = compact_sketch.serialize();
@@ -170,42 +170,6 @@ TEST_CASE("tuple sketch: float, custom policy", "[tuple_sketch]") {
   REQUIRE(sum == 22); // 5 + 10 + 7
 }
 
-struct test_type_replace_policy {
-  test_type create() const { return test_type(0); }
-  void update(test_type& summary, const test_type& update) const {
-    //std::cerr << "policy::update lvalue begin" << std::endl;
-    summary = update;
-    //std::cerr << "policy::update lvalue end" << std::endl;
-  }
-  void update(test_type& summary, test_type&& update) const {
-    //std::cerr << "policy::update rvalue begin" << std::endl;
-    summary = std::move(update);
-    //std::cerr << "policy::update rvalue end" << std::endl;
-  }
-};
-
-TEST_CASE("tuple sketch: test type with replace policy", "[tuple_sketch]") {
-  auto update_sketch = update_tuple_sketch<test_type, test_type, test_type_replace_policy>::builder().build();
-  test_type a(1);
-  update_sketch.update(1, a); // this should copy
-  update_sketch.update(2, 2); // this should move
-  update_sketch.update(1, 2); // this should move
-//  std::cout << sketch.to_string(true);
-  REQUIRE(update_sketch.get_num_retained() == 2);
-  for (const auto& entry: update_sketch) {
-    REQUIRE(entry.second.get_value() == 2);
-  }
-
-  auto compact_sketch = update_sketch.compact();
-  auto bytes = compact_sketch.serialize(0, test_type_serde());
-  auto deserialized_sketch = compact_tuple_sketch<test_type>::deserialize(bytes.data(), bytes.size(),
-      DEFAULT_SEED, test_type_serde());
-  REQUIRE(deserialized_sketch.get_num_retained() == 2);
-  for (const auto& entry: deserialized_sketch) {
-    REQUIRE(entry.second.get_value() == 2);
-  }
-}
-
 struct three_doubles_update_policy {
   std::tuple<double, double, double> create() const {
     return std::tuple<double, double, double>(0, 0, 0);


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