Skip to content

Commit

Permalink
Merge pull request #148 from zyantific/stringpool-overhaul
Browse files Browse the repository at this point in the history
Stringpool overhaul
  • Loading branch information
ZehMatt authored Nov 8, 2024
2 parents 57540ab + 9dc918e commit e7d80fb
Show file tree
Hide file tree
Showing 5 changed files with 658 additions and 128 deletions.
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

0 comments on commit e7d80fb

Please sign in to comment.