1 /* Simple speed tests for hashtables. */
2 #include <ccan/htable/htable_type.h>
3 #include <ccan/htable/htable.c>
4 #include <ccan/hash/hash.h>
5 #include <ccan/time/time.h>
11 static size_t hashcount;
16 /* Some contents. Doubles as consistency check. */
20 static const unsigned int *objkey(const struct object *obj)
25 static size_t hash_obj(const unsigned int *key)
28 return hashl(key, 1, 0);
31 static bool cmp(const struct object *object, const unsigned int *key)
33 return object->key == *key;
36 HTABLE_DEFINE_TYPE(struct object, objkey, hash_obj, cmp, htable_obj);
38 static unsigned int popcount(unsigned long val)
40 #if HAVE_BUILTIN_POPCOUNTL
41 return __builtin_popcountl(val);
43 if (sizeof(long) == sizeof(u64)) {
45 v = (v & 0x5555555555555555ULL)
46 + ((v >> 1) & 0x5555555555555555ULL);
47 v = (v & 0x3333333333333333ULL)
48 + ((v >> 1) & 0x3333333333333333ULL);
49 v = (v & 0x0F0F0F0F0F0F0F0FULL)
50 + ((v >> 1) & 0x0F0F0F0F0F0F0F0FULL);
51 v = (v & 0x00FF00FF00FF00FFULL)
52 + ((v >> 1) & 0x00FF00FF00FF00FFULL);
53 v = (v & 0x0000FFFF0000FFFFULL)
54 + ((v >> 1) & 0x0000FFFF0000FFFFULL);
55 v = (v & 0x00000000FFFFFFFFULL)
56 + ((v >> 1) & 0x00000000FFFFFFFFULL);
59 val = (val & 0x55555555ULL) + ((val >> 1) & 0x55555555ULL);
60 val = (val & 0x33333333ULL) + ((val >> 1) & 0x33333333ULL);
61 val = (val & 0x0F0F0F0FULL) + ((val >> 1) & 0x0F0F0F0FULL);
62 val = (val & 0x00FF00FFULL) + ((val >> 1) & 0x00FF00FFULL);
63 val = (val & 0x0000FFFFULL) + ((val >> 1) & 0x0000FFFFULL);
68 static size_t perfect(const struct htable *ht)
70 size_t i, placed_perfect = 0;
72 for (i = 0; i < ((size_t)1 << ht->bits); i++) {
73 if (!entry_is_valid(ht, i))
75 if (hash_bucket(ht, ht->rehash(get_raw_ptr(ht, ht->table[i]),
77 assert((ht->table[i] & ht_perfect_mask(ht))
78 == ht_perfect_mask(ht));
82 return placed_perfect;
85 static size_t count_deleted(const struct htable *ht)
87 size_t i, delete_markers = 0;
89 for (i = 0; i < ((size_t)1 << ht->bits); i++) {
90 if (entry_is_deleted(ht, i))
93 return delete_markers;
96 /* Nanoseconds per operation */
97 static size_t normalize(const struct timeabs *start,
98 const struct timeabs *stop,
101 return time_to_nsec(time_divide(time_between(*stop, *start), num));
104 static size_t worst_run(struct htable *ht, size_t *deleted)
106 size_t longest = 0, len = 0, this_del = 0, i;
109 /* This doesn't take into account end-wrap, but gives an idea. */
110 for (i = 0; i < ((size_t)1 << ht->bits); i++) {
113 if (ht->table[i] == HTABLE_DELETED)
127 int main(int argc, char *argv[])
132 struct timeabs start, stop;
133 struct htable_obj ht;
134 bool make_dumb = false;
136 if (argv[1] && strcmp(argv[1], "--dumb") == 0) {
140 num = argv[1] ? atoi(argv[1]) : 1000000;
141 objs = calloc(num, sizeof(objs[0]));
143 for (i = 0; i < num; i++) {
145 objs[i].self = &objs[i];
148 htable_obj_init(&ht);
150 printf("Initial insert: ");
153 for (i = 0; i < num; i++)
154 htable_obj_add(&ht, objs[i].self);
156 printf(" %zu ns\n", normalize(&start, &stop, num));
157 printf("Details: hash size %u, mask bits %u, perfect %.0f%%\n",
158 1U << ht.raw.bits, popcount(ht.raw.common_mask),
159 perfect(&ht.raw) * 100.0 / ht.raw.elems);
162 /* Screw with mask, to hobble us. */
163 update_common(&ht.raw, (void *)~ht.raw.common_bits);
164 printf("Details: DUMB MODE: mask bits %u\n",
165 popcount(ht.raw.common_mask));
168 printf("Initial lookup (match): ");
171 for (i = 0; i < num; i++)
172 if (htable_obj_get(&ht, &i)->self != objs[i].self)
175 printf(" %zu ns\n", normalize(&start, &stop, num));
177 printf("Initial lookup (miss): ");
180 for (i = 0; i < num; i++) {
181 unsigned int n = i + num;
182 if (htable_obj_get(&ht, &n))
186 printf(" %zu ns\n", normalize(&start, &stop, num));
188 /* Lookups in order are very cache-friendly for judy; try random */
189 printf("Initial lookup (random): ");
192 for (i = 0, j = 0; i < num; i++, j = (j + 10007) % num)
193 if (htable_obj_get(&ht, &j)->self != &objs[j])
196 printf(" %zu ns\n", normalize(&start, &stop, num));
199 printf("Initial delete all: ");
202 for (i = 0; i < num; i++)
203 if (!htable_obj_del(&ht, objs[i].self))
206 printf(" %zu ns\n", normalize(&start, &stop, num));
207 printf("Details: rehashes %zu\n", hashcount);
209 printf("Initial re-inserting: ");
212 for (i = 0; i < num; i++)
213 htable_obj_add(&ht, objs[i].self);
215 printf(" %zu ns\n", normalize(&start, &stop, num));
218 printf("Deleting first half: ");
221 for (i = 0; i < num; i+=2)
222 if (!htable_obj_del(&ht, objs[i].self))
225 printf(" %zu ns\n", normalize(&start, &stop, num));
227 printf("Details: rehashes %zu, delete markers %zu\n",
228 hashcount, count_deleted(&ht.raw));
230 printf("Adding (a different) half: ");
233 for (i = 0; i < num; i+=2)
237 for (i = 0; i < num; i+=2)
238 htable_obj_add(&ht, objs[i].self);
240 printf(" %zu ns\n", normalize(&start, &stop, num));
242 printf("Details: delete markers %zu, perfect %.0f%%\n",
243 count_deleted(&ht.raw), perfect(&ht.raw) * 100.0 / ht.raw.elems);
245 printf("Lookup after half-change (match): ");
248 for (i = 1; i < num; i+=2)
249 if (htable_obj_get(&ht, &i)->self != objs[i].self)
251 for (i = 0; i < num; i+=2) {
252 unsigned int n = i + num;
253 if (htable_obj_get(&ht, &n)->self != objs[i].self)
257 printf(" %zu ns\n", normalize(&start, &stop, num));
259 printf("Lookup after half-change (miss): ");
262 for (i = 0; i < num; i++) {
263 unsigned int n = i + num * 2;
264 if (htable_obj_get(&ht, &n))
268 printf(" %zu ns\n", normalize(&start, &stop, num));
270 /* Hashtables with delete markers can fill with markers over time.
271 * so do some changes to see how it operates in long-term. */
272 for (i = 0; i < 5; i++) {
274 /* We don't measure this: jmap is different. */
275 printf("Details: initial churn\n");
277 printf("Churning %s time: ",
285 for (j = 0; j < num; j++) {
286 if (!htable_obj_del(&ht, &objs[j]))
288 objs[j].key = num*i+j;
289 if (!htable_obj_add(&ht, &objs[j]))
294 printf(" %zu ns\n", normalize(&start, &stop, num));
297 /* Spread out the keys more to try to make it harder. */
298 printf("Details: reinserting with spread\n");
299 for (i = 0; i < num; i++) {
300 if (!htable_obj_del(&ht, objs[i].self))
302 objs[i].key = num * 5 + i * 9;
303 if (!htable_obj_add(&ht, objs[i].self))
306 printf("Details: delete markers %zu, perfect %.0f%%\n",
307 count_deleted(&ht.raw), perfect(&ht.raw) * 100.0 / ht.raw.elems);
308 i = worst_run(&ht.raw, &deleted);
309 printf("Details: worst run %u (%zu deleted)\n", i, deleted);
311 printf("Lookup after churn & spread (match): ");
314 for (i = 0; i < num; i++) {
315 unsigned int n = num * 5 + i * 9;
316 if (htable_obj_get(&ht, &n)->self != objs[i].self)
320 printf(" %zu ns\n", normalize(&start, &stop, num));
322 printf("Lookup after churn & spread (miss): ");
325 for (i = 0; i < num; i++) {
326 unsigned int n = num * (5 + 9) + i * 9;
327 if (htable_obj_get(&ht, &n))
331 printf(" %zu ns\n", normalize(&start, &stop, num));
333 printf("Lookup after churn & spread (random): ");
336 for (i = 0, j = 0; i < num; i++, j = (j + 10007) % num) {
337 unsigned int n = num * 5 + j * 9;
338 if (htable_obj_get(&ht, &n)->self != &objs[j])
342 printf(" %zu ns\n", normalize(&start, &stop, num));
345 printf("Deleting half after churn & spread: ");
348 for (i = 0; i < num; i+=2)
349 if (!htable_obj_del(&ht, objs[i].self))
352 printf(" %zu ns\n", normalize(&start, &stop, num));
354 printf("Adding (a different) half after churn & spread: ");
357 for (i = 0; i < num; i+=2)
358 objs[i].key = num*6+i*9;
361 for (i = 0; i < num; i+=2)
362 htable_obj_add(&ht, objs[i].self);
364 printf(" %zu ns\n", normalize(&start, &stop, num));
366 printf("Details: delete markers %zu, perfect %.0f%%\n",
367 count_deleted(&ht.raw), perfect(&ht.raw) * 100.0 / ht.raw.elems);