Skip to content
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

Stringpool overhaul #148

Merged
merged 8 commits into from
Nov 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
115 changes: 89 additions & 26 deletions benchmark/src/benchmarks/benchmark.stringpool.cpp
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
Loading
Loading