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->table[i]))
75 if (hash_bucket(ht, ht->rehash(get_raw_ptr(ht, ht->table[i]),
77 assert((ht->table[i] & ht->perfect_bit)
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 (ht->table[i] == HTABLE_DELETED)
93 return delete_markers;
96 /* Nanoseconds per operation */
97 static size_t normalize(const struct timespec *start,
98 const struct timespec *stop,
101 return time_to_nsec(time_divide(time_sub(*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[])
130 size_t i, j, num, deleted;
131 struct timespec start, stop;
132 struct htable_obj ht;
133 bool make_dumb = false;
135 if (argv[1] && strcmp(argv[1], "--dumb") == 0) {
139 num = argv[1] ? atoi(argv[1]) : 1000000;
140 objs = calloc(num, sizeof(objs[0]));
142 for (i = 0; i < num; i++) {
144 objs[i].self = &objs[i];
147 htable_obj_init(&ht);
149 printf("Initial insert: ");
152 for (i = 0; i < num; i++)
153 htable_obj_add(&ht, objs[i].self);
155 printf(" %zu ns\n", normalize(&start, &stop, num));
156 printf("Details: hash size %u, mask bits %u, perfect %.0f%%\n",
157 1U << ht.raw.bits, popcount(ht.raw.common_mask),
158 perfect(&ht.raw) * 100.0 / ht.raw.elems);
161 /* Screw with mask, to hobble us. */
162 update_common(&ht.raw, (void *)~ht.raw.common_bits);
163 printf("Details: DUMB MODE: mask bits %u\n",
164 popcount(ht.raw.common_mask));
167 printf("Initial lookup (match): ");
170 for (i = 0; i < num; i++)
171 if (htable_obj_get(&ht, &i)->self != objs[i].self)
174 printf(" %zu ns\n", normalize(&start, &stop, num));
176 printf("Initial lookup (miss): ");
179 for (i = 0; i < num; i++) {
180 unsigned int n = i + num;
181 if (htable_obj_get(&ht, &n))
185 printf(" %zu ns\n", normalize(&start, &stop, num));
187 /* Lookups in order are very cache-friendly for judy; try random */
188 printf("Initial lookup (random): ");
191 for (i = 0, j = 0; i < num; i++, j = (j + 10007) % num)
192 if (htable_obj_get(&ht, &j)->self != &objs[j])
195 printf(" %zu ns\n", normalize(&start, &stop, num));
198 printf("Initial delete all: ");
201 for (i = 0; i < num; i++)
202 if (!htable_obj_del(&ht, objs[i].self))
205 printf(" %zu ns\n", normalize(&start, &stop, num));
206 printf("Details: rehashes %zu\n", hashcount);
208 printf("Initial re-inserting: ");
211 for (i = 0; i < num; i++)
212 htable_obj_add(&ht, objs[i].self);
214 printf(" %zu ns\n", normalize(&start, &stop, num));
217 printf("Deleting first half: ");
220 for (i = 0; i < num; i+=2)
221 if (!htable_obj_del(&ht, objs[i].self))
224 printf(" %zu ns\n", normalize(&start, &stop, num));
226 printf("Details: rehashes %zu, delete markers %zu\n",
227 hashcount, count_deleted(&ht.raw));
229 printf("Adding (a different) half: ");
232 for (i = 0; i < num; i+=2)
236 for (i = 0; i < num; i+=2)
237 htable_obj_add(&ht, objs[i].self);
239 printf(" %zu ns\n", normalize(&start, &stop, num));
241 printf("Details: delete markers %zu, perfect %.0f%%\n",
242 count_deleted(&ht.raw), perfect(&ht.raw) * 100.0 / ht.raw.elems);
244 printf("Lookup after half-change (match): ");
247 for (i = 1; i < num; i+=2)
248 if (htable_obj_get(&ht, &i)->self != objs[i].self)
250 for (i = 0; i < num; i+=2) {
251 unsigned int n = i + num;
252 if (htable_obj_get(&ht, &n)->self != objs[i].self)
256 printf(" %zu ns\n", normalize(&start, &stop, num));
258 printf("Lookup after half-change (miss): ");
261 for (i = 0; i < num; i++) {
262 unsigned int n = i + num * 2;
263 if (htable_obj_get(&ht, &n))
267 printf(" %zu ns\n", normalize(&start, &stop, num));
269 /* Hashtables with delete markers can fill with markers over time.
270 * so do some changes to see how it operates in long-term. */
271 for (i = 0; i < 5; i++) {
273 /* We don't measure this: jmap is different. */
274 printf("Details: initial churn\n");
276 printf("Churning %s time: ",
284 for (j = 0; j < num; j++) {
285 if (!htable_obj_del(&ht, &objs[j]))
287 objs[j].key = num*i+j;
288 if (!htable_obj_add(&ht, &objs[j]))
293 printf(" %zu ns\n", normalize(&start, &stop, num));
296 /* Spread out the keys more to try to make it harder. */
297 printf("Details: reinserting with spread\n");
298 for (i = 0; i < num; i++) {
299 if (!htable_obj_del(&ht, objs[i].self))
301 objs[i].key = num * 5 + i * 9;
302 if (!htable_obj_add(&ht, objs[i].self))
305 printf("Details: delete markers %zu, perfect %.0f%%\n",
306 count_deleted(&ht.raw), perfect(&ht.raw) * 100.0 / ht.raw.elems);
307 i = worst_run(&ht.raw, &deleted);
308 printf("Details: worst run %zu (%zu deleted)\n", i, deleted);
310 printf("Lookup after churn & spread (match): ");
313 for (i = 0; i < num; i++) {
314 unsigned int n = num * 5 + i * 9;
315 if (htable_obj_get(&ht, &n)->self != objs[i].self)
319 printf(" %zu ns\n", normalize(&start, &stop, num));
321 printf("Lookup after churn & spread (miss): ");
324 for (i = 0; i < num; i++) {
325 unsigned int n = num * (5 + 9) + i * 9;
326 if (htable_obj_get(&ht, &n))
330 printf(" %zu ns\n", normalize(&start, &stop, num));
332 printf("Lookup after churn & spread (random): ");
335 for (i = 0, j = 0; i < num; i++, j = (j + 10007) % num) {
336 unsigned int n = num * 5 + j * 9;
337 if (htable_obj_get(&ht, &n)->self != &objs[j])
341 printf(" %zu ns\n", normalize(&start, &stop, num));
344 printf("Deleting half after churn & spread: ");
347 for (i = 0; i < num; i+=2)
348 if (!htable_obj_del(&ht, objs[i].self))
351 printf(" %zu ns\n", normalize(&start, &stop, num));
353 printf("Adding (a different) half after churn & spread: ");
356 for (i = 0; i < num; i+=2)
357 objs[i].key = num*6+i*9;
360 for (i = 0; i < num; i+=2)
361 htable_obj_add(&ht, objs[i].self);
363 printf(" %zu ns\n", normalize(&start, &stop, num));
365 printf("Details: delete markers %zu, perfect %.0f%%\n",
366 count_deleted(&ht.raw), perfect(&ht.raw) * 100.0 / ht.raw.elems);