|
| 1 | +/* |
| 2 | + * Copyright (c) Facebook, Inc. and its affiliates. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +#include <folly/Benchmark.h> |
| 17 | +#include <folly/init/Init.h> |
| 18 | + |
| 19 | +#include "velox/common/memory/Memory.h" |
| 20 | +#include "velox/exec/SetAccumulator.h" |
| 21 | +#include "velox/vector/fuzzer/VectorFuzzer.h" |
| 22 | +#include "velox/vector/tests/utils/VectorTestBase.h" |
| 23 | + |
| 24 | +using namespace facebook::velox; |
| 25 | +using namespace facebook::velox::exec; |
| 26 | + |
| 27 | +namespace { |
| 28 | + |
| 29 | +// Adds 10M mostly unique values to a single SetAccumulator, then extracts |
| 30 | +// unique values from it. |
| 31 | +class SetAccumulatorBenchmark : public facebook::velox::test::VectorTestBase { |
| 32 | + public: |
| 33 | + void setup() { |
| 34 | + VectorFuzzer::Options opts; |
| 35 | + opts.vectorSize = 1'000'000; |
| 36 | + VectorFuzzer fuzzer(opts, pool()); |
| 37 | + |
| 38 | + auto rowType = ROW({"a", "b", "c"}, {BIGINT(), BIGINT(), VARCHAR()}); |
| 39 | + for (auto i = 0; i < 10; ++i) { |
| 40 | + rowVectors_.emplace_back(fuzzer.fuzzInputRow(rowType)); |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + void runBigint() { |
| 45 | + runPrimitive<int64_t>("a"); |
| 46 | + } |
| 47 | + |
| 48 | + void runVarchar() { |
| 49 | + runPrimitive<StringView>("c"); |
| 50 | + } |
| 51 | + |
| 52 | + void runTwoBigints() { |
| 53 | + HashStringAllocator allocator(pool()); |
| 54 | + const TypePtr type = ROW({BIGINT(), BIGINT()}); |
| 55 | + aggregate::prestosql::SetAccumulator<ComplexType> accumulator( |
| 56 | + type, &allocator); |
| 57 | + |
| 58 | + for (const auto& rowVector : rowVectors_) { |
| 59 | + auto vector = |
| 60 | + makeRowVector({rowVector->childAt("a"), rowVector->childAt("b")}); |
| 61 | + DecodedVector decoded(*vector); |
| 62 | + for (auto i = 0; i < rowVector->size(); ++i) { |
| 63 | + accumulator.addValue(decoded, i, &allocator); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + auto result = BaseVector::create(type, accumulator.size(), pool()); |
| 68 | + accumulator.extractValues(*result, 0); |
| 69 | + folly::doNotOptimizeAway(result); |
| 70 | + } |
| 71 | + |
| 72 | + private: |
| 73 | + template <typename T> |
| 74 | + void runPrimitive(const std::string& name) { |
| 75 | + const auto& type = rowVectors_[0]->childAt(name)->type(); |
| 76 | + |
| 77 | + HashStringAllocator allocator(pool()); |
| 78 | + aggregate::prestosql::SetAccumulator<T> accumulator(type, &allocator); |
| 79 | + |
| 80 | + for (const auto& rowVector : rowVectors_) { |
| 81 | + DecodedVector decoded(*rowVector->childAt(name)); |
| 82 | + for (auto i = 0; i < rowVector->size(); ++i) { |
| 83 | + accumulator.addValue(decoded, i, &allocator); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + auto result = |
| 88 | + BaseVector::create<FlatVector<T>>(type, accumulator.size(), pool()); |
| 89 | + accumulator.extractValues(*result, 0); |
| 90 | + folly::doNotOptimizeAway(result); |
| 91 | + } |
| 92 | + |
| 93 | + std::vector<RowVectorPtr> rowVectors_; |
| 94 | +}; |
| 95 | + |
| 96 | +std::unique_ptr<SetAccumulatorBenchmark> bm; |
| 97 | + |
| 98 | +BENCHMARK(bigint) { |
| 99 | + bm->runBigint(); |
| 100 | +} |
| 101 | + |
| 102 | +BENCHMARK(varchar) { |
| 103 | + bm->runVarchar(); |
| 104 | +} |
| 105 | + |
| 106 | +BENCHMARK(twoBigints) { |
| 107 | + bm->runTwoBigints(); |
| 108 | +} |
| 109 | + |
| 110 | +} // namespace |
| 111 | + |
| 112 | +int main(int argc, char** argv) { |
| 113 | + folly::init(&argc, &argv); |
| 114 | + memory::MemoryManager::initialize({}); |
| 115 | + |
| 116 | + bm = std::make_unique<SetAccumulatorBenchmark>(); |
| 117 | + bm->setup(); |
| 118 | + |
| 119 | + folly::runBenchmarks(); |
| 120 | + |
| 121 | + bm.reset(); |
| 122 | + |
| 123 | + return 0; |
| 124 | +} |
0 commit comments