cyb70289 commented on a change in pull request #7963:
URL: https://github.com/apache/arrow/pull/7963#discussion_r474658427



##########
File path: cpp/src/arrow/compute/kernels/aggregate_mode.cc
##########
@@ -69,28 +72,83 @@ struct ModeState {
   std::unordered_map<T, int64_t> value_counts{};
 };
 
+// Use array to count small integers(bool, int8, uint8), improves performance 
2x ~ 6x
+template <typename ArrowType>
+struct ModeState<ArrowType, enable_if_t<(sizeof(typename ArrowType::c_type) == 
1)>> {
+  using ThisType = ModeState<ArrowType>;
+  using T = typename ArrowType::c_type;
+  using Limits = std::numeric_limits<T>;
+
+  ModeState() : value_counts(Limits::max() - Limits::min() + 1) {}
+
+  void MergeFrom(const ThisType& state) {
+    std::transform(this->value_counts.cbegin(), this->value_counts.cend(),
+                   state.value_counts.cbegin(), this->value_counts.begin(),
+                   std::plus<int64_t>{});
+  }
+
+  void MergeOne(T value) { ++this->value_counts[value - Limits::min()]; }
+
+  std::pair<T, int64_t> Finalize() {
+    T mode = Limits::min();
+    int64_t count = 0;
+
+    for (int i = 0; i <= Limits::max() - Limits::min(); ++i) {
+      T this_value = static_cast<T>(i + Limits::min());
+      int64_t this_count = this->value_counts[i];
+      if (this_count > count || (this_count == count && this_value < mode)) {
+        count = this_count;
+        mode = this_value;
+      }
+    }
+    return std::make_pair(mode, count);
+  }
+
+  std::vector<int64_t> value_counts;
+};
+
+// read raw_values[] directly improves performance ~15%

Review comment:
       Agreed. Will remove it.




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to