|
| 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/container/F14Set.h> |
| 18 | +#include <folly/init/Init.h> |
| 19 | +#include "velox/expression/tests/VectorFuzzer.h" |
| 20 | +#include "velox/functions/lib/benchmarks/FunctionBenchmarkBase.h" |
| 21 | +#include "velox/functions/prestosql/VectorFunctions.h" |
| 22 | + |
| 23 | +using namespace facebook::velox; |
| 24 | +using namespace facebook::velox::exec; |
| 25 | +using namespace facebook::velox::test; |
| 26 | + |
| 27 | +namespace { |
| 28 | + |
| 29 | +/// Fast implementation of IN (a, b, c,..) using F14FastSet. |
| 30 | +VectorPtr fastIn( |
| 31 | + const folly::F14FastSet<int32_t>& inSet, |
| 32 | + const VectorPtr& data) { |
| 33 | + const auto numRows = data->size(); |
| 34 | + auto result = std::static_pointer_cast<FlatVector<bool>>( |
| 35 | + BaseVector::create(BOOLEAN(), numRows, data->pool())); |
| 36 | + auto rawResults = result->mutableRawValues<int32_t>(); |
| 37 | + |
| 38 | + auto rawData = data->asUnchecked<FlatVector<int32_t>>()->rawValues(); |
| 39 | + for (auto row = 0; row < numRows; ++row) { |
| 40 | + bits::setBit(rawResults, row, inSet.contains(rawData[row])); |
| 41 | + } |
| 42 | + |
| 43 | + return result; |
| 44 | +} |
| 45 | + |
| 46 | +class InBenchmark : public functions::test::FunctionBenchmarkBase { |
| 47 | + public: |
| 48 | + InBenchmark() : FunctionBenchmarkBase() { |
| 49 | + functions::registerVectorFunctions(); |
| 50 | + } |
| 51 | + |
| 52 | + RowVectorPtr makeData() { |
| 53 | + VectorFuzzer::Options opts; |
| 54 | + opts.vectorSize = 1'000; |
| 55 | + return vectorMaker_.rowVector( |
| 56 | + {VectorFuzzer(opts, pool()).fuzzFlat(INTEGER())}); |
| 57 | + } |
| 58 | + |
| 59 | + void run(size_t numValues) { |
| 60 | + folly::BenchmarkSuspender suspender; |
| 61 | + auto data = makeData(); |
| 62 | + |
| 63 | + std::ostringstream inList; |
| 64 | + inList << "0"; |
| 65 | + for (auto i = 1; i < numValues; ++i) { |
| 66 | + inList << ", " << i * 2; |
| 67 | + } |
| 68 | + |
| 69 | + auto sql = fmt::format("c0 IN ({})", inList.str()); |
| 70 | + auto exprSet = compileExpression(sql, data->type()); |
| 71 | + suspender.dismiss(); |
| 72 | + |
| 73 | + doRun(exprSet, data); |
| 74 | + } |
| 75 | + |
| 76 | + void doRun(ExprSet& exprSet, const RowVectorPtr& rowVector) { |
| 77 | + int cnt = 0; |
| 78 | + for (auto i = 0; i < 1000; i++) { |
| 79 | + cnt += evaluate(exprSet, rowVector)->size(); |
| 80 | + } |
| 81 | + folly::doNotOptimizeAway(cnt); |
| 82 | + } |
| 83 | + |
| 84 | + void runFast(size_t numValues) { |
| 85 | + folly::BenchmarkSuspender suspender; |
| 86 | + auto data = makeData(); |
| 87 | + |
| 88 | + folly::F14FastSet<int32_t> inSet; |
| 89 | + inSet.reserve(numValues); |
| 90 | + for (auto i = 0; i < numValues; ++i) { |
| 91 | + inSet.insert(i); |
| 92 | + } |
| 93 | + suspender.dismiss(); |
| 94 | + |
| 95 | + doRunFast(inSet, data->childAt(0)); |
| 96 | + } |
| 97 | + |
| 98 | + void doRunFast( |
| 99 | + const folly::F14FastSet<int32_t>& inSet, |
| 100 | + const VectorPtr& data) { |
| 101 | + int cnt = 0; |
| 102 | + for (auto i = 0; i < 1000; i++) { |
| 103 | + cnt += fastIn(inSet, data)->size(); |
| 104 | + } |
| 105 | + folly::doNotOptimizeAway(cnt); |
| 106 | + } |
| 107 | +}; |
| 108 | + |
| 109 | +BENCHMARK(fastIn) { |
| 110 | + InBenchmark benchmark; |
| 111 | + benchmark.runFast(10); |
| 112 | +} |
| 113 | + |
| 114 | +BENCHMARK_RELATIVE(in) { |
| 115 | + InBenchmark benchmark; |
| 116 | + benchmark.run(10); |
| 117 | +} |
| 118 | + |
| 119 | +BENCHMARK(fastIn1K) { |
| 120 | + InBenchmark benchmark; |
| 121 | + benchmark.runFast(1'000); |
| 122 | +} |
| 123 | + |
| 124 | +BENCHMARK_RELATIVE(in1K) { |
| 125 | + InBenchmark benchmark; |
| 126 | + benchmark.run(1'000); |
| 127 | +} |
| 128 | + |
| 129 | +} // namespace |
| 130 | + |
| 131 | +int main(int /*argc*/, char** /*argv*/) { |
| 132 | + folly::runBenchmarks(); |
| 133 | + return 0; |
| 134 | +} |
0 commit comments