Skip to content

Commit

Permalink
🔧 cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
kris-jusiak committed Jul 1, 2024
1 parent fc9f95a commit f99eb17
Show file tree
Hide file tree
Showing 10 changed files with 98 additions and 87 deletions.
2 changes: 1 addition & 1 deletion perfect_hashing/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ gperf:

%:
${CXX} \
-I. -I.. -I ~/Projects/boost-1.85 ${CXXFLAGS} ${CONSTEXPR_LIMIT} \
-I. -I.. -I ~/Projects/boost-1.85 ${CXXFLAGS} -DNTEST ${CONSTEXPR_LIMIT} \
-DSIZE=${SIZE} \
-DPROBABILITY=${PROBABILITY} \
-DSEED=${SEED} \
Expand Down
15 changes: 7 additions & 8 deletions perfect_hashing/benchmark.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@
//
#pragma once

#ifndef NTEST
#define NTEST
#endif
#include <mph>
#include <chrono>
#include <vector>
Expand All @@ -34,7 +31,7 @@ void timeit(const auto& fn) {
std::cout << checksum << ":" << std::chrono::duration_cast<std::chrono::nanoseconds>(stop - start).count() / double(1e9) << std::endl;
}

template<u32 Size, double Probability, u64 Seed, auto N = 100'000, const auto& input = DATA>
template<u32 Size, double Probability, u64 Seed, template<class, class, const auto&> class Benchmark, auto N = 100'000, const auto& input = DATA>
[[nodiscard]] auto int_to_int(auto fn) {
using key_type = decltype(input[0].first);
using mapped_type = decltype(input[0].second);
Expand All @@ -48,17 +45,18 @@ template<u32 Size, double Probability, u64 Seed, auto N = 100'000, const auto& i
std::vector<key_type> lookups(Size * N);
for (auto i = 0u; i < lookups.size(); ++i) lookups[i] = data[(i % 100) < (Probability * 100)][i];

Benchmark<key_type, mapped_type, input> benchmark{};
timeit([&]{
u64 checksum{};
for (const auto& lookup: lookups) {
checksum += (fn.template operator()<input>(lookup) != mapped_type{});
checksum += (benchmark(lookup) != mapped_type{});
}
return checksum;
});
}

