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


##########
cpp/src/arrow/type_benchmark.cc:
##########
@@ -418,6 +421,114 @@ static void ErrorSchemeExceptionNoInline(
   state.SetItemsProcessed(state.iterations() * integers.size());
 }
 
+// ----------------------------------------------------------------------
+// FieldPath::Get benchmarks
+
+static std::shared_ptr<Schema> GenerateTestSchema(int num_columns) {
+  FieldVector fields(num_columns);
+  for (int i = 0; i < num_columns; ++i) {
+    auto name = std::string("f") + std::to_string(i);
+    fields[i] = field(std::move(name), int64());
+  }
+  return schema(std::move(fields));
+}
+
+static std::shared_ptr<Array> GenerateTestArray(int num_columns) {
+  constexpr int64_t kLength = 100;
+
+  auto rand = random::RandomArrayGenerator(0xbeef);
+  auto schm = GenerateTestSchema(num_columns);
+
+  ArrayVector columns(num_columns);
+  for (auto& column : columns) {
+    column = rand.Int64(kLength, 0, std::numeric_limits<int64_t>::max());
+  }
+
+  return *StructArray::Make(columns, schm->fields());
+}
+
+static std::shared_ptr<RecordBatch> ToBatch(const std::shared_ptr<Array>& 
array) {
+  return *RecordBatch::FromStructArray(array);
+}
+
+static std::shared_ptr<ChunkedArray> ToChunked(const std::shared_ptr<Array>& 
array,
+                                               double chunk_proportion = 1.0) {
+  auto struct_array = internal::checked_pointer_cast<StructArray>(array);
+  const auto num_rows = struct_array->length();
+  const auto chunk_length = static_cast<int64_t>(std::ceil(num_rows * 
chunk_proportion));
+
+  ArrayVector chunks;
+  for (int64_t offset = 0; offset < num_rows;) {
+    int64_t slice_length = std::min(chunk_length, num_rows - offset);
+    chunks.push_back(*struct_array->SliceSafe(offset, slice_length));
+    offset += slice_length;
+  }
+
+  return *ChunkedArray::Make(std::move(chunks));
+}
+
+static std::shared_ptr<Table> ToTable(const std::shared_ptr<Array>& array,
+                                      double chunk_proportion = 1.0) {
+  return *Table::FromChunkedStructArray(ToChunked(array, chunk_proportion));
+}
+
+template <typename T>
+static void BenchmarkFieldPathGet(benchmark::State& state,  // NOLINT 
non-const reference
+                                  const T& input, int num_columns) {
+  // Reassigning a single FieldPath var within each iteration's scope seems to 
be costly
+  // enough to influence the timings, so we preprocess them.
+  std::vector<FieldPath> paths(num_columns);
+  for (int i = 0; i < num_columns; ++i) {
+    paths[i] = {i};
+  }
+
+  for (auto _ : state) {
+    for (const auto& path : paths) {
+      benchmark::DoNotOptimize(path.Get(input));
+    }
+  }
+
+  state.SetItemsProcessed(state.iterations() * num_columns);

Review Comment:
   ```suggestion
     state.SetItemsProcessed(state.iterations() * num_columns);
     state.counters["num_columns"] = num_columns;
     if (num_chunks.has_value()) {
       state.counters["num_chunks"] = num_chunks.value();
     }
   ```



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