-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbench_hashes.c
339 lines (286 loc) · 8.63 KB
/
bench_hashes.c
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
/*
Simple Hash Benchmark
Copyright (c) 2024, Eddy L O Jansson. Licensed under The MIT License.
See https://github.com/eloj/hashbench
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdint.h>
#include <time.h>
#include <float.h>
#include <assert.h>
//
// This is the One True prototype for a hash function.
//
typedef uint32_t hash32_t(const void *data, size_t len, void *state);
typedef uint64_t hash64_t(const void *data, size_t len, void *state);
static size_t n_rounds = 10;
static size_t n_iter = 100;
static int do_dump_buckets = 0;
#include "hash/hash_murmur3.c"
#include "hash/hash_djb2.c"
#include "hash/hash_jenkins.c"
#include "hash/hash_fnv1a.c"
#include "hash/hash_crc32c.c"
#include "hash/hash_siphash.c"
static struct hash_t {
const char *name;
hash32_t *func32; // Only one ...
hash64_t *func64; // ... of these should be defined
void *state;
} hashes[] = {
{ "murmur3_32", hash_murmur3_32, NULL, (void*)0x9747b28c },
{ "djb2", hash_djb2, NULL, NULL },
{ "jenkins", hash_jenkins, NULL, NULL },
{ "fnv1a_32", hash_fnv1a_32, NULL, NULL },
{ "crc32c", hash_crc32c, NULL, NULL },
// { "siphash32", hash_siphash_32, NULL, "randomsecretkey!" },
{ "siphash64", NULL, hash_siphash_64, "randomsecretkey!" },
};
// Not a static const int because clang complains about its use in VLA.
#define NUM_HASHES (int)(sizeof(hashes)/sizeof(hashes[0]))
struct hash_bench_result {
int hash_idx;
int n_buckets;
size_t bytes;
double tot_timer;
double min_timer;
double score;
uint64_t hashval_acc;
size_t buckets[];
};
#define START_TIMER() do { clock_gettime(CLOCK_MONOTONIC_RAW, &tp_start); } while (0)
#define STOP_TIMER(timer) do { \
clock_gettime(CLOCK_MONOTONIC_RAW, &tp_end); \
struct timespec tp_res; \
timespec_diff(&tp_start, &tp_end, &tp_res); \
timer += (tp_res.tv_sec * 1000) + (tp_res.tv_nsec / 1.0e6f); } while (0)
void timespec_diff(struct timespec *start, struct timespec *stop,
struct timespec *result)
{
if ((stop->tv_nsec - start->tv_nsec) < 0) {
result->tv_sec = stop->tv_sec - start->tv_sec - 1;
result->tv_nsec = stop->tv_nsec - start->tv_nsec + 1000000000;
} else {
result->tv_sec = stop->tv_sec - start->tv_sec;
result->tv_nsec = stop->tv_nsec - start->tv_nsec;
}
}
struct hash_bench_result* create_hash_bench_result(int idx, int buckets) {
struct hash_bench_result *res = malloc(sizeof(struct hash_bench_result) + (buckets * sizeof(res->buckets[0])));
assert(res);
res->hash_idx = idx;
res->n_buckets = buckets;
res->min_timer = DBL_MAX;
res->tot_timer = 0;
return res;
}
void bench_hash(hash32_t func32, hash64_t func64, void *hash_state, const char *words, struct hash_bench_result *res) {
struct timespec tp_start;
struct timespec tp_end;
char *ptr;
res->bytes = 0;
res->tot_timer = 0;
assert(!(func32 && func64));
for (size_t i = 0 ; i < n_iter ; ++i) {
const char *word = words;
memset(res->buckets, 0, sizeof(res->buckets[0])*res->n_buckets);
res->hashval_acc = 0;
double local_timer = 0;
if (func32) {
START_TIMER();
while ((ptr = strchr(word, '\n')) != NULL) {
size_t len = ptr - word;
uint32_t hashval = 0;
for (size_t rounds = 0 ; rounds < n_rounds ; ++rounds) {
hashval = func32(word, len, hash_state);
res->bytes += len;
res->hashval_acc += hashval;
}
if (res->n_buckets)
++res->buckets[hashval % res->n_buckets];
word = ptr + 1;
}
STOP_TIMER(local_timer);
} else if (func64) {
START_TIMER();
while ((ptr = strchr(word, '\n')) != NULL) {
size_t len = ptr - word;
uint64_t hashval = 0;
for (size_t rounds = 0 ; rounds < n_rounds ; ++rounds) {
hashval = func64(word, len, hash_state);
res->bytes += len;
res->hashval_acc += hashval;
}
if (res->n_buckets)
++res->buckets[hashval % res->n_buckets];
word = ptr + 1;
}
STOP_TIMER(local_timer);
}
res->tot_timer += local_timer;
if (local_timer < res->min_timer) {
res->min_timer = local_timer;
}
}
}
char *read_entire_file(const char *filename, size_t *len) {
FILE *f = fopen(filename, "rb");
if (!f)
return NULL;
fseek(f, 0, SEEK_END);
long bytes = ftell(f);
if (bytes < 0) {
fclose(f);
return NULL;
}
rewind(f);
char *buf = malloc(bytes + 1);
if (!buf) {
fclose(f);
return NULL;
}
size_t rnum = fread(buf, bytes, 1, f);
fclose(f);
if (bytes && rnum != 1) {
free(buf);
return NULL;
}
buf[bytes] = 0; // always zero-terminate.
if (len)
*len = bytes;
return buf;
}
void print_results_human(struct hash_bench_result *res[]) {
// Extract winners and losers
int fastest = -1;
int slowest = -1;
int bestscore = -1;
int worstscore = -1;
for (int i = 0 ; i < NUM_HASHES ; ++i) {
if (fastest == -1 || res[i]->min_timer < res[fastest]->min_timer) {
fastest = i;
}
if (slowest == -1 || res[i]->min_timer > res[slowest]->min_timer) {
slowest = i;
}
if (bestscore == -1 || res[i]->score > res[bestscore]->score) {
bestscore = i;
}
if (worstscore == -1 || res[i]->score < res[worstscore]->score) {
worstscore = i;
}
}
// Output result table
for (int i = 0 ; i < NUM_HASHES ; ++i) {
struct hash_bench_result *bres = res[i];
struct hash_t *hash = &hashes[bres->hash_idx];
if (hash->func32) {
printf("%s:\n%.2f/%.2f ms (min/avg), ~%.2f MiB/s (avg) (hash:0x%" PRIx32 "), score=%.2f", hash->name,
bres->min_timer / n_rounds, bres->tot_timer / (n_iter*n_rounds),
((bres->bytes / bres->tot_timer)*1000)/(1024*1024),
(uint32_t)bres->hashval_acc,
bres->score
);
} else if (hash->func64) {
printf("%s:\n%.2f/%.2f ms (min/avg), ~%.2f MiB/s (avg) (hash:0x%" PRIx64 "), score=%.2f", hash->name,
bres->min_timer / n_rounds, bres->tot_timer / (n_iter*n_rounds),
((bres->bytes / bres->tot_timer)*1000)/(1024*1024),
bres->hashval_acc,
bres->score
);
}
if (i == fastest) {
printf(" best time! ");
}
if (i == slowest) {
printf(" slowest. ");
}
if (i == bestscore) {
printf(" best score! ");
}
if (i == worstscore) {
printf(" worst score. ");
}
printf("\n");
}
}
void dump_buckets(struct hash_bench_result *res[], size_t word_count) {
for (int i = 0 ; i < NUM_HASHES ; ++i) {
struct hash_bench_result *bres = res[i];
struct hash_t *hash = &hashes[bres->hash_idx];
if (i == 0)
printf("# %zu words, %d buckets\n", word_count, bres->n_buckets);
printf("\"%s\",", hash->name);
for (int j = 0 ; j < bres->n_buckets ; ++j) {
printf("%zu,", bres->buckets[j]);
}
printf("\n");
}
}
void run_benchmarks(struct hash_bench_result *res[], const char *words, size_t word_count, size_t buckets) {
for (int i = 0 ; i < NUM_HASHES ; ++i) {
struct hash_bench_result *bres = create_hash_bench_result(i, buckets);
struct hash_t *hash = &hashes[i];
bench_hash(hash->func32, hash->func64, hash->state, words, bres);
bres->score = 0;
// Calculate the sum of differences squared. Closer to zero is better ~= more uniform dist.
double expected = word_count / buckets;
for (size_t j=0 ; j < buckets ; ++j) {
double deviance = expected - bres->buckets[j];
bres->score += deviance*deviance;
// printf("[%d]=%zu, dev=%.4f\n", j, bres.buckets[j], deviance);
}
if (buckets) {
bres->score /= buckets; // normalize over buckets
bres->score = -bres->score;
}
res[i] = bres;
}
}
int main(int argc, char *argv[])
{
const char *dict = argc > 1 ? argv[1] : "/usr/share/dict/american-english";
size_t n_buckets = argc > 2 ? atoi(argv[2]) : 32;
n_rounds = argc > 3 ? atoi(argv[3]) : 10;
n_iter = argc > 4 ? atoi(argv[4]) : 100;
if (n_rounds < 1) n_rounds = 1;
if (n_iter < 1) n_iter = 1;
size_t words_len = 0;
printf("Benchmarking hashes using '%s' with %zu buckets, %zu rounds/word, %zu iterations.\n", dict, n_buckets, n_rounds, n_iter);
char *words = read_entire_file(dict, &words_len);
if (words == NULL) {
fprintf(stderr, "Error loading dictionary: %m\n");
return EXIT_FAILURE;
}
size_t word_count = 0;
size_t word_size = 0;
struct timespec tp_start;
struct timespec tp_end;
double timer_iterate = 0;
// Null-Hypothesis (also cache-warming and word counting)
char *prev = words;
char *ptr;
START_TIMER();
while ((ptr = strchr(prev, '\n')) != NULL) {
size_t len = ptr - prev;
// printf("%s (%zu)\n", prev, len);
word_size += len;
prev = ptr + 1;
++word_count;
}
STOP_TIMER(timer_iterate);
printf("time to iterate: %.2f ms (%zu words, %zu bytes)\n", timer_iterate, word_count, word_size);
struct hash_bench_result *res[NUM_HASHES] = { };
run_benchmarks(res, words, word_count, n_buckets);
print_results_human(res);
if (do_dump_buckets) {
dump_buckets(res, word_count);
}
for (size_t i = 0 ; i < NUM_HASHES ; ++i) {
free(res[i]);
}
free(words);
return EXIT_SUCCESS;
}