You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@doris.apache.org by yi...@apache.org on 2023/01/11 00:39:21 UTC

[doris] branch master updated: [refactor](hashtable) simplify template args of partitioned hash table (#15736)

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

yiguolei pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/doris.git


The following commit(s) were added to refs/heads/master by this push:
     new 4bbc93b7ce [refactor](hashtable) simplify template args of partitioned hash table (#15736)
4bbc93b7ce is described below

commit 4bbc93b7cee12c0e245ef3a578d827aa1738274c
Author: Jerry Hu <mr...@gmail.com>
AuthorDate: Wed Jan 11 08:39:13 2023 +0800

    [refactor](hashtable) simplify template args of partitioned hash table (#15736)
---
 be/src/vec/common/hash_table/hash_table.h          |  8 +++--
 .../vec/common/hash_table/partitioned_hash_map.h   | 37 ++++++++--------------
 .../vec/common/hash_table/partitioned_hash_table.h | 33 ++++++++-----------
 3 files changed, 31 insertions(+), 47 deletions(-)

diff --git a/be/src/vec/common/hash_table/hash_table.h b/be/src/vec/common/hash_table/hash_table.h
index 457012c0fd..337d3ed8f9 100644
--- a/be/src/vec/common/hash_table/hash_table.h
+++ b/be/src/vec/common/hash_table/hash_table.h
@@ -420,9 +420,9 @@ struct ZeroValueStorage<false, Cell> {
     const Cell* zero_value() const { return nullptr; }
 };
 
-template <typename Key, typename Cell, typename Hash, typename Grower, typename Allocator>
+template <typename Key, typename Cell, typename HashMethod, typename Grower, typename Allocator>
 class HashTable : private boost::noncopyable,
-                  protected Hash,
+                  protected HashMethod,
                   protected Allocator,
                   protected Cell::State,
                   protected ZeroValueStorage<Cell::need_zero_value_storage,
@@ -431,7 +431,7 @@ class HashTable : private boost::noncopyable,
 protected:
     friend class Reader;
 
-    template <typename, typename, typename, typename, typename, typename, size_t>
+    template <typename, size_t>
     friend class PartitionedHashTable;
 
     template <typename SubMaps>
@@ -610,6 +610,8 @@ protected:
 public:
     using key_type = Key;
     using value_type = typename Cell::value_type;
+    using mapped_type = value_type;
+    using Hash = HashMethod;
 
     // Use lookup_result_get_mapped/Key to work with these values.
     using LookupResult = Cell*;
diff --git a/be/src/vec/common/hash_table/partitioned_hash_map.h b/be/src/vec/common/hash_table/partitioned_hash_map.h
index fabf61b27a..b78d9a56ed 100644
--- a/be/src/vec/common/hash_table/partitioned_hash_map.h
+++ b/be/src/vec/common/hash_table/partitioned_hash_map.h
@@ -22,43 +22,32 @@
 #include "vec/common/hash_table/hash_map.h"
 #include "vec/common/hash_table/partitioned_hash_table.h"
 
-template <typename Key, typename Cell, typename Hash = DefaultHash<Key>,
-          typename Grower = PartitionedHashTableGrower<>, typename Allocator = HashTableAllocator,
-          template <typename...> typename ImplTable = HashMapTable>
-class PartitionedHashMapTable
-        : public PartitionedHashTable<Key, Cell, Hash, Grower, Allocator,
-                                      ImplTable<Key, Cell, Hash, Grower, Allocator>> {
+template <typename ImplTable>
+class PartitionedHashMapTable : public PartitionedHashTable<ImplTable> {
 public:
-    using Impl = ImplTable<Key, Cell, Hash, Grower, Allocator>;
-    using Base = PartitionedHashTable<Key, Cell, Hash, Grower, Allocator,
-                                      ImplTable<Key, Cell, Hash, Grower, Allocator>>;
+    using Impl = ImplTable;
+    using Base = PartitionedHashTable<ImplTable>;
+    using Key = typename ImplTable::key_type;
     using LookupResult = typename Impl::LookupResult;
 
     using Base::Base;
     using Base::prefetch;
 
-    using mapped_type = typename Cell::Mapped;
+    using mapped_type = typename Base::mapped_type;
 
-    typename Cell::Mapped& ALWAYS_INLINE operator[](const Key& x) {
+    auto& ALWAYS_INLINE operator[](const Key& x) {
         LookupResult it;
         bool inserted;
         this->emplace(x, it, inserted);
 
-        if (inserted) new (lookup_result_get_mapped(it)) mapped_type();
+        if (inserted) {
+            new (lookup_result_get_mapped(it)) mapped_type();
+        }
 
         return *lookup_result_get_mapped(it);
     }
 };
 
-template <typename Key, typename Mapped, typename Hash = DefaultHash<Key>,
-          typename Grower = PartitionedHashTableGrower<>, typename Allocator = HashTableAllocator,
-          template <typename...> typename ImplTable = HashMapTable>
-using PartitionedHashMap = PartitionedHashMapTable<Key, HashMapCell<Key, Mapped, Hash>, Hash,
-                                                   Grower, Allocator, ImplTable>;
-
-template <typename Key, typename Mapped, typename Hash = DefaultHash<Key>,
-          typename Grower = PartitionedHashTableGrower<>, typename Allocator = HashTableAllocator,
-          template <typename...> typename ImplTable = HashMapTable>
-using PartitionedHashMapWithSavedHash =
-        PartitionedHashMapTable<Key, HashMapCellWithSavedHash<Key, Mapped, Hash>, Hash, Grower,
-                                Allocator, ImplTable>;
+template <typename Key, typename Mapped, typename Hash = DefaultHash<Key>>
+using PartitionedHashMap =
+        PartitionedHashMapTable<HashMap<Key, Mapped, Hash, PartitionedHashTableGrower<>>>;
diff --git a/be/src/vec/common/hash_table/partitioned_hash_table.h b/be/src/vec/common/hash_table/partitioned_hash_table.h
index 8dea744d9a..1bf128803b 100644
--- a/be/src/vec/common/hash_table/partitioned_hash_table.h
+++ b/be/src/vec/common/hash_table/partitioned_hash_table.h
@@ -38,25 +38,19 @@ struct PartitionedHashTableGrower : public HashTableGrowerWithPrecalculation<ini
     void increase_size() { this->increase_size_degree(this->size_degree() >= 15 ? 1 : 2); }
 };
 
-template <typename Key, typename Cell, typename Hash, typename Grower, typename Allocator,
-          typename ImplTable = HashTable<Key, Cell, Hash, Grower, Allocator>,
-          size_t BITS_FOR_SUB_TABLE = 4>
-class PartitionedHashTable : private boost::noncopyable,
-                             protected Hash /// empty base optimization
-{
+template <typename Impl, size_t BITS_FOR_SUB_TABLE = 4>
+class PartitionedHashTable : private boost::noncopyable, Impl::Hash {
 public:
-    using Impl = ImplTable;
-
     using key_type = typename Impl::key_type;
     using mapped_type = typename Impl::mapped_type;
     using value_type = typename Impl::value_type;
     using cell_type = typename Impl::cell_type;
+    using Key = typename Impl::key_type;
 
     using LookupResult = typename Impl::LookupResult;
     using ConstLookupResult = typename Impl::ConstLookupResult;
 
 protected:
-    using HashValue = size_t;
     using Self = PartitionedHashTable;
 
 private:
@@ -86,11 +80,10 @@ public:
             level1_sub_tables[i] = std::move(rhs.level1_sub_tables[i]);
         }
 
-        Hash::operator=(std::move(rhs));
         return *this;
     }
 
-    size_t hash(const Key& x) const { return Hash::operator()(x); }
+    size_t hash(const Key& x) const { return Impl::Hash::operator()(x); }
 
     float get_factor() const { return MAX_SUB_TABLE_OCCUPANCY_FRACTION; }
 
@@ -270,10 +263,10 @@ public:
             return *this;
         }
 
-        Cell& operator*() const { return *current_it; }
-        Cell* operator->() const { return current_it.get_ptr(); }
+        auto& operator*() const { return *current_it; }
+        auto* operator->() const { return current_it.get_ptr(); }
 
-        Cell* get_ptr() const { return current_it.get_ptr(); }
+        auto* get_ptr() const { return current_it.get_ptr(); }
         size_t get_hash() const { return current_it.get_hash(); }
     };
 
@@ -313,10 +306,10 @@ public:
             return *this;
         }
 
-        const Cell& operator*() const { return *current_it; }
-        const Cell* operator->() const { return current_it->get_ptr(); }
+        const auto& operator*() const { return *current_it; }
+        const auto* operator->() const { return current_it->get_ptr(); }
 
-        const Cell* get_ptr() const { return current_it.get_ptr(); }
+        const auto* get_ptr() const { return current_it.get_ptr(); }
         size_t get_hash() const { return current_it.get_hash(); }
     };
 
@@ -358,10 +351,10 @@ public:
 
     /// Insert a value. In the case of any more complex values, it is better to use the `emplace` function.
     std::pair<LookupResult, bool> ALWAYS_INLINE insert(const value_type& x) {
-        size_t hash_value = hash(Cell::get_key(x));
+        size_t hash_value = hash(cell_type::get_key(x));
 
         std::pair<LookupResult, bool> res;
-        emplace(Cell::get_key(x), res.first, res.second, hash_value);
+        emplace(cell_type::get_key(x), res.first, res.second, hash_value);
 
         if (res.second) insert_set_mapped(lookup_result_get_mapped(res.first), x);
 
@@ -531,7 +524,7 @@ private:
         }
 
         for (; it != level0_sub_table.end(); ++it) {
-            const Cell* cell = it.get_ptr();
+            const auto* cell = it.get_ptr();
             size_t hash_value = cell->get_hash(level0_sub_table);
             size_t sub_table_idx = get_sub_table_from_hash(hash_value);
             level1_sub_tables[sub_table_idx].insert_unique_non_zero(cell, hash_value);


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