|
| 1 | +#include "benchmark/benchmark.h" |
| 2 | +#include "llvm/ADT/SmallString.h" |
| 3 | +#include "llvm/ADT/StringRef.h" |
| 4 | +#include "llvm/Support/MemoryBuffer.h" |
| 5 | +#include "llvm/Support/SpecialCaseList.h" |
| 6 | +#include "llvm/Support/raw_ostream.h" |
| 7 | +#include <cassert> |
| 8 | +#include <iterator> |
| 9 | +#include <random> |
| 10 | +#include <string> |
| 11 | +#include <utility> |
| 12 | +#include <vector> |
| 13 | + |
| 14 | +using namespace llvm; |
| 15 | + |
| 16 | +namespace { |
| 17 | +constexpr int RNG_SEED = 123456; |
| 18 | +constexpr int MAX_LIST_SIZE = 100000; |
| 19 | +constexpr int MAX_LIST_MUL = 1000; |
| 20 | + |
| 21 | +std::unique_ptr<SpecialCaseList> makeSpecialCaseList(StringRef List) { |
| 22 | + std::string Error; |
| 23 | + std::unique_ptr<MemoryBuffer> MB = MemoryBuffer::getMemBuffer(List); |
| 24 | + auto SCL = SpecialCaseList::create(MB.get(), Error); |
| 25 | + assert(SCL); |
| 26 | + assert(Error == ""); |
| 27 | + return SCL; |
| 28 | +} |
| 29 | + |
| 30 | +static const std::string Dictionary[] = { |
| 31 | + "orange", "tabby", "tortie", "tuxedo", "void", |
| 32 | + "multiple", "spaces", "cute", "fluffy", "kittens", |
| 33 | +}; |
| 34 | + |
| 35 | +std::vector<std::string> genFiles(size_t N) { |
| 36 | + std::vector<std::string> R; |
| 37 | + R.reserve(N); |
| 38 | + std::mt19937 Rng(RNG_SEED); |
| 39 | + std::uniform_int_distribution<> DepthDistrib(8, 16); |
| 40 | + std::uniform_int_distribution<> WordDistrib(0, std::size(Dictionary) - 1); |
| 41 | + |
| 42 | + std::string S; |
| 43 | + for (size_t i = 0; i < N; ++i) { |
| 44 | + for (size_t D = DepthDistrib(Rng); D; --D) { |
| 45 | + S += Dictionary[WordDistrib(Rng)]; |
| 46 | + if (D > 1) |
| 47 | + S += "/"; |
| 48 | + } |
| 49 | + R.push_back(std::move(S)); |
| 50 | + S.clear(); |
| 51 | + } |
| 52 | + return R; |
| 53 | +} |
| 54 | + |
| 55 | +std::string genFixedPath(const std::vector<std::string> &Files) { |
| 56 | + std::string S; |
| 57 | + for (const auto &F : Files) { |
| 58 | + S += "src:"; |
| 59 | + S += F; |
| 60 | + S += "\n"; |
| 61 | + } |
| 62 | + return S; |
| 63 | +} |
| 64 | + |
| 65 | +std::string genGlobMid(const std::vector<std::string> &Files) { |
| 66 | + std::string S; |
| 67 | + std::mt19937 Rng(RNG_SEED); |
| 68 | + for (std::string F : Files) { |
| 69 | + std::uniform_int_distribution<> PosDistrib(0, F.size() - 1); |
| 70 | + F[PosDistrib(Rng)] = '*'; |
| 71 | + S += "src:"; |
| 72 | + S += F; |
| 73 | + S += "\n"; |
| 74 | + } |
| 75 | + return S; |
| 76 | +} |
| 77 | + |
| 78 | +std::string genGlobStart(const std::vector<std::string> &Files) { |
| 79 | + std::string S; |
| 80 | + for (auto F : Files) { |
| 81 | + F.front() = '*'; |
| 82 | + S += "src:"; |
| 83 | + S += F; |
| 84 | + S += "\n"; |
| 85 | + } |
| 86 | + return S; |
| 87 | +} |
| 88 | + |
| 89 | +std::string genGlobEnd(const std::vector<std::string> &Files) { |
| 90 | + std::string S; |
| 91 | + for (auto F : Files) { |
| 92 | + F.back() = '*'; |
| 93 | + S += "src:"; |
| 94 | + S += F; |
| 95 | + S += "\n"; |
| 96 | + } |
| 97 | + return S; |
| 98 | +} |
| 99 | + |
| 100 | +std::string genGlobBoth(const std::vector<std::string> &Files) { |
| 101 | + std::string S; |
| 102 | + for (auto F : Files) { |
| 103 | + F.back() = '*'; |
| 104 | + F.front() = '*'; |
| 105 | + S += "src:"; |
| 106 | + S += F; |
| 107 | + S += "\n"; |
| 108 | + } |
| 109 | + return S; |
| 110 | +} |
| 111 | + |
| 112 | +void BM_Create( |
| 113 | + benchmark::State &state, |
| 114 | + std::string (*GenerateCaseList)(const std::vector<std::string> &Files)) { |
| 115 | + std::vector<std::string> BigFileList = genFiles(state.range(0)); |
| 116 | + std::string BigCaseList = GenerateCaseList(BigFileList); |
| 117 | + for (auto _ : state) { |
| 118 | + auto SCL = makeSpecialCaseList(BigCaseList); |
| 119 | + benchmark::DoNotOptimize(SCL); |
| 120 | + } |
| 121 | +} |
| 122 | +void BM_Pos( |
| 123 | + benchmark::State &state, |
| 124 | + std::string (*GenerateCaseList)(const std::vector<std::string> &Files)) { |
| 125 | + std::vector<std::string> BigFileList = genFiles(state.range(0)); |
| 126 | + std::string BigCaseList = GenerateCaseList(BigFileList); |
| 127 | + auto SCL = makeSpecialCaseList(BigCaseList); |
| 128 | + std::mt19937 Rng(RNG_SEED); |
| 129 | + std::uniform_int_distribution<> LineDistrib(0, BigFileList.size() - 1); |
| 130 | + for (auto _ : state) { |
| 131 | + auto &Q = BigFileList[LineDistrib(Rng)]; |
| 132 | + bool R = SCL->inSection("", "src", Q); |
| 133 | + if (!R) |
| 134 | + abort(); |
| 135 | + benchmark::DoNotOptimize(R); |
| 136 | + } |
| 137 | +} |
| 138 | + |
| 139 | +void BM_Neg( |
| 140 | + benchmark::State &state, |
| 141 | + std::string (*GenerateCaseList)(const std::vector<std::string> &Files)) { |
| 142 | + std::vector<std::string> BigFileList = genFiles(state.range(0)); |
| 143 | + std::string BigCaseList = GenerateCaseList(BigFileList); |
| 144 | + auto SCL = makeSpecialCaseList(BigCaseList); |
| 145 | + std::mt19937 Rng(RNG_SEED); |
| 146 | + std::uniform_int_distribution<> LineDistrib(0, BigFileList.size() - 1); |
| 147 | + for (auto _ : state) { |
| 148 | + std::string Q = BigFileList[LineDistrib(Rng)]; |
| 149 | + std::uniform_int_distribution<> PosDistrib(0, Q.size() - 1); |
| 150 | + Q[PosDistrib(Rng)] = '_'; |
| 151 | + bool R = SCL->inSection("", "src", Q); |
| 152 | + benchmark::DoNotOptimize(R); |
| 153 | + } |
| 154 | +} |
| 155 | + |
| 156 | +} // namespace |
| 157 | + |
| 158 | +BENCHMARK_CAPTURE(BM_Create, Exact, genFixedPath) |
| 159 | + ->RangeMultiplier(MAX_LIST_MUL) |
| 160 | + ->Range(1, MAX_LIST_SIZE); |
| 161 | +BENCHMARK_CAPTURE(BM_Create, Start, genGlobStart) |
| 162 | + ->RangeMultiplier(MAX_LIST_MUL) |
| 163 | + ->Range(1, MAX_LIST_SIZE); |
| 164 | +BENCHMARK_CAPTURE(BM_Create, End, genGlobEnd) |
| 165 | + ->RangeMultiplier(MAX_LIST_MUL) |
| 166 | + ->Range(1, MAX_LIST_SIZE); |
| 167 | +BENCHMARK_CAPTURE(BM_Create, Mid, genGlobMid) |
| 168 | + ->RangeMultiplier(MAX_LIST_MUL) |
| 169 | + ->Range(1, MAX_LIST_SIZE); |
| 170 | +BENCHMARK_CAPTURE(BM_Create, EndBoth, genGlobBoth) |
| 171 | + ->RangeMultiplier(MAX_LIST_MUL) |
| 172 | + ->Range(1, MAX_LIST_SIZE); |
| 173 | + |
| 174 | +BENCHMARK_CAPTURE(BM_Pos, Exact, genFixedPath) |
| 175 | + ->RangeMultiplier(MAX_LIST_MUL) |
| 176 | + ->Range(1, MAX_LIST_SIZE); |
| 177 | +BENCHMARK_CAPTURE(BM_Pos, Start, genGlobStart) |
| 178 | + ->RangeMultiplier(MAX_LIST_MUL) |
| 179 | + ->Range(1, MAX_LIST_SIZE); |
| 180 | +BENCHMARK_CAPTURE(BM_Pos, End, genGlobEnd) |
| 181 | + ->RangeMultiplier(MAX_LIST_MUL) |
| 182 | + ->Range(1, MAX_LIST_SIZE); |
| 183 | +BENCHMARK_CAPTURE(BM_Pos, Mid, genGlobMid) |
| 184 | + ->RangeMultiplier(MAX_LIST_MUL) |
| 185 | + ->Range(1, MAX_LIST_SIZE); |
| 186 | +BENCHMARK_CAPTURE(BM_Pos, Both, genGlobBoth) |
| 187 | + ->RangeMultiplier(MAX_LIST_MUL) |
| 188 | + ->Range(1, MAX_LIST_SIZE); |
| 189 | + |
| 190 | +BENCHMARK_CAPTURE(BM_Neg, Exact, genFixedPath) |
| 191 | + ->RangeMultiplier(MAX_LIST_MUL) |
| 192 | + ->Range(1, MAX_LIST_SIZE); |
| 193 | +BENCHMARK_CAPTURE(BM_Neg, Start, genGlobStart) |
| 194 | + ->RangeMultiplier(MAX_LIST_MUL) |
| 195 | + ->Range(1, MAX_LIST_SIZE); |
| 196 | +BENCHMARK_CAPTURE(BM_Neg, End, genGlobEnd) |
| 197 | + ->RangeMultiplier(MAX_LIST_MUL) |
| 198 | + ->Range(1, MAX_LIST_SIZE); |
| 199 | +BENCHMARK_CAPTURE(BM_Neg, Mid, genGlobMid) |
| 200 | + ->RangeMultiplier(MAX_LIST_MUL) |
| 201 | + ->Range(1, MAX_LIST_SIZE); |
| 202 | +BENCHMARK_CAPTURE(BM_Neg, End, genGlobBoth) |
| 203 | + ->RangeMultiplier(MAX_LIST_MUL) |
| 204 | + ->Range(1, MAX_LIST_SIZE); |
| 205 | + |
| 206 | +BENCHMARK_MAIN(); |
0 commit comments