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


##########
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()));
+  std::vector<compute::KeyColumnArray> columns{input_keycol};
+
+  // run the benchmark
+  while (state.KeepRunning()) {
+    compute::Hashing32::HashMultiColumn(
+        columns, &hash_ctx, 
reinterpret_cast<uint32_t*>(hash_buffer->mutable_data()));
+  }

Review Comment:
   In this benchmark, the hash output is never consumed, so the compiler may be 
able to optimize away some/all of the work (especially with inlining/LTO), 
making the timing misleading. Add a benchmark::DoNotOptimize / ClobberMemory on 
the output buffer inside the loop (and apply the same change to 
KeyHashIntegers64 below).



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