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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
|
/* Simple speed tests for hashtables. */
#include <ccan/htable/htable_type.h>
#include <ccan/htable/htable.c>
#include <ccan/hash/hash.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include <sys/time.h>
static size_t hashcount;
struct object {
/* The key. */
unsigned int key;
/* Some contents. Doubles as consistency check. */
struct object *self;
};
static const unsigned int *objkey(const struct object *obj)
{
return &obj->key;
}
static size_t hash_obj(const unsigned int *key)
{
hashcount++;
return hashl(key, 1, 0);
}
static bool cmp(const unsigned int *key1, const unsigned int *key2)
{
return *key1 == *key2;
}
HTABLE_DEFINE_TYPE(struct object, objkey, hash_obj, cmp, obj);
static unsigned int popcount(unsigned long val)
{
#if HAVE_BUILTIN_POPCOUNTL
return __builtin_popcountl(val);
#else
if (sizeof(long) == sizeof(u64)) {
u64 v = val;
v = (v & 0x5555555555555555ULL)
+ ((v >> 1) & 0x5555555555555555ULL);
v = (v & 0x3333333333333333ULL)
+ ((v >> 1) & 0x3333333333333333ULL);
v = (v & 0x0F0F0F0F0F0F0F0FULL)
+ ((v >> 1) & 0x0F0F0F0F0F0F0F0FULL);
v = (v & 0x00FF00FF00FF00FFULL)
+ ((v >> 1) & 0x00FF00FF00FF00FFULL);
v = (v & 0x0000FFFF0000FFFFULL)
+ ((v >> 1) & 0x0000FFFF0000FFFFULL);
v = (v & 0x00000000FFFFFFFFULL)
+ ((v >> 1) & 0x00000000FFFFFFFFULL);
return v;
}
val = (val & 0x55555555ULL) + ((val >> 1) & 0x55555555ULL);
val = (val & 0x33333333ULL) + ((val >> 1) & 0x33333333ULL);
val = (val & 0x0F0F0F0FULL) + ((val >> 1) & 0x0F0F0F0FULL);
val = (val & 0x00FF00FFULL) + ((val >> 1) & 0x00FF00FFULL);
val = (val & 0x0000FFFFULL) + ((val >> 1) & 0x0000FFFFULL);
return val;
#endif
}
static size_t perfect(const struct htable *ht)
{
size_t i, placed_perfect = 0;
for (i = 0; i < ((size_t)1 << ht->bits); i++) {
if (!entry_is_valid(ht->table[i]))
continue;
if (hash_bucket(ht, ht->rehash(get_raw_ptr(ht, ht->table[i]),
ht->priv)) == i) {
assert((ht->table[i] & ht->perfect_bit)
== ht->perfect_bit);
placed_perfect++;
}
}
return placed_perfect;
}
static size_t count_deleted(const struct htable *ht)
{
size_t i, delete_markers = 0;
for (i = 0; i < ((size_t)1 << ht->bits); i++) {
if (ht->table[i] == HTABLE_DELETED)
delete_markers++;
}
return delete_markers;
}
/* Nanoseconds per operation */
static size_t normalize(const struct timeval *start,
const struct timeval *stop,
unsigned int num)
{
struct timeval diff;
timersub(stop, start, &diff);
/* Floating point is more accurate here. */
return (double)(diff.tv_sec * 1000000 + diff.tv_usec)
/ num * 1000;
}
static size_t worst_run(struct htable *ht, size_t *deleted)
{
size_t longest = 0, len = 0, this_del = 0, i;
*deleted = 0;
/* This doesn't take into account end-wrap, but gives an idea. */
for (i = 0; i < ((size_t)1 << ht->bits); i++) {
if (ht->table[i]) {
len++;
if (ht->table[i] == HTABLE_DELETED)
this_del++;
} else {
if (len > longest) {
longest = len;
*deleted = this_del;
}
len = 0;
this_del = 0;
}
}
return longest;
}
int main(int argc, char *argv[])
{
struct object *objs;
size_t i, j, num, deleted;
struct timeval start, stop;
struct htable_obj *ht;
struct htable *htr;
bool make_dumb = false;
if (argv[1] && strcmp(argv[1], "--dumb") == 0) {
argv++;
make_dumb = true;
}
num = argv[1] ? atoi(argv[1]) : 1000000;
objs = calloc(num, sizeof(objs[0]));
for (i = 0; i < num; i++) {
objs[i].key = i;
objs[i].self = &objs[i];
}
ht = htable_obj_new();
htr = (void *)ht;
printf("Initial insert: ");
fflush(stdout);
gettimeofday(&start, NULL);
for (i = 0; i < num; i++)
htable_obj_add(ht, objs[i].self);
gettimeofday(&stop, NULL);
printf(" %zu ns\n", normalize(&start, &stop, num));
printf("Details: hash size %u, mask bits %u, perfect %.0f%%\n",
1U << htr->bits, popcount(htr->common_mask),
perfect(htr) * 100.0 / htr->elems);
if (make_dumb) {
/* Screw with mask, to hobble us. */
update_common(htr, (void *)~htr->common_bits);
printf("Details: DUMB MODE: mask bits %u\n",
popcount(htr->common_mask));
}
printf("Initial lookup (match): ");
fflush(stdout);
gettimeofday(&start, NULL);
for (i = 0; i < num; i++)
if (htable_obj_get(ht, &i)->self != objs[i].self)
abort();
gettimeofday(&stop, NULL);
printf(" %zu ns\n", normalize(&start, &stop, num));
printf("Initial lookup (miss): ");
fflush(stdout);
gettimeofday(&start, NULL);
for (i = 0; i < num; i++) {
unsigned int n = i + num;
if (htable_obj_get(ht, &n))
abort();
}
gettimeofday(&stop, NULL);
printf(" %zu ns\n", normalize(&start, &stop, num));
/* Lookups in order are very cache-friendly for judy; try random */
printf("Initial lookup (random): ");
fflush(stdout);
gettimeofday(&start, NULL);
for (i = 0, j = 0; i < num; i++, j = (j + 10007) % num)
if (htable_obj_get(ht, &j)->self != &objs[j])
abort();
gettimeofday(&stop, NULL);
printf(" %zu ns\n", normalize(&start, &stop, num));
hashcount = 0;
printf("Initial delete all: ");
fflush(stdout);
gettimeofday(&start, NULL);
for (i = 0; i < num; i++)
if (!htable_obj_del(ht, objs[i].self))
abort();
gettimeofday(&stop, NULL);
printf(" %zu ns\n", normalize(&start, &stop, num));
printf("Details: rehashes %zu\n", hashcount);
printf("Initial re-inserting: ");
fflush(stdout);
gettimeofday(&start, NULL);
for (i = 0; i < num; i++)
htable_obj_add(ht, objs[i].self);
gettimeofday(&stop, NULL);
printf(" %zu ns\n", normalize(&start, &stop, num));
hashcount = 0;
printf("Deleting first half: ");
fflush(stdout);
gettimeofday(&start, NULL);
for (i = 0; i < num; i+=2)
if (!htable_obj_del(ht, objs[i].self))
abort();
gettimeofday(&stop, NULL);
printf(" %zu ns\n", normalize(&start, &stop, num));
printf("Details: rehashes %zu, delete markers %zu\n",
hashcount, count_deleted(htr));
printf("Adding (a different) half: ");
fflush(stdout);
for (i = 0; i < num; i+=2)
objs[i].key = num+i;
gettimeofday(&start, NULL);
for (i = 0; i < num; i+=2)
htable_obj_add(ht, objs[i].self);
gettimeofday(&stop, NULL);
printf(" %zu ns\n", normalize(&start, &stop, num));
printf("Details: delete markers %zu, perfect %.0f%%\n",
count_deleted(htr), perfect(htr) * 100.0 / htr->elems);
printf("Lookup after half-change (match): ");
fflush(stdout);
gettimeofday(&start, NULL);
for (i = 1; i < num; i+=2)
if (htable_obj_get(ht, &i)->self != objs[i].self)
abort();
for (i = 0; i < num; i+=2) {
unsigned int n = i + num;
if (htable_obj_get(ht, &n)->self != objs[i].self)
abort();
}
gettimeofday(&stop, NULL);
printf(" %zu ns\n", normalize(&start, &stop, num));
printf("Lookup after half-change (miss): ");
fflush(stdout);
gettimeofday(&start, NULL);
for (i = 0; i < num; i++) {
unsigned int n = i + num * 2;
if (htable_obj_get(ht, &n))
abort();
}
gettimeofday(&stop, NULL);
printf(" %zu ns\n", normalize(&start, &stop, num));
/* Hashtables with delete markers can fill with markers over time.
* so do some changes to see how it operates in long-term. */
for (i = 0; i < 5; i++) {
if (i == 0) {
/* We don't measure this: jmap is different. */
printf("Details: initial churn\n");
} else {
printf("Churning %s time: ",
i == 1 ? "second"
: i == 2 ? "third"
: i == 3 ? "fourth"
: "fifth");
fflush(stdout);
}
gettimeofday(&start, NULL);
for (j = 0; j < num; j++) {
if (!htable_obj_del(ht, &objs[j]))
abort();
objs[j].key = num*i+j;
if (!htable_obj_add(ht, &objs[j]))
abort();
}
gettimeofday(&stop, NULL);
if (i != 0)
printf(" %zu ns\n", normalize(&start, &stop, num));
}
/* Spread out the keys more to try to make it harder. */
printf("Details: reinserting with spread\n");
for (i = 0; i < num; i++) {
if (!htable_obj_del(ht, objs[i].self))
abort();
objs[i].key = num * 5 + i * 9;
if (!htable_obj_add(ht, objs[i].self))
abort();
}
printf("Details: delete markers %zu, perfect %.0f%%\n",
count_deleted(htr), perfect(htr) * 100.0 / htr->elems);
i = worst_run(htr, &deleted);
printf("Details: worst run %zu (%zu deleted)\n", i, deleted);
printf("Lookup after churn & spread (match): ");
fflush(stdout);
gettimeofday(&start, NULL);
for (i = 0; i < num; i++) {
unsigned int n = num * 5 + i * 9;
if (htable_obj_get(ht, &n)->self != objs[i].self)
abort();
}
gettimeofday(&stop, NULL);
printf(" %zu ns\n", normalize(&start, &stop, num));
printf("Lookup after churn & spread (miss): ");
fflush(stdout);
gettimeofday(&start, NULL);
for (i = 0; i < num; i++) {
unsigned int n = num * (5 + 9) + i * 9;
if (htable_obj_get(ht, &n))
abort();
}
gettimeofday(&stop, NULL);
printf(" %zu ns\n", normalize(&start, &stop, num));
printf("Lookup after churn & spread (random): ");
fflush(stdout);
gettimeofday(&start, NULL);
for (i = 0, j = 0; i < num; i++, j = (j + 10007) % num) {
unsigned int n = num * 5 + j * 9;
if (htable_obj_get(ht, &n)->self != &objs[j])
abort();
}
gettimeofday(&stop, NULL);
printf(" %zu ns\n", normalize(&start, &stop, num));
hashcount = 0;
printf("Deleting half after churn & spread: ");
fflush(stdout);
gettimeofday(&start, NULL);
for (i = 0; i < num; i+=2)
if (!htable_obj_del(ht, objs[i].self))
abort();
gettimeofday(&stop, NULL);
printf(" %zu ns\n", normalize(&start, &stop, num));
printf("Adding (a different) half after churn & spread: ");
fflush(stdout);
for (i = 0; i < num; i+=2)
objs[i].key = num*6+i*9;
gettimeofday(&start, NULL);
for (i = 0; i < num; i+=2)
htable_obj_add(ht, objs[i].self);
gettimeofday(&stop, NULL);
printf(" %zu ns\n", normalize(&start, &stop, num));
printf("Details: delete markers %zu, perfect %.0f%%\n",
count_deleted(htr), perfect(htr) * 100.0 / htr->elems);
return 0;
}
|