template<u32 Size, double Probability, u64 Seed, auto N = 100'000, const auto& input = DATA>
[[nodiscard]] auto str_to_int(auto fn) {
template<u32 Size, double Probability, u64 Seed, template<class, class, const auto&> class Benchmark, auto N = 100'000, const auto& input = DATA>
[[nodiscard]] auto str_to_int() {
using key_type = decltype(input[0].first);
using mapped_type = decltype(input[0].second);
std::mt19937_64 gen{Seed};
Expand All @@ -71,10 +69,11 @@ template<u32 Size, double Probability, u64 Seed, auto N = 100'000, const auto& i
std::vector<std::string_view> lookups(Size * N);
for (auto i = 0u; i < lookups.size(); ++i) lookups[i] = data[(i % 100) < (Probability * 100)][i];

Benchmark<key_type, mapped_type, input> benchmark{};
timeit([&]{
u64 checksum{};
for (const auto& lookup: lookups) {
checksum += fn.template operator()<input>(lookup);
checksum += benchmark(lookup);
}
return checksum;
});
Expand Down
19 changes: 10 additions & 9 deletions perfect_hashing/boost_unordered_flat_map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,16 @@
#include <benchmark.hpp>
#include <boost/unordered/unordered_flat_map.hpp>

template<const auto& entries>
auto find(auto value) {
static const auto values = []<auto... Ns>(std::index_sequence<Ns...>) {
return boost::unordered_flat_map{entries[Ns]...};
}(std::make_index_sequence<entries.size()>{});
const auto it = values.find(value);
return it != values.end() ? it->second : decltype(it->second){};
}
template<class T, class TMapped, const auto& entries>
struct find {
auto operator()(auto value) const {
const auto it = map.find(value);
return it != map.end() ? it->second : decltype(it->second){};
}
private:
boost::unordered_flat_map<T, TMapped> map{entries.begin(), entries.end()};
};

int main() {
BENCHMARK<SIZE, PROBABILITY, SEED>([]<const auto& entries>(auto value) { return find<entries>(value); });
BENCHMARK<SIZE, PROBABILITY, SEED, find>();
}
24 changes: 16 additions & 8 deletions perfect_hashing/frozen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,26 @@
// http://www.boost.org/LICENSE_1_0.txt)
//
#include <benchmark.hpp>
#include <utility>
#include <frozen/unordered_map.h>
#include <frozen/string.h>

template<const auto& entries>
auto find(auto value) {
static constexpr auto values = []<auto... Ns>(std::index_sequence<Ns...>) {
return frozen::unordered_map<std::conditional_t<std::is_same_v<std::remove_cvref_t<decltype(entries[0].first)>, std::string_view>, frozen::string, std::remove_cvref_t<decltype(entries[0].first)>>, std::remove_cvref_t<decltype(entries[0].second)>, entries.size()>{entries[Ns]...};
template<class T, class TMapped, const auto& entries>
struct find {
auto operator()(auto value) const {
const auto it = map.find(value);
return it != map.end() ? it->second : decltype(it->second){};
}

private:
static constexpr auto map = []<auto... Ns>(std::index_sequence<Ns...>) {
return frozen::unordered_map<
std::conditional_t<std::is_same_v<T, std::string_view>, frozen::string, T>, TMapped,
entries.size()
>{entries[Ns]...};
}(std::make_index_sequence<entries.size()>{});
const auto it = values.find(value);
return it != values.end() ? it->second : decltype(it->second){};
}
};

int main() {
BENCHMARK<SIZE, PROBABILITY, SEED>([]<const auto& entries>(auto value) { return find<entries>(value); });
BENCHMARK<SIZE, PROBABILITY, SEED, find>();
}
18 changes: 10 additions & 8 deletions perfect_hashing/gperf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,17 @@
#include <benchmark.hpp>
#include "/dev/shm/gperf.hpp"

template<const auto& entries>
auto find(auto value) {
if (auto ptr = Perfect_Hash::find(value.data(), value.size())) {
return ptr->value;
} else {
return decltype(ptr->value){};
template<class T, class TMapped, const auto& entries>
struct find {
auto operator()(auto value) const -> TMapped {
if (auto ptr = Perfect_Hash::find(value.data(), value.size())) {
return ptr->value;
} else {
return {};
}
}
}
};

int main() {
BENCHMARK<SIZE, PROBABILITY, SEED>([]<const auto& entries>(auto value) { return find<entries>(value); });
BENCHMARK<SIZE, PROBABILITY, SEED, find>();
}
22 changes: 12 additions & 10 deletions perfect_hashing/if_else.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,19 @@
//
#include <benchmark.hpp>

template<const auto& entries, auto I = 0>
auto find(auto value) -> decltype(entries[0].second) {
if constexpr (I == entries.size()) {
return {};
} else if (value == entries[I].first) {
return entries[I].second;
} else {
return find<entries, I + 1>(value);
template<class T, class TMapped, const auto& entries>
struct find {
template<auto I = 0> auto operator()(auto value) const -> TMapped {
if constexpr (I == entries.size()) {
return {};
} else if (value == entries[I].first) {
return entries[I].second;
} else {
return operator()<I + 1>(value);
}
}
}
};

int main() {
BENCHMARK<SIZE, PROBABILITY, SEED>([]<const auto& entries>(auto value) { return find<entries>(value); });
BENCHMARK<SIZE, PROBABILITY, SEED, find>();
}
19 changes: 10 additions & 9 deletions perfect_hashing/map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,16 @@
#include <benchmark.hpp>
#include <map>

template<const auto& entries>
auto find(auto value) {
static const auto values = []<auto... Ns>(std::index_sequence<Ns...>) {
return std::map{entries[Ns]...};
}(std::make_index_sequence<entries.size()>{});
const auto it = values.find(value);
return it != values.end() ? it->second : decltype(it->second){};
}
template<class T, class TMapped, const auto& entries>
struct find {
auto operator()(auto value) const {
const auto it = map.find(value);
return it != map.end() ? it->second : decltype(it->second){};
}
private:
std::map<T, TMapped> map{entries.begin(), entries.end()};
};

int main() {
BENCHMARK<SIZE, PROBABILITY, SEED>([]<const auto& entries>(auto value) { return find<entries>(value); });
BENCHMARK<SIZE, PROBABILITY, SEED, find>();
}
15 changes: 7 additions & 8 deletions perfect_hashing/mph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,15 @@
// http://www.boost.org/LICENSE_1_0.txt)
//
#include <benchmark.hpp>
#ifndef NTEST
#define NTEST
#endif
#include <mph>

template<const auto& entries>
auto find(auto value) {
return *mph::find<entries>(value);
}
template<class T, class TMapped, const auto& entries>
struct find {
auto operator()(auto value) const {
return *mph::find<entries>(value);
}
};

int main() {
BENCHMARK<SIZE, PROBABILITY, SEED>([]<const auto& entries>(auto value) { return find<entries>(value); });
BENCHMARK<SIZE, PROBABILITY, SEED, find>();
}
32 changes: 15 additions & 17 deletions perfect_hashing/switch_case.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,19 @@
//
#include <benchmark.hpp>

template <const auto& entries>
constexpr auto find(auto value) {
const auto switch_case = [value]<auto I = 0>(auto self) {
if constexpr (I == entries.size()) {
return decltype(entries[0].second){};
} else {
switch (value) {
default: return self.template operator()<I + 1>(self);
case entries[I].first: return entries[I].second;
template<class T, class TMapped, const auto& entries>
struct find {
auto operator()(auto value) const {
const auto switch_case = [value]<auto I = 0>(auto self) {
if constexpr (I == entries.size()) {
return decltype(entries[0].second){};
} else {
switch (value) {
default: return self.template operator()<I + 1>(self);
case entries[I].first: return entries[I].second;
}
}
}
};
return switch_case(switch_case);
}

int main() {
BENCHMARK<SIZE, PROBABILITY, SEED>([]<const auto& entries>(auto value) { return find<entries>(value); });
}
};
return switch_case(switch_case);
}
};
19 changes: 10 additions & 9 deletions perfect_hashing/unordered_map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,16 @@
#include <benchmark.hpp>
#include <unordered_map>

template<const auto& entries>
auto find(auto value) {
static const auto values = []<auto... Ns>(std::index_sequence<Ns...>) {
return std::unordered_map{entries[Ns]...};
}(std::make_index_sequence<entries.size()>{});
const auto it = values.find(value);
return it != values.end() ? it->second : decltype(it->second){};
}
template<class T, class TMapped, const auto& entries>
struct find {
auto operator()(auto value) const {
const auto it = map.find(value);
return it != map.end() ? it->second : decltype(it->second){};
}
private:
std::unordered_map<T, TMapped> map{entries.begin(), entries.end()};
};

int main() {
BENCHMARK<SIZE, PROBABILITY, SEED>([]<const auto& entries>(auto value) { return find<entries>(value); });
BENCHMARK<SIZE, PROBABILITY, SEED, find>();
}

0 comments on commit f99eb17

Please sign in to comment.