pitrou commented on code in PR #47515:
URL: https://github.com/apache/arrow/pull/47515#discussion_r2349262442


##########
cpp/src/arrow/util/bpacking_benchmark.cc:
##########
@@ -0,0 +1,162 @@
+// 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 <stdexcept>
+#include <vector>
+
+#include <benchmark/benchmark.h>
+
+#include "arrow/testing/util.h"
+#include "arrow/util/bpacking_internal.h"
+
+#if defined(ARROW_HAVE_RUNTIME_AVX2)
+#  include "arrow/util/bpacking_avx2_internal.h"
+#  include "arrow/util/cpu_info.h"
+#endif
+#if defined(ARROW_HAVE_RUNTIME_AVX512)
+#  include "arrow/util/bpacking_avx512_internal.h"
+#endif
+#if defined(ARROW_HAVE_NEON)
+#  include "arrow/util/bpacking_neon_internal.h"
+#endif
+
+namespace arrow::internal {
+namespace {
+
+template <typename Int>
+using UnpackFunc = int (*)(const uint8_t*, Int*, int, int);
+
+/// Get the number of bytes associate with a packing.
+constexpr int32_t GetNumBytes(int32_t num_values, int32_t bit_width) {
+  auto const num_bits = num_values * bit_width;
+  if (num_bits % 8 != 0) {
+    throw std::invalid_argument("Must pack a multiple of 8 bits.");
+  }
+  return num_bits / 8;
+}
+
+/// Generate random bytes as packed integers.
+std::vector<uint8_t> GenerateRandomPackedValues(int32_t num_values, int32_t 
bit_width) {
+  constexpr uint32_t kSeed = 3214;
+  auto const num_bytes = GetNumBytes(num_values, bit_width);
+
+  std::vector<uint8_t> out(num_bytes);
+  random_bytes(num_bytes, kSeed, out.data());
+
+  return out;
+}
+
+const uint8_t* GetNextAlignedByte(const uint8_t* ptr, std::size_t alignment) {
+  auto addr = reinterpret_cast<std::uintptr_t>(ptr);
+
+  if (addr % alignment == 0) {
+    return ptr;
+  }
+
+  auto remainder = addr % alignment;
+  auto bytes_to_add = alignment - remainder;
+
+  return ptr + bytes_to_add;
+}
+
+template <typename Int>
+void BM_Unpack(benchmark::State& state, bool aligned, UnpackFunc<Int> unpack, 
bool skip,
+               std::string skip_msg) {
+  if (skip) {
+    state.SkipWithMessage(skip_msg);
+  }
+
+  auto const bit_width = static_cast<int32_t>(state.range(0));
+  auto const num_values = static_cast<int32_t>(state.range(1));
+
+  // Assume std::vector allocation is likely be aligned for greater than a 
byte.
+  // So we allocate more values than necessary and skip to the next byte with 
the
+  // desired (non) alignment to test the proper condition.
+  constexpr int32_t kExtraValues = sizeof(Int) * 8;
+  auto const packed = GenerateRandomPackedValues(num_values + kExtraValues, 
bit_width);
+  const uint8_t* packed_ptr =
+      GetNextAlignedByte(packed.data(), sizeof(Int)) + (aligned ? 0 : 1);
+
+  std::vector<Int> unpacked(num_values, 0);
+
+  for (auto _ : state) {
+    unpack(packed_ptr, unpacked.data(), num_values, bit_width);
+    benchmark::ClobberMemory();
+  }
+  state.SetItemsProcessed(num_values * state.iterations());
+}
+
+constexpr int32_t kMinRange = 64;
+constexpr int32_t kMaxRange = 32768;
+constexpr std::initializer_list<int64_t> kBitWidths32 = {1, 2, 8, 20};
+constexpr std::initializer_list<int64_t> kBitWidths64 = {1, 2, 8, 20, 47};
+static const std::vector<std::vector<int64_t>> bitWidthsNumValues32 = {
+    kBitWidths32,
+    benchmark::CreateRange(kMinRange, kMaxRange, /*multi=*/32),
+};
+static const std::vector<std::vector<int64_t>> bitWidthsNumValues64 = {
+    kBitWidths64,
+    benchmark::CreateRange(kMinRange, kMaxRange, /*multi=*/32),
+};
+
+/// Nudge for MSVC template inside BENCHMARK_CAPTURE macro.
+void BM_UnpackUint32(benchmark::State& state, bool aligned, 
UnpackFunc<uint32_t> unpack,
+                     bool skip = false, std::string skip_msg = "") {
+  return BM_Unpack<uint32_t>(state, aligned, unpack, skip, 
std::move(skip_msg));
+}
+/// Nudge for MSVC template inside BENCHMARK_CAPTURE macro.
+void BM_UnpackUint64(benchmark::State& state, bool aligned, 
UnpackFunc<uint64_t> unpack,
+                     bool skip = false, std::string skip_msg = "") {
+  return BM_Unpack<uint64_t>(state, aligned, unpack, skip, 
std::move(skip_msg));
+}
+
+BENCHMARK_CAPTURE(BM_UnpackUint32, unpack32_default_unaligned, false, 
unpack32_default)
+    ->ArgsProduct(bitWidthsNumValues32);

Review Comment:
   Nitpicky, but a suggestion to make naming more consistent and more 
informative.
   ```suggestion
   BENCHMARK_CAPTURE(BM_UnpackUint32, ScalarUnaligned, false, unpack32_default)
       ->ArgsProduct(bitWidthsNumValues32);
   ```



-- 
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: github-unsubscr...@arrow.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to