You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by GitBox <gi...@apache.org> on 2020/08/20 17:17:41 UTC

[GitHub] [arrow] pitrou commented on a change in pull request #7963: ARROW-9699: [C++][Compute] Optimize mode kernel for small integer types

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



##########
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:
       This is probably heavily compiler-dependent. In any case, such micro-optimizations mostly complicate the code for no real benefit.

##########
File path: cpp/src/arrow/compute/kernels/aggregate_test.cc
##########
@@ -685,5 +687,10 @@ TYPED_TEST(TestFloatingModeKernel, Floats) {
   this->AssertModeIsNull("[]");
 }
 
+TEST_F(TestInt8ModeKernelValueRange, Basics) {

Review comment:
       Are these tests useful? `int8` should already be tested in `TestIntegerModeKernel`, right?




----------------------------------------------------------------
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:
users@infra.apache.org