-
Notifications
You must be signed in to change notification settings - Fork 14.9k
[NFC][SpecialCaseList] Add SpecialCaseList Benchmark #163274
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
vitalybuka
merged 5 commits into
main
from
users/vitalybuka/spr/nfc-specialcaselist-benchmark
Oct 14, 2025
Merged
Changes from 4 commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,207 @@ | ||
#include "benchmark/benchmark.h" | ||
#include "llvm/ADT/SmallString.h" | ||
#include "llvm/ADT/StringRef.h" | ||
#include "llvm/Support/MemoryBuffer.h" | ||
#include "llvm/Support/SpecialCaseList.h" | ||
#include "llvm/Support/raw_ostream.h" | ||
#include <cassert> | ||
#include <iterator> | ||
#include <random> | ||
#include <string> | ||
#include <utility> | ||
#include <vector> | ||
|
||
using namespace llvm; | ||
|
||
namespace { | ||
constexpr int RNG_SEED = 123456; | ||
constexpr int MAX_LIST_MIN = 10; | ||
constexpr int MAX_LIST_MAX = 1000000; | ||
constexpr int MAX_LIST_MUL = 10; | ||
|
||
std::unique_ptr<SpecialCaseList> makeSpecialCaseList(StringRef List) { | ||
std::string Error; | ||
std::unique_ptr<MemoryBuffer> MB = MemoryBuffer::getMemBuffer(List); | ||
auto SCL = SpecialCaseList::create(MB.get(), Error); | ||
assert(SCL); | ||
assert(Error == ""); | ||
return SCL; | ||
} | ||
|
||
static const std::string Dictionary[] = { | ||
"orange", "tabby", "tortie", "tuxedo", "void", | ||
"multiple", "spaces", "cute", "fluffy", "kittens", | ||
}; | ||
|
||
std::vector<std::string> genFiles(size_t N) { | ||
std::vector<std::string> R; | ||
R.reserve(N); | ||
std::mt19937 Rng(RNG_SEED); | ||
std::uniform_int_distribution<> DepthDistrib(8, 16); | ||
std::uniform_int_distribution<> WordDistrib(0, std::size(Dictionary) - 1); | ||
|
||
std::string S; | ||
for (size_t i = 0; i < N; ++i) { | ||
for (size_t D = DepthDistrib(Rng); D; --D) { | ||
S += Dictionary[WordDistrib(Rng)]; | ||
if (D > 1) | ||
S += "/"; | ||
} | ||
R.push_back(std::move(S)); | ||
S.clear(); | ||
} | ||
return R; | ||
} | ||
|
||
std::string genFixed(const std::vector<std::string> &Files) { | ||
std::string S; | ||
for (const auto &F : Files) { | ||
S += "src:"; | ||
S += F; | ||
S += "\n"; | ||
} | ||
return S; | ||
} | ||
|
||
std::string genMid(const std::vector<std::string> &Files) { | ||
vitalybuka marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
std::string S; | ||
std::mt19937 Rng(RNG_SEED); | ||
for (std::string F : Files) { | ||
std::uniform_int_distribution<> PosDistrib(0, F.size() - 1); | ||
F[PosDistrib(Rng)] = '*'; | ||
S += "src:"; | ||
S += F; | ||
S += "\n"; | ||
} | ||
return S; | ||
} | ||
|
||
std::string genStart(const std::vector<std::string> &Files) { | ||
std::string S; | ||
for (auto F : Files) { | ||
F.front() = '*'; | ||
S += "src:"; | ||
S += F; | ||
S += "\n"; | ||
} | ||
return S; | ||
} | ||
|
||
std::string genEnd(const std::vector<std::string> &Files) { | ||
std::string S; | ||
for (auto F : Files) { | ||
F.back() = '*'; | ||
S += "src:"; | ||
S += F; | ||
S += "\n"; | ||
} | ||
return S; | ||
} | ||
|
||
std::string genBoth(const std::vector<std::string> &Files) { | ||
std::string S; | ||
for (auto F : Files) { | ||
F.back() = '*'; | ||
F.front() = '*'; | ||
S += "src:"; | ||
S += F; | ||
S += "\n"; | ||
} | ||
return S; | ||
} | ||
|
||
void BM_Make_( | ||
benchmark::State &state, | ||
std::string (*GenerateCaseList)(const std::vector<std::string> &Files)) { | ||
std::vector<std::string> BigFileList = genFiles(state.range(0)); | ||
std::string BigCaseList = GenerateCaseList(BigFileList); | ||
for (auto _ : state) { | ||
auto SCL = makeSpecialCaseList(BigCaseList); | ||
benchmark::DoNotOptimize(SCL); | ||
} | ||
} | ||
void BM_True_( | ||
benchmark::State &state, | ||
std::string (*GenerateCaseList)(const std::vector<std::string> &Files)) { | ||
std::vector<std::string> BigFileList = genFiles(state.range(0)); | ||
std::string BigCaseList = GenerateCaseList(BigFileList); | ||
auto SCL = makeSpecialCaseList(BigCaseList); | ||
std::mt19937 Rng(RNG_SEED); | ||
std::uniform_int_distribution<> LineDistrib(0, BigFileList.size() - 1); | ||
for (auto _ : state) { | ||
auto &Q = BigFileList[LineDistrib(Rng)]; | ||
bool R = SCL->inSection("", "src", Q); | ||
if (!R) | ||
abort(); | ||
benchmark::DoNotOptimize(R); | ||
} | ||
} | ||
|
||
void BM_False( | ||
benchmark::State &state, | ||
std::string (*GenerateCaseList)(const std::vector<std::string> &Files)) { | ||
std::vector<std::string> BigFileList = genFiles(state.range(0)); | ||
std::string BigCaseList = GenerateCaseList(BigFileList); | ||
auto SCL = makeSpecialCaseList(BigCaseList); | ||
std::mt19937 Rng(RNG_SEED); | ||
std::uniform_int_distribution<> LineDistrib(0, BigFileList.size() - 1); | ||
for (auto _ : state) { | ||
std::string Q = BigFileList[LineDistrib(Rng)]; | ||
std::uniform_int_distribution<> PosDistrib(0, Q.size() - 1); | ||
Q[PosDistrib(Rng)] = '_'; | ||
bool R = SCL->inSection("", "src", Q); | ||
benchmark::DoNotOptimize(R); | ||
} | ||
} | ||
|
||
} // namespace | ||
|
||
BENCHMARK_CAPTURE(BM_Make_, None_, genFixed) | ||
->RangeMultiplier(MAX_LIST_MUL) | ||
->Range(MAX_LIST_MIN, MAX_LIST_MAX); | ||
BENCHMARK_CAPTURE(BM_Make_, Start, genStart) | ||
->RangeMultiplier(MAX_LIST_MUL) | ||
->Range(MAX_LIST_MIN, MAX_LIST_MAX); | ||
BENCHMARK_CAPTURE(BM_Make_, End__, genEnd) | ||
->RangeMultiplier(MAX_LIST_MUL) | ||
->Range(MAX_LIST_MIN, MAX_LIST_MAX); | ||
BENCHMARK_CAPTURE(BM_Make_, Mid__, genMid) | ||
->RangeMultiplier(MAX_LIST_MUL) | ||
->Range(MAX_LIST_MIN, MAX_LIST_MAX); | ||
BENCHMARK_CAPTURE(BM_Make_, Both_, genBoth) | ||
->RangeMultiplier(MAX_LIST_MUL) | ||
->Range(MAX_LIST_MIN, MAX_LIST_MAX); | ||
|
||
BENCHMARK_CAPTURE(BM_True_, None_, genFixed) | ||
->RangeMultiplier(MAX_LIST_MUL) | ||
->Range(MAX_LIST_MIN, MAX_LIST_MAX); | ||
BENCHMARK_CAPTURE(BM_True_, Start, genStart) | ||
->RangeMultiplier(MAX_LIST_MUL) | ||
->Range(MAX_LIST_MIN, MAX_LIST_MAX); | ||
BENCHMARK_CAPTURE(BM_True_, End__, genEnd) | ||
->RangeMultiplier(MAX_LIST_MUL) | ||
->Range(MAX_LIST_MIN, MAX_LIST_MAX); | ||
BENCHMARK_CAPTURE(BM_True_, Mid__, genMid) | ||
->RangeMultiplier(MAX_LIST_MUL) | ||
->Range(MAX_LIST_MIN, MAX_LIST_MAX); | ||
BENCHMARK_CAPTURE(BM_True_, Both_, genBoth) | ||
->RangeMultiplier(MAX_LIST_MUL) | ||
->Range(MAX_LIST_MIN, MAX_LIST_MAX); | ||
|
||
BENCHMARK_CAPTURE(BM_False, None_, genFixed) | ||
->RangeMultiplier(MAX_LIST_MUL) | ||
->Range(MAX_LIST_MIN, MAX_LIST_MAX); | ||
BENCHMARK_CAPTURE(BM_False, Start, genStart) | ||
->RangeMultiplier(MAX_LIST_MUL) | ||
->Range(MAX_LIST_MIN, MAX_LIST_MAX); | ||
BENCHMARK_CAPTURE(BM_False, End__, genEnd) | ||
->RangeMultiplier(MAX_LIST_MUL) | ||
->Range(MAX_LIST_MIN, MAX_LIST_MAX); | ||
BENCHMARK_CAPTURE(BM_False, Mid__, genMid) | ||
->RangeMultiplier(MAX_LIST_MUL) | ||
->Range(MAX_LIST_MIN, MAX_LIST_MAX); | ||
BENCHMARK_CAPTURE(BM_False, Both_, genBoth) | ||
->RangeMultiplier(MAX_LIST_MUL) | ||
->Range(MAX_LIST_MIN, MAX_LIST_MAX); | ||
|
||
BENCHMARK_MAIN(); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.