1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
|
#include <benchmark/benchmark.h>
#include "bench_common.h"
extern "C" {
#include "klib/khash.h"
}
namespace {
using namespace cmbench;
// Define khash instances for each key-value type
static inline std::uint64_t kh_hash_u64(std::uint64_t key) {
return hash_xxh_avalanche(key);
}
static inline int kh_equal_u64(std::uint64_t a, std::uint64_t b) {
return a == b;
}
KHASH_INIT(u64_scalar, std::uint64_t, ScalarValue, 1, kh_hash_u64, kh_equal_u64)
static inline std::uint64_t kh_hash_entity(EntityId key) {
return hash_xxh_avalanche(key);
}
static inline int kh_equal_entity(EntityId a, EntityId b) {
return byte_equal(a, b);
}
KHASH_INIT(entity_payload, EntityId, ComponentPayload, 1, kh_hash_entity,
kh_equal_entity)
static inline std::uint64_t kh_hash_component(ComponentKey key) {
return hash_xxh_avalanche(key);
}
static inline int kh_equal_component(ComponentKey a, ComponentKey b) {
return byte_equal(a, b);
}
KHASH_INIT(component_meta, ComponentKey, ComponentMeta, 1, kh_hash_component,
kh_equal_component)
// Adapter for u64 -> ScalarValue
struct KlibScalarAdapter {
khash_t(u64_scalar) * map = nullptr;
KlibScalarAdapter() { map = kh_init(u64_scalar); }
~KlibScalarAdapter() {
if (map != nullptr) kh_destroy(u64_scalar, map);
}
KlibScalarAdapter(const KlibScalarAdapter&) = delete;
KlibScalarAdapter& operator=(const KlibScalarAdapter&) = delete;
void reserve(std::size_t count) { benchmark::DoNotOptimize(count); }
void insert(const std::uint64_t& key, const ScalarValue& value) {
int ret;
khiter_t k = kh_put(u64_scalar, map, key, &ret);
if (ret < 0) panic_impl(__FILE__, __LINE__, "insert failed");
kh_value(map, k) = value;
}
ScalarValue* lookup_hit(const std::uint64_t& key) {
khiter_t k = kh_get(u64_scalar, map, key);
if (k == kh_end(map)) panic_impl(__FILE__, __LINE__, "lookup hit failed");
return &kh_value(map, k);
}
void lookup_miss(const std::uint64_t& key) {
khiter_t k = kh_get(u64_scalar, map, key);
if (k != kh_end(map)) panic_impl(__FILE__, __LINE__, "lookup miss failed");
benchmark::DoNotOptimize(k);
}
void erase(const std::uint64_t& key) {
khiter_t k = kh_get(u64_scalar, map, key);
if (k == kh_end(map)) panic_impl(__FILE__, __LINE__, "erase failed");
kh_del(u64_scalar, map, k);
}
};
// Adapter for EntityId -> ComponentPayload
struct KlibEntityAdapter {
khash_t(entity_payload) * map = nullptr;
KlibEntityAdapter() { map = kh_init(entity_payload); }
~KlibEntityAdapter() {
if (map != nullptr) kh_destroy(entity_payload, map);
}
KlibEntityAdapter(const KlibEntityAdapter&) = delete;
KlibEntityAdapter& operator=(const KlibEntityAdapter&) = delete;
void reserve(std::size_t count) { benchmark::DoNotOptimize(count); }
void insert(const EntityId& key, const ComponentPayload& value) {
int ret;
khiter_t k = kh_put(entity_payload, map, key, &ret);
if (ret < 0) panic_impl(__FILE__, __LINE__, "insert failed");
kh_value(map, k) = value;
}
ComponentPayload* lookup_hit(const EntityId& key) {
khiter_t k = kh_get(entity_payload, map, key);
if (k == kh_end(map)) panic_impl(__FILE__, __LINE__, "lookup hit failed");
return &kh_value(map, k);
}
void lookup_miss(const EntityId& key) {
khiter_t k = kh_get(entity_payload, map, key);
if (k != kh_end(map)) panic_impl(__FILE__, __LINE__, "lookup miss failed");
benchmark::DoNotOptimize(k);
}
void erase(const EntityId& key) {
khiter_t k = kh_get(entity_payload, map, key);
if (k == kh_end(map)) panic_impl(__FILE__, __LINE__, "erase failed");
kh_del(entity_payload, map, k);
}
};
// Adapter for ComponentKey -> ComponentMeta
struct KlibComponentAdapter {
khash_t(component_meta) * map = nullptr;
KlibComponentAdapter() { map = kh_init(component_meta); }
~KlibComponentAdapter() {
if (map != nullptr) kh_destroy(component_meta, map);
}
KlibComponentAdapter(const KlibComponentAdapter&) = delete;
KlibComponentAdapter& operator=(const KlibComponentAdapter&) = delete;
void reserve(std::size_t count) { benchmark::DoNotOptimize(count); }
void insert(const ComponentKey& key, const ComponentMeta& value) {
int ret;
khiter_t k = kh_put(component_meta, map, key, &ret);
if (ret < 0) panic_impl(__FILE__, __LINE__, "insert failed");
kh_value(map, k) = value;
}
ComponentMeta* lookup_hit(const ComponentKey& key) {
khiter_t k = kh_get(component_meta, map, key);
if (k == kh_end(map)) panic_impl(__FILE__, __LINE__, "lookup hit failed");
return &kh_value(map, k);
}
void lookup_miss(const ComponentKey& key) {
khiter_t k = kh_get(component_meta, map, key);
if (k != kh_end(map)) panic_impl(__FILE__, __LINE__, "lookup miss failed");
benchmark::DoNotOptimize(k);
}
void erase(const ComponentKey& key) {
khiter_t k = kh_get(component_meta, map, key);
if (k == kh_end(map)) panic_impl(__FILE__, __LINE__, "erase failed");
kh_del(component_meta, map, k);
}
};
static void BM_Insert_Scalar(benchmark::State& state) {
bench_insert<KlibScalarAdapter, std::uint64_t, ScalarValue>(
state, scalar_workload(), {"khash", "c", "Scalar", "Insert"});
}
static void BM_LookupHit_Scalar(benchmark::State& state) {
bench_lookup_hit<KlibScalarAdapter, std::uint64_t, ScalarValue>(
state, scalar_workload(), {"khash", "c", "Scalar", "LookupHit"});
}
static void BM_LookupMiss_Scalar(benchmark::State& state) {
bench_lookup_miss<KlibScalarAdapter, std::uint64_t, ScalarValue>(
state, scalar_workload(), {"khash", "c", "Scalar", "LookupMiss"});
}
static void BM_Erase_Scalar(benchmark::State& state) {
bench_erase<KlibScalarAdapter, std::uint64_t, ScalarValue>(
state, scalar_workload(), {"khash", "c", "Scalar", "Erase"});
}
static void BM_Insert_HandlePayload(benchmark::State& state) {
bench_insert<KlibEntityAdapter, EntityId, ComponentPayload>(
state, entity_workload(), {"khash", "c", "HandlePayload", "Insert"});
}
static void BM_LookupHit_HandlePayload(benchmark::State& state) {
bench_lookup_hit<KlibEntityAdapter, EntityId, ComponentPayload>(
state, entity_workload(), {"khash", "c", "HandlePayload", "LookupHit"});
}
static void BM_LookupMiss_HandlePayload(benchmark::State& state) {
bench_lookup_miss<KlibEntityAdapter, EntityId, ComponentPayload>(
state, entity_workload(),
{"khash", "c", "HandlePayload", "LookupMiss"});
}
static void BM_Erase_HandlePayload(benchmark::State& state) {
bench_erase<KlibEntityAdapter, EntityId, ComponentPayload>(
state, entity_workload(), {"khash", "c", "HandlePayload", "Erase"});
}
static void BM_Insert_CompositeKey(benchmark::State& state) {
bench_insert<KlibComponentAdapter, ComponentKey, ComponentMeta>(
state, component_workload(), {"khash", "c", "CompositeKey", "Insert"});
}
static void BM_LookupHit_CompositeKey(benchmark::State& state) {
bench_lookup_hit<KlibComponentAdapter, ComponentKey, ComponentMeta>(
state, component_workload(),
{"khash", "c", "CompositeKey", "LookupHit"});
}
static void BM_LookupMiss_CompositeKey(benchmark::State& state) {
bench_lookup_miss<KlibComponentAdapter, ComponentKey, ComponentMeta>(
state, component_workload(),
{"khash", "c", "CompositeKey", "LookupMiss"});
}
static void BM_Erase_CompositeKey(benchmark::State& state) {
bench_erase<KlibComponentAdapter, ComponentKey, ComponentMeta>(
state, component_workload(), {"khash", "c", "CompositeKey", "Erase"});
}
} // namespace
BENCHMARK(BM_Insert_Scalar);
BENCHMARK(BM_LookupHit_Scalar);
BENCHMARK(BM_LookupMiss_Scalar);
BENCHMARK(BM_Erase_Scalar);
BENCHMARK(BM_Insert_HandlePayload);
BENCHMARK(BM_LookupHit_HandlePayload);
BENCHMARK(BM_LookupMiss_HandlePayload);
BENCHMARK(BM_Erase_HandlePayload);
BENCHMARK(BM_Insert_CompositeKey);
BENCHMARK(BM_LookupHit_CompositeKey);
BENCHMARK(BM_LookupMiss_CompositeKey);
BENCHMARK(BM_Erase_CompositeKey);
|