kszucs commented on code in PR #45001:
URL: https://github.com/apache/arrow/pull/45001#discussion_r3645171898


##########
cpp/src/arrow/compute/kernels/scalar_hash_benchmark.cc:
##########
@@ -0,0 +1,235 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+#include <algorithm>
+#include <cstdint>
+#include <limits>
+#include <random>
+#include <string>
+#include <vector>
+
+#include "benchmark/benchmark.h"
+
+#include "arrow/testing/gtest_util.h"
+#include "arrow/testing/random.h"
+#include "arrow/util/hashing.h"
+
+#include "arrow/array/array_nested.h"
+#include "arrow/compute/exec.h"
+
+namespace arrow {
+namespace internal {
+
+// ------------------------------
+// Anonymous namespace with global params
+
+namespace {
+// copied from scalar_string_benchmark
+constexpr auto kSeed = 0x94378165;
+constexpr double null_prob = 0.2;
+
+static random::RandomArrayGenerator hashing_rng(kSeed);
+}  // namespace
+
+// ------------------------------
+// Convenience functions
+
+static Result<std::shared_ptr<StructArray>> MakeStructArray(int64_t n_values,
+                                                            int32_t min_strlen,
+                                                            int32_t 
max_strlen) {
+  auto vals_first = hashing_rng.Int64(n_values, 0, 
std::numeric_limits<int64_t>::max());
+  auto vals_second = hashing_rng.String(n_values, min_strlen, max_strlen, 
null_prob);
+  auto vals_third = hashing_rng.Int64(n_values, 0, 
std::numeric_limits<int64_t>::max());
+
+  return arrow::StructArray::Make(
+      arrow::ArrayVector{vals_first, vals_second, vals_third},
+      arrow::FieldVector{arrow::field("first", arrow::int64()),
+                         arrow::field("second", arrow::utf8()),
+                         arrow::field("third", arrow::int64())});
+}
+
+// ------------------------------
+// Benchmark implementations
+
+static void Hash64Int64(benchmark::State& state) {  // NOLINT non-const 
reference
+  auto test_vals = hashing_rng.Int64(10000, 0, 
std::numeric_limits<int64_t>::max());
+
+  while (state.KeepRunning()) {
+    ASSERT_OK_AND_ASSIGN(Datum hash_result, compute::CallFunction("hash64", 
{test_vals}));
+    benchmark::DoNotOptimize(hash_result);
+  }

Review Comment:
   Fixed — switched all hot-loop call sites in this file to 
`compute::CallFunction(...).ValueOrDie()`, matching other Arrow benchmarks that 
avoid gtest assertion machinery inside `while (state.KeepRunning())`.



##########
cpp/src/arrow/compute/key_hash_benchmark.cc:
##########
@@ -0,0 +1,119 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+#include <algorithm>
+#include <cstdint>
+#include <limits>
+#include <random>
+#include <string>
+#include <vector>
+
+#include "benchmark/benchmark.h"
+
+#include "arrow/testing/gtest_util.h"
+#include "arrow/testing/random.h"
+#include "arrow/util/hashing.h"
+
+#include "arrow/array/builder_primitive.h"
+#include "arrow/compute/key_hash_internal.h"
+#include "arrow/compute/util_internal.h"
+
+namespace arrow {
+namespace internal {
+
+namespace {
+// copied from scalar_string_benchmark
+constexpr auto kSeed = 0x94378165;
+
+static random::RandomArrayGenerator hashing_rng(kSeed);
+}  // namespace
+
+static void KeyHashIntegers32(benchmark::State& state) {  // NOLINT non-const 
reference
+  auto test_vals = hashing_rng.Int32(10000, 0, 
std::numeric_limits<int32_t>::max());
+
+  // initialize the stack allocator
+  util::TempVectorStack stack_memallocator;
+  
ASSERT_OK(stack_memallocator.Init(compute::default_exec_context()->memory_pool(),
+                                    
compute::Hashing32::kHashBatchTempStackUsage));
+
+  // prepare the execution context for Hashing32
+  compute::LightContext hash_ctx;
+  hash_ctx.hardware_flags = 
compute::default_exec_context()->cpu_info()->hardware_flags();
+  hash_ctx.stack = &stack_memallocator;
+
+  // allocate memory for results
+  ASSERT_OK_AND_ASSIGN(std::unique_ptr<Buffer> hash_buffer,
+                       AllocateBuffer(test_vals->length() * sizeof(int32_t)));
+
+  // Prepare input data structure for propagation to hash function
+  ASSERT_OK_AND_ASSIGN(
+      compute::KeyColumnArray input_keycol,
+      compute::ColumnArrayFromArrayData(test_vals->data(), 0, 
test_vals->length()));
+
+  // run the benchmark
+  while (state.KeepRunning()) {
+    compute::Hashing32::HashMultiColumn(
+        {input_keycol}, &hash_ctx,
+        reinterpret_cast<uint32_t*>(hash_buffer->mutable_data()));
+  }

Review Comment:
   Fixed — hoisted `std::vector<KeyColumnArray> columns{input_keycol};` out of 
both benchmark loops (KeyHashIntegers32 and KeyHashIntegers64), reusing it 
across iterations.



-- 
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.

To unsubscribe, e-mail: [email protected]

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

Reply via email to