-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #148 from zyantific/stringpool-overhaul
Stringpool overhaul
- Loading branch information
Showing
5 changed files
with
658 additions
and
128 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
@@ -1,73 +1,136 @@ | ||
#include <benchmark/benchmark.h> | ||
#include <random> | ||
#include <zasm/core/stringpool.hpp> | ||
|
||
namespace zasm::benchmarks | ||
{ | ||
static const std::string TestStrings[] = { "hello", "world", "longer string", "even longer string", | ||
"even longer longer string" }; | ||
static constexpr auto kTestSize = 500'000; | ||
static constexpr const char kChars[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; | ||
|
||
static void BM_StringPool_Aquire(benchmark::State& state) | ||
static const std::vector<std::string> kInputStrings = []() { | ||
std::vector<std::string> strings; | ||
|
||
std::mt19937 prng(42); | ||
for (int i = 0; i < kTestSize; ++i) | ||
{ | ||
std::string str; | ||
for (size_t i = 0; i < 4 + (prng() % 24); ++i) | ||
{ | ||
str.push_back(kChars[prng() % (sizeof(kChars) - 1)]); | ||
} | ||
strings.push_back(std::move(str)); | ||
} | ||
return strings; | ||
}(); | ||
|
||
static void BM_StringPool_Acquire(benchmark::State& state) | ||
{ | ||
StringPool pool; | ||
for (auto _ : state) | ||
{ | ||
const auto& str = TestStrings[state.range(0)]; | ||
for (auto i = 0; i < state.range(0); ++i) | ||
{ | ||
const auto& str = kInputStrings[i]; | ||
|
||
auto stringId = pool.aquire(str); | ||
benchmark::DoNotOptimize(stringId); | ||
auto stringId = pool.acquire(str); | ||
benchmark::DoNotOptimize(stringId); | ||
} | ||
} | ||
} | ||
BENCHMARK(BM_StringPool_Aquire)->DenseRange(0, std::size(TestStrings) - 1); | ||
BENCHMARK(BM_StringPool_Acquire)->Range(0, kTestSize)->Unit(benchmark::kMillisecond); | ||
|
||
static void BM_StringPool_Release(benchmark::State& state) | ||
{ | ||
StringPool pool; | ||
for (auto _ : state) | ||
{ | ||
for (auto i = 0; i < state.range(0); ++i) | ||
{ | ||
state.PauseTiming(); | ||
const auto& str = kInputStrings[i]; | ||
|
||
auto stringId = pool.acquire(str); | ||
state.ResumeTiming(); | ||
|
||
auto refCount = pool.release(stringId); | ||
benchmark::DoNotOptimize(refCount); | ||
} | ||
} | ||
} | ||
BENCHMARK(BM_StringPool_Release)->Range(0, kTestSize)->Unit(benchmark::kMillisecond); | ||
|
||
static void BM_StringPool_Reuse(benchmark::State& state) | ||
{ | ||
StringPool pool; | ||
for (auto _ : state) | ||
{ | ||
std::vector<StringPool::Id> stringIds; | ||
|
||
state.PauseTiming(); | ||
const auto& str = TestStrings[state.range(0)]; | ||
for (auto i = 0; i < state.range(0); ++i) | ||
{ | ||
const auto& str = kInputStrings[i]; | ||
|
||
auto stringId = pool.acquire(str); | ||
stringIds.push_back(stringId); | ||
} | ||
|
||
auto stringId = pool.aquire(str); | ||
// Clear. | ||
for (auto i = 0; i < state.range(0); ++i) | ||
{ | ||
auto stringId = pool.release(stringIds[i]); | ||
} | ||
state.ResumeTiming(); | ||
|
||
auto refCount = pool.release(stringId); | ||
benchmark::DoNotOptimize(refCount); | ||
for (auto i = 0; i < state.range(0); ++i) | ||
{ | ||
const auto& str = kInputStrings[i]; | ||
|
||
auto stringId = pool.acquire(str); | ||
benchmark::DoNotOptimize(stringId); | ||
} | ||
} | ||
} | ||
BENCHMARK(BM_StringPool_Release)->DenseRange(0, std::size(TestStrings) - 1); | ||
BENCHMARK(BM_StringPool_Reuse)->Range(0, kTestSize)->Unit(benchmark::kMillisecond); | ||
|
||
static void BM_StringPool_Get(benchmark::State& state) | ||
{ | ||
StringPool pool; | ||
for (auto _ : state) | ||
{ | ||
state.PauseTiming(); | ||
const auto& str = TestStrings[state.range(0)]; | ||
for (auto i = 0; i < state.range(0); ++i) | ||
{ | ||
state.PauseTiming(); | ||
const auto& str = kInputStrings[i]; | ||
|
||
auto stringId = pool.aquire(str); | ||
state.ResumeTiming(); | ||
auto stringId = pool.acquire(str); | ||
state.ResumeTiming(); | ||
|
||
const char* res = pool.get(stringId); | ||
benchmark::DoNotOptimize(res); | ||
const char* res = pool.get(stringId); | ||
benchmark::DoNotOptimize(res); | ||
} | ||
} | ||
} | ||
BENCHMARK(BM_StringPool_Get)->DenseRange(0, std::size(TestStrings) - 1); | ||
BENCHMARK(BM_StringPool_Get)->Range(0, kTestSize)->Unit(benchmark::kMillisecond); | ||
|
||
static void BM_StringPool_GetLength(benchmark::State& state) | ||
{ | ||
StringPool pool; | ||
for (auto _ : state) | ||
{ | ||
state.PauseTiming(); | ||
const auto& str = TestStrings[state.range(0)]; | ||
for (auto i = 0; i < state.range(0); ++i) | ||
{ | ||
state.PauseTiming(); | ||
const auto& str = kInputStrings[i]; | ||
|
||
auto stringId = pool.aquire(str); | ||
state.ResumeTiming(); | ||
auto stringId = pool.acquire(str); | ||
state.ResumeTiming(); | ||
|
||
auto strLen = pool.getLength(stringId); | ||
benchmark::DoNotOptimize(strLen); | ||
auto strLen = pool.getLength(stringId); | ||
benchmark::DoNotOptimize(strLen); | ||
} | ||
} | ||
} | ||
BENCHMARK(BM_StringPool_GetLength)->DenseRange(0, std::size(TestStrings) - 1); | ||
BENCHMARK(BM_StringPool_GetLength)->Range(0, kTestSize)->Unit(benchmark::kMillisecond); | ||
|
||
} // namespace zasm::benchmarks |
Oops, something went wrong.