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/07/09 05:59:47 UTC

[incubator-datasketches-cpp] 01/02: inlining and forwarding

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 d694163bf2d5e86d0f292cbe2bbc48c74417ea5b
Author: AlexanderSaydakov <Al...@users.noreply.github.com>
AuthorDate: Wed Jul 8 22:58:27 2020 -0700

    inlining and forwarding
---
 tuple/include/theta_comparators.hpp         | 16 ++++------------
 tuple/include/theta_intersection_base.hpp   |  2 +-
 tuple/include/theta_set_difference_base.hpp |  2 +-
 tuple/include/theta_sketch_experimental.hpp |  6 +++---
 tuple/include/theta_union_base.hpp          |  2 +-
 tuple/include/theta_union_experimental.hpp  |  2 +-
 tuple/include/theta_update_sketch_base.hpp  | 19 ++++++++-----------
 tuple/include/tuple_sketch.hpp              | 24 ++++++++++++------------
 8 files changed, 31 insertions(+), 42 deletions(-)

diff --git a/tuple/include/theta_comparators.hpp b/tuple/include/theta_comparators.hpp
index f461b5d..a7520c1 100644
--- a/tuple/include/theta_comparators.hpp
+++ b/tuple/include/theta_comparators.hpp
@@ -22,24 +22,16 @@
 
 namespace datasketches {
 
-template<typename Entry, typename ExtractKey>
+template<typename ExtractKey>
 struct compare_by_key {
-  bool operator()(const Entry& a, const Entry& b) const {
-    return ExtractKey()(a) < ExtractKey()(b);
+  template<typename Entry>
+  bool operator()(Entry&& a, Entry&& b) const {
+    return ExtractKey()(std::forward<Entry>(a)) < ExtractKey()(std::forward<Entry>(b));
   }
 };
 
 // less than
 
-template<typename T>
-class less_than {
-public:
-  explicit less_than(const T& value): value(value) {}
-  bool operator()(const T& value) const { return value < this->value; }
-private:
-  T value;
-};
-
 template<typename Key, typename Entry, typename ExtractKey>
 class key_less_than {
 public:
diff --git a/tuple/include/theta_intersection_base.hpp b/tuple/include/theta_intersection_base.hpp
index f6c748a..3ed7f79 100644
--- a/tuple/include/theta_intersection_base.hpp
+++ b/tuple/include/theta_intersection_base.hpp
@@ -32,7 +32,7 @@ template<
 >
 class theta_intersection_base {
 public:
-  using comparator = compare_by_key<Entry, ExtractKey>;
+  using comparator = compare_by_key<ExtractKey>;
   theta_intersection_base(uint64_t seed, const Policy& policy);
   ~theta_intersection_base();
   void destroy_objects();
diff --git a/tuple/include/theta_set_difference_base.hpp b/tuple/include/theta_set_difference_base.hpp
index 03fe6bb..253fbba 100644
--- a/tuple/include/theta_set_difference_base.hpp
+++ b/tuple/include/theta_set_difference_base.hpp
@@ -34,7 +34,7 @@ template<
 >
 class theta_set_difference_base {
 public:
-  using comparator = compare_by_key<Entry, ExtractKey>;
+  using comparator = compare_by_key<ExtractKey>;
   using hash_table = theta_update_sketch_base<Entry, ExtractKey, Allocator>;
 
   theta_set_difference_base(uint64_t seed);
diff --git a/tuple/include/theta_sketch_experimental.hpp b/tuple/include/theta_sketch_experimental.hpp
index 7e87b95..fff6623 100644
--- a/tuple/include/theta_sketch_experimental.hpp
+++ b/tuple/include/theta_sketch_experimental.hpp
@@ -47,7 +47,7 @@ public:
   uint64_t get_theta64() const { return table_.theta_; }
   uint32_t get_num_retained() const { return table_.num_entries_; }
 
-  void update(uint64_t key);
+  inline void update(uint64_t key);
   void update(const void* key, size_t length);
 
   void trim();
@@ -56,7 +56,7 @@ public:
 
   vector_bytes serialize(unsigned header_size_bytes = 0) const;
 
-  using const_iterator = theta_const_iterator<uint64_t, trivial_extract_key<uint64_t>>;
+  using const_iterator = theta_const_iterator<uint64_t, trivial_extract_key>;
   const_iterator begin() const;
   const_iterator end() const;
 
@@ -64,7 +64,7 @@ public:
 
 private:
   enum flags { IS_BIG_ENDIAN, IS_READ_ONLY, IS_EMPTY, IS_COMPACT, IS_ORDERED };
-  using theta_table = theta_update_sketch_base<uint64_t, trivial_extract_key<uint64_t>, A>;
+  using theta_table = theta_update_sketch_base<uint64_t, trivial_extract_key, A>;
   theta_table table_;
 
   theta_sketch_experimental(uint8_t lg_cur_size, uint8_t lg_nom_size, resize_factor rf, float p, uint64_t seed);
diff --git a/tuple/include/theta_union_base.hpp b/tuple/include/theta_union_base.hpp
index 0cb4005..0b9a1b8 100644
--- a/tuple/include/theta_union_base.hpp
+++ b/tuple/include/theta_union_base.hpp
@@ -36,7 +36,7 @@ class theta_union_base {
 public:
   using hash_table = theta_update_sketch_base<Entry, ExtractKey, Allocator>;
   using resize_factor = typename hash_table::resize_factor;
-  using comparator = compare_by_key<Entry, ExtractKey>;
+  using comparator = compare_by_key<ExtractKey>;
 
   theta_union_base(uint8_t lg_cur_size, uint8_t lg_nom_size, resize_factor rf, float p, uint64_t seed, const Policy& policy);
 
diff --git a/tuple/include/theta_union_experimental.hpp b/tuple/include/theta_union_experimental.hpp
index a394152..5fc7faf 100644
--- a/tuple/include/theta_union_experimental.hpp
+++ b/tuple/include/theta_union_experimental.hpp
@@ -40,7 +40,7 @@ template<typename Allocator = std::allocator<uint64_t>>
 class theta_union_experimental {
 public:
   using Entry = uint64_t;
-  using ExtractKey = trivial_extract_key<uint64_t>;
+  using ExtractKey = trivial_extract_key;
   using Sketch = theta_sketch_experimental<Allocator>;
   using CompactSketch = compact_theta_sketch_experimental<Allocator>;
   using resize_factor = theta_constants::resize_factor;
diff --git a/tuple/include/theta_update_sketch_base.hpp b/tuple/include/theta_update_sketch_base.hpp
index f261389..6c1e441 100644
--- a/tuple/include/theta_update_sketch_base.hpp
+++ b/tuple/include/theta_update_sketch_base.hpp
@@ -41,7 +41,7 @@ template<
 >
 struct theta_update_sketch_base {
   using resize_factor = theta_constants::resize_factor;
-  using comparator = compare_by_key<Entry, ExtractKey>;
+  using comparator = compare_by_key<ExtractKey>;
 
   theta_update_sketch_base(uint8_t lg_cur_size, uint8_t lg_nom_size, resize_factor rf, float p, uint64_t seed);
   // TODO: copy and move
@@ -49,12 +49,12 @@ struct theta_update_sketch_base {
 
   using iterator = Entry*;
 
-  uint64_t hash_and_screen(const void* data, size_t length);
+  inline uint64_t hash_and_screen(const void* data, size_t length);
 
-  std::pair<iterator, bool> find(uint64_t key) const;
+  inline std::pair<iterator, bool> find(uint64_t key) const;
 
   template<typename FwdEntry>
-  void insert(iterator it, FwdEntry&& entry);
+  inline void insert(iterator it, FwdEntry&& entry);
 
   iterator begin() const;
   iterator end() const;
@@ -86,7 +86,7 @@ struct theta_update_sketch_base {
   static inline uint32_t get_capacity(uint8_t lg_cur_size, uint8_t lg_nom_size);
   static inline uint32_t get_stride(uint64_t key, uint8_t lg_size);
 
-  static std::pair<iterator, bool> find(Entry* entries, uint8_t lg_size, uint64_t key);
+  static inline std::pair<iterator, bool> find(Entry* entries, uint8_t lg_size, uint64_t key);
 };
 
 // builder
@@ -148,13 +148,10 @@ protected:
 
 // key extractors
 
-template<typename T>
 struct trivial_extract_key {
-  T& operator()(T& entry) const {
-    return entry;
-  }
-  const T& operator()(const T& entry) const {
-    return entry;
+  template<typename T>
+  auto operator()(T&& entry) const -> decltype(std::forward<T>(entry)) {
+    return std::forward<T>(entry);
   }
 };
 
diff --git a/tuple/include/tuple_sketch.hpp b/tuple/include/tuple_sketch.hpp
index 87d6dc9..5ecb044 100644
--- a/tuple/include/tuple_sketch.hpp
+++ b/tuple/include/tuple_sketch.hpp
@@ -186,21 +186,21 @@ public:
    * @param value string to update the sketch with
    */
   template<typename FwdUpdate>
-  void update(const std::string& key, FwdUpdate&& value);
+  inline void update(const std::string& key, FwdUpdate&& value);
 
   /**
    * Update this sketch with a given unsigned 64-bit integer.
    * @param value uint64_t to update the sketch with
    */
   template<typename FwdUpdate>
-  void update(uint64_t key, FwdUpdate&& value);
+  inline void update(uint64_t key, FwdUpdate&& value);
 
   /**
    * Update this sketch with a given signed 64-bit integer.
    * @param value int64_t to update the sketch with
    */
   template<typename FwdUpdate>
-  void update(int64_t key, FwdUpdate&& value);
+  inline void update(int64_t key, FwdUpdate&& value);
 
   /**
    * Update this sketch with a given unsigned 32-bit integer.
@@ -208,7 +208,7 @@ public:
    * @param value uint32_t to update the sketch with
    */
   template<typename FwdUpdate>
-  void update(uint32_t key, FwdUpdate&& value);
+  inline void update(uint32_t key, FwdUpdate&& value);
 
   /**
    * Update this sketch with a given signed 32-bit integer.
@@ -216,7 +216,7 @@ public:
    * @param value int32_t to update the sketch with
    */
   template<typename FwdUpdate>
-  void update(int32_t key, FwdUpdate&& value);
+  inline void update(int32_t key, FwdUpdate&& value);
 
   /**
    * Update this sketch with a given unsigned 16-bit integer.
@@ -224,7 +224,7 @@ public:
    * @param value uint16_t to update the sketch with
    */
   template<typename FwdUpdate>
-  void update(uint16_t key, FwdUpdate&& value);
+  inline void update(uint16_t key, FwdUpdate&& value);
 
   /**
    * Update this sketch with a given signed 16-bit integer.
@@ -232,7 +232,7 @@ public:
    * @param value int16_t to update the sketch with
    */
   template<typename FwdUpdate>
-  void update(int16_t key, FwdUpdate&& value);
+  inline void update(int16_t key, FwdUpdate&& value);
 
   /**
    * Update this sketch with a given unsigned 8-bit integer.
@@ -240,7 +240,7 @@ public:
    * @param value uint8_t to update the sketch with
    */
   template<typename FwdUpdate>
-  void update(uint8_t key, FwdUpdate&& value);
+  inline void update(uint8_t key, FwdUpdate&& value);
 
   /**
    * Update this sketch with a given signed 8-bit integer.
@@ -248,7 +248,7 @@ public:
    * @param value int8_t to update the sketch with
    */
   template<typename FwdUpdate>
-  void update(int8_t key, FwdUpdate&& value);
+  inline void update(int8_t key, FwdUpdate&& value);
 
   /**
    * Update this sketch with a given double-precision floating point value.
@@ -256,7 +256,7 @@ public:
    * @param value double to update the sketch with
    */
   template<typename FwdUpdate>
-  void update(double key, FwdUpdate&& value);
+  inline void update(double key, FwdUpdate&& value);
 
   /**
    * Update this sketch with a given floating point value.
@@ -264,7 +264,7 @@ public:
    * @param value float to update the sketch with
    */
   template<typename FwdUpdate>
-  void update(float key, FwdUpdate&& value);
+  inline void update(float key, FwdUpdate&& value);
 
   /**
    * Update this sketch with given data of any type.
@@ -321,7 +321,7 @@ public:
   using AllocU64 = typename std::allocator_traits<Allocator>::template rebind_alloc<uint64_t>;
   using AllocBytes = typename std::allocator_traits<Allocator>::template rebind_alloc<uint8_t>;
   using vector_bytes = std::vector<uint8_t, AllocBytes>;
-  using comparator = compare_by_key<Entry, ExtractKey>;
+  using comparator = compare_by_key<ExtractKey>;
 
   static const uint8_t SERIAL_VERSION = 3;
   static const uint8_t SKETCH_TYPE = 3;


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