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


##########
cpp/src/arrow/chunk_resolver_benchmark.cc:
##########
@@ -0,0 +1,166 @@
+// 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 "benchmark/benchmark.h"
+
+#include <algorithm>
+#include <cstdint>
+#include <random>
+#include <vector>
+
+#include "arrow/chunk_resolver.h"
+#include "arrow/util/benchmark_util.h"
+#include "arrow/util/pcg_random.h"
+
+namespace arrow {
+
+using internal::ChunkResolver;
+using internal::TypedChunkLocation;
+
+namespace {
+
+int64_t constexpr kChunkedArrayLength = std::numeric_limits<uint16_t>::max();
+
+struct ResolveManyBenchmark {
+  benchmark::State& state;
+  random::pcg64 rng;
+  // Values from the state.range(i)
+  int64_t chunked_array_length;
+  int32_t num_chunks;
+  int64_t num_logical_indices;
+
+  explicit ResolveManyBenchmark(benchmark::State& state)
+      : state(state),
+        rng(42),
+        chunked_array_length(state.range(0)),
+        num_chunks(static_cast<int32_t>(state.range(1))),
+        num_logical_indices(state.range(2)) {}
+
+  std::vector<int64_t> GenChunkedArrayOffsets() {
+    std::uniform_int_distribution<int64_t> offset_gen(1, chunked_array_length);
+    std::vector<int64_t> offsets;
+    offsets.reserve(num_chunks + 1);
+    offsets.push_back(0);
+    while (offsets.size() < static_cast<size_t>(num_chunks)) {
+      offsets.push_back(offset_gen(rng));
+    }
+    offsets.push_back(chunked_array_length);
+    std::sort(offsets.begin() + 1, offsets.end());
+    return offsets;
+  }
+
+  template <typename IndexType>
+  std::vector<IndexType> GenRandomIndices(IndexType max_index, bool sorted) {
+    std::uniform_int_distribution<IndexType> index_gen(0, max_index);
+    std::vector<IndexType> indices;
+    indices.reserve(num_logical_indices);
+    while (indices.size() < static_cast<size_t>(num_logical_indices)) {
+      indices.push_back(index_gen(rng));
+    }
+    if (sorted) {
+      std::sort(indices.begin(), indices.end());
+    }
+    return indices;
+  }
+
+  template <typename IndexType>
+  void Bench(bool sorted) {
+    if constexpr (sizeof(IndexType) < 8) {
+      constexpr uint64_t kLimitIndex = std::numeric_limits<IndexType>::max();
+      ARROW_CHECK_LE(static_cast<uint64_t>(chunked_array_length), kLimitIndex);
+    }
+    const auto max_random_index = static_cast<IndexType>(chunked_array_length);
+    auto offsets = GenChunkedArrayOffsets();
+    auto logical_indices = GenRandomIndices<IndexType>(max_random_index, 
sorted);
+    ChunkResolver resolver(std::move(offsets));
+    std::vector<TypedChunkLocation<IndexType>> 
chunk_location_vec(num_logical_indices);
+    BENCHMARK_UNUSED bool all_succeeded = true;
+    for (auto _ : state) {
+      const bool success = resolver.ResolveMany<IndexType>(
+          num_logical_indices, logical_indices.data(), 
chunk_location_vec.data());
+      all_succeeded &= success;
+    }
+    ARROW_CHECK(all_succeeded);
+    state.counters["logical_len"] = static_cast<double>(chunked_array_length);
+    state.counters["num_chunks"] = static_cast<double>(num_chunks);
+    state.SetItemsProcessed(state.iterations() * num_logical_indices);
+  }
+};
+
+template <typename IndexType>
+void ResolveManySetArgs(benchmark::internal::Benchmark* bench) {
+  constexpr int32_t kNonAligned = 3;

Review Comment:
   Can you explain what this is for?



##########
cpp/src/arrow/chunk_resolver_benchmark.cc:
##########
@@ -0,0 +1,166 @@
+// 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 "benchmark/benchmark.h"
+
+#include <algorithm>
+#include <cstdint>
+#include <random>
+#include <vector>
+
+#include "arrow/chunk_resolver.h"
+#include "arrow/util/benchmark_util.h"
+#include "arrow/util/pcg_random.h"
+
+namespace arrow {
+
+using internal::ChunkResolver;
+using internal::TypedChunkLocation;
+
+namespace {
+
+int64_t constexpr kChunkedArrayLength = std::numeric_limits<uint16_t>::max();
+
+struct ResolveManyBenchmark {
+  benchmark::State& state;
+  random::pcg64 rng;
+  // Values from the state.range(i)
+  int64_t chunked_array_length;
+  int32_t num_chunks;
+  int64_t num_logical_indices;
+
+  explicit ResolveManyBenchmark(benchmark::State& state)
+      : state(state),
+        rng(42),
+        chunked_array_length(state.range(0)),
+        num_chunks(static_cast<int32_t>(state.range(1))),
+        num_logical_indices(state.range(2)) {}
+
+  std::vector<int64_t> GenChunkedArrayOffsets() {
+    std::uniform_int_distribution<int64_t> offset_gen(1, chunked_array_length);
+    std::vector<int64_t> offsets;
+    offsets.reserve(num_chunks + 1);
+    offsets.push_back(0);
+    while (offsets.size() < static_cast<size_t>(num_chunks)) {
+      offsets.push_back(offset_gen(rng));
+    }
+    offsets.push_back(chunked_array_length);
+    std::sort(offsets.begin() + 1, offsets.end());
+    return offsets;
+  }
+
+  template <typename IndexType>
+  std::vector<IndexType> GenRandomIndices(IndexType max_index, bool sorted) {
+    std::uniform_int_distribution<IndexType> index_gen(0, max_index);
+    std::vector<IndexType> indices;
+    indices.reserve(num_logical_indices);
+    while (indices.size() < static_cast<size_t>(num_logical_indices)) {
+      indices.push_back(index_gen(rng));
+    }
+    if (sorted) {
+      std::sort(indices.begin(), indices.end());
+    }
+    return indices;
+  }
+
+  template <typename IndexType>
+  void Bench(bool sorted) {
+    if constexpr (sizeof(IndexType) < 8) {
+      constexpr uint64_t kLimitIndex = std::numeric_limits<IndexType>::max();
+      ARROW_CHECK_LE(static_cast<uint64_t>(chunked_array_length), kLimitIndex);
+    }
+    const auto max_random_index = static_cast<IndexType>(chunked_array_length);
+    auto offsets = GenChunkedArrayOffsets();
+    auto logical_indices = GenRandomIndices<IndexType>(max_random_index, 
sorted);
+    ChunkResolver resolver(std::move(offsets));
+    std::vector<TypedChunkLocation<IndexType>> 
chunk_location_vec(num_logical_indices);
+    BENCHMARK_UNUSED bool all_succeeded = true;
+    for (auto _ : state) {
+      const bool success = resolver.ResolveMany<IndexType>(
+          num_logical_indices, logical_indices.data(), 
chunk_location_vec.data());
+      all_succeeded &= success;
+    }
+    ARROW_CHECK(all_succeeded);
+    state.counters["logical_len"] = static_cast<double>(chunked_array_length);
+    state.counters["num_chunks"] = static_cast<double>(num_chunks);
+    state.SetItemsProcessed(state.iterations() * num_logical_indices);
+  }
+};
+
+template <typename IndexType>
+void ResolveManySetArgs(benchmark::internal::Benchmark* bench) {
+  constexpr int32_t kNonAligned = 3;
+  const int64_t kNumIndicesFew = (kChunkedArrayLength >> 7) - kNonAligned;
+  const int64_t kNumIndicesMany = (kChunkedArrayLength >> 1) - kNonAligned;
+
+  switch (sizeof(IndexType)) {
+    case 1:
+      // Unexpected. See comments below.
+    case 2:
+    case 4:
+    case 8:
+      bench->Args({kChunkedArrayLength, /*num_chunks*/ 10000, kNumIndicesFew});

Review Comment:
   10000 chunks is really a lot and I'm not sure it's really useful to test 
different numbers of chunks. By accumulating different combinations of 
parameters we make the benchmark results less immediately readable.



##########
cpp/src/arrow/chunked_array_test.cc:
##########
@@ -373,10 +374,27 @@ TEST(TestChunkResolver, Resolve) {
   ASSERT_EQ(resolver.Resolve(10).chunk_index, 3);
 }
 
+template <class RNG>
+std::vector<int64_t> GenChunkedArrayOffsets(RNG& rng, int32_t num_chunks,
+                                            int64_t chunked_array_len) {
+  std::uniform_int_distribution<int64_t> offset_gen(1, chunked_array_len - 1);
+  std::vector<int64_t> offsets;
+  offsets.reserve(num_chunks + 1);
+  offsets.push_back(0);
+  while (offsets.size() < static_cast<size_t>(num_chunks)) {
+    offsets.push_back(offset_gen(rng));
+  }
+  offsets.push_back(chunked_array_len);
+  std::sort(offsets.begin() + 1, offsets.end());
+  return offsets;
+}
+
 template <typename T>
 class TestChunkResolverMany : public ::testing::Test {
  public:
   using IndexType = T;
+  static constexpr int32_t kMaxInt32 = std::numeric_limits<int32_t>::max();
+  static constexpr size_t kMaxValidIndex = 
std::numeric_limits<IndexType>::max();

Review Comment:
   Shouldn't this be `uint64_t`? Especially for 32-bit platforms.



##########
cpp/src/arrow/chunked_array.cc:
##########
@@ -55,7 +55,7 @@ ChunkedArray::ChunkedArray(ArrayVector chunks, 
std::shared_ptr<DataType> type)
         << "cannot construct ChunkedArray from empty vector and omitted type";
     type_ = chunks_[0]->type();
   }
-
+  ARROW_CHECK_LE(chunks.size(), std::numeric_limits<int>::max());

Review Comment:
   Is this limit useful if it ends up not making performance better anyway?



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