ZhangHuiGui commented on code in PR #41036: URL: https://github.com/apache/arrow/pull/41036#discussion_r1563700270
########## cpp/src/arrow/compute/row/grouper_benchmark.cc: ########## @@ -0,0 +1,127 @@ +// 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 "arrow/util/key_value_metadata.h" +#include "arrow/util/string.h" +#include "benchmark/benchmark.h" + +#include "arrow/compute/row/grouper.h" +#include "arrow/testing/gtest_util.h" +#include "arrow/testing/random.h" +#include "arrow/util/benchmark_util.h" + +namespace arrow { +namespace compute { + +constexpr auto kSeed = 0x0ff1ce; +constexpr int64_t kRound = 256; + +static ExecBatch MakeRandomExecBatch(const DataTypeVector& types, int64_t num_rows, + double null_probability, + int64_t alignment = kDefaultBufferAlignment, + MemoryPool* memory_pool = nullptr) { + random::RandomArrayGenerator rng(kSeed); + auto num_types = static_cast<int>(types.size()); + + // clang-format off + auto metadata = key_value_metadata( + { + "null_probability", + "true_probability", + "unique" + }, + { + internal::ToChars(null_probability), + internal::ToChars(null_probability), // for boolean type + internal::ToChars(static_cast<int32_t>(num_rows * 0.5)) // for string type + }); + // clang-format on + + std::vector<Datum> values; + values.resize(num_types); + for (int i = 0; i < num_types; ++i) { + auto field = ::arrow::field("", types[i], metadata); + values[i] = rng.ArrayOf(*field, num_rows, alignment, memory_pool); + } + + return ExecBatch(std::move(values), num_rows); +} + +static void GrouperBenchmark(benchmark::State& state, const ExecSpan& span, + ExecContext* ctx = nullptr) { + for (auto _ : state) { + ASSIGN_OR_ABORT(auto grouper, Grouper::Make(span.GetTypes(), ctx)); + for (int i = 0; i < kRound; ++i) { + ASSIGN_OR_ABORT(auto group_ids, grouper->Consume(span)); + } + } + + state.SetItemsProcessed(state.iterations() * kRound * span.length); +} + +static void GrouperWithMultiTypes(benchmark::State& state, const DataTypeVector& types) { + auto ctx = default_exec_context(); + + RegressionArgs args(state, false); + const int64_t num_rows = args.size; + const double null_proportion = args.null_proportion; + + auto exec_batch = MakeRandomExecBatch(types, num_rows, null_proportion, + kDefaultBufferAlignment, ctx->memory_pool()); + ExecSpan exec_span(exec_batch); + ASSIGN_OR_ABORT(auto grouper, Grouper::Make(exec_span.GetTypes(), ctx)); + GrouperBenchmark(state, exec_span, ctx); +} + +void SetArgs(benchmark::internal::Benchmark* bench) { + BenchmarkSetArgsWithSizes(bench, {1 << 10, 1 << 12}); +} + +// basic type column +BENCHMARK_CAPTURE(GrouperWithMultiTypes, "{boolean}", {boolean()})->Apply(SetArgs); +BENCHMARK_CAPTURE(GrouperWithMultiTypes, "{int32}", {int32()})->Apply(SetArgs); +BENCHMARK_CAPTURE(GrouperWithMultiTypes, "{int64}", {int64()})->Apply(SetArgs); +BENCHMARK_CAPTURE(GrouperWithMultiTypes, "{utf8}", {utf8()})->Apply(SetArgs); +BENCHMARK_CAPTURE(GrouperWithMultiTypes, "{fixed_size_binary(128)}", + {fixed_size_binary(128)}) + ->Apply(SetArgs); + +// multi types' columns +BENCHMARK_CAPTURE(GrouperWithMultiTypes, "{boolean, utf8}", {boolean(), utf8()}) + ->Apply(SetArgs); +BENCHMARK_CAPTURE(GrouperWithMultiTypes, "{int32, int32}", {int32(), int32()}) + ->Apply(SetArgs); +BENCHMARK_CAPTURE(GrouperWithMultiTypes, "{int32, int64}", {int32(), int64()}) + ->Apply(SetArgs); +BENCHMARK_CAPTURE(GrouperWithMultiTypes, "{boolean, int64, utf8}", + {boolean(), int64(), utf8()}) + ->Apply(SetArgs); + +// multi types' columns with column resorted Review Comment: Thanks to `num_groups` in the benchmark, I discovered a very interesting performance phenomenon. When setting `are_cols_in_encoding_order` in `KeyCompare::CompareColumnsToRows` in Grouper to false, using the same way of generating data sets we will Generate much more groups, and the performance with a larger number of groups will obviously be worse (because the grouper needs to build and find swisstable). So the essence of the final performance problem is: Why does the number of groupings in the same data set increase significantly after resort column? Further analysis is needed. ```shell # origin-order, produce much more groups than resorted order GrouperWithMultiTypes/"{int32, int64}"/8388608/0 45000851 us 44976124 us 1 items_per_second=186.512k/s null_percent=0 num_groups=134.218M size=8.38861M uniqueness=1 GrouperWithMultiTypes/"{int32, int64}"/8388608/0 43829079 us 43799112 us 1 items_per_second=191.525k/s null_percent=0 num_groups=134.218M size=8.38861M uniqueness=1 # resorted order GrouperWithMultiTypes/"{int64, int32}"/8388608/0 4955565 us 4952519 us 1 items_per_second=1.69381M/s null_percent=0 num_groups=8.38861M size=8.38861M uniqueness=0.0625 GrouperWithMultiTypes/"{int64, int32}"/8388608/0 5004994 us 5001648 us 1 items_per_second=1.67717M/s null_percent=0 num_groups=8.38861M size=8.38861M uniqueness=0.0625 ``` -- 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]
