1 /* Test speed of intmap */
2 #include <ccan/time/time.h>
3 #include <ccan/intmap/intmap.h>
4 #include <ccan/isaac/isaac64.h>
5 #include <ccan/htable/htable_type.h>
6 #include <ccan/crypto/siphash24/siphash24.h>
12 /* hack to let us gather span. */
14 /* These point to strings or nodes. */
15 struct intmap child[2];
16 /* Encoding both prefix and critbit: 1 is appended to prefix. */
17 intmap_index_t prefix_and_critbit;
20 static void update_span(const void *p, size_t s, uintptr_t *min, uintptr_t *max)
22 if ((uintptr_t)p < *min)
24 if ((uintptr_t)p + s > *max)
25 *max = (uintptr_t)p + s;
28 static void getspan(const struct intmap *m, uintptr_t *min, uintptr_t *max)
36 update_span(n, sizeof(*n), min, max);
37 getspan(&n->child[0], min, max);
38 getspan(&n->child[1], min, max);
46 static struct siphash_seed sipseed;
48 static uint64_t keyof(const struct htable_elem *elem)
53 static size_t hashfn(const uint64_t index)
55 return siphash24(&sipseed, &index, sizeof(index));
58 static bool eqfn(const struct htable_elem *elem, const uint64_t index)
60 return elem->index == index;
62 HTABLE_DEFINE_TYPE(struct htable_elem, keyof, hashfn, eqfn, hash);
64 int main(int argc, char *argv[])
66 uint64_t i, total = 0, seed, *v;
67 size_t max = argv[1] ? atol(argv[1]) : 100000000;
69 struct timeabs start, end;
70 UINTMAP(uint64_t *) map;
72 struct htable_elem *e;
74 uintptr_t span_min, span_max;
79 /* We don't want our randomness function to dominate the time,
80 * nor deal with duplicates (just abort, that's v. unlikely) */
81 seed = time_now().ts.tv_sec + time_now().ts.tv_nsec;
82 isaac64_init(&isaac, (unsigned char *)&seed, sizeof(seed));
85 for (i = 0; i < max; i++)
86 total += isaac64_next_uint64(&isaac);
88 printf("%zu,random generation (nsec),%"PRIu64"\n", max,
89 time_to_nsec(time_divide(time_between(end, start), max)));
91 isaac64_init(&isaac, (unsigned char *)&seed, sizeof(seed));
93 for (i = 0; i < max; i++) {
94 if (!uintmap_add(&map, isaac64_next_uint64(&isaac), &i))
98 printf("%zu,critbit insert (nsec),%"PRIu64"\n", max,
99 time_to_nsec(time_divide(time_between(end, start), max)));
101 isaac64_init(&isaac, (unsigned char *)&seed, sizeof(seed));
103 for (i = 0; i < max; i++) {
104 if (uintmap_get(&map, isaac64_next_uint64(&isaac)) != &i)
108 printf("%zu,critbit successful lookup (nsec),%"PRIu64"\n", max,
109 time_to_nsec(time_divide(time_between(end, start), max)));
112 for (i = 0; i < max; i++) {
113 if (uintmap_get(&map, isaac64_next_uint64(&isaac)))
117 printf("%zu,critbit failed lookup (nsec),%"PRIu64"\n", max,
118 time_to_nsec(time_divide(time_between(end, start), max)));
120 isaac64_init(&isaac, (unsigned char *)&seed, sizeof(seed));
122 for (v = uintmap_first(&map, &i); v; v = uintmap_after(&map, &i)) {
127 printf("%zu,critbit iteration (nsec),%"PRIu64"\n", max,
128 time_to_nsec(time_divide(time_between(end, start), max)));
132 getspan(uintmap_unwrap_(&map), &span_min, &span_max);
133 printf("%zu,critbit memory (bytes),%zu\n",
134 max, (size_t)(span_max - span_min + max / 2) / max);
136 isaac64_init(&isaac, (unsigned char *)&seed, sizeof(seed));
138 for (i = 0; i < max; i++) {
139 if (!uintmap_del(&map, isaac64_next_uint64(&isaac)))
143 printf("%zu,critbit delete (nsec),%"PRIu64"\n", max,
144 time_to_nsec(time_divide(time_between(end, start), max)));
146 /* Fill with consecutive values */
147 for (i = 0; i < max; i++) {
148 if (!uintmap_add(&map, i, &i))
152 for (v = uintmap_first(&map, &i); v; v = uintmap_after(&map, &i)) {
157 printf("%zu,critbit consecutive iteration (nsec),%"PRIu64"\n", max,
158 time_to_nsec(time_divide(time_between(end, start), max)));
160 sipseed.u.u64[0] = isaac64_next_uint64(&isaac);
161 sipseed.u.u64[1] = isaac64_next_uint64(&isaac);
163 isaac64_init(&isaac, (unsigned char *)&seed, sizeof(seed));
165 for (i = 0; i < max; i++) {
166 e = malloc(sizeof(*e));
168 e->index = isaac64_next_uint64(&isaac);
172 printf("%zu,hash insert (nsec),%"PRIu64"\n", max,
173 time_to_nsec(time_divide(time_between(end, start), max)));
175 isaac64_init(&isaac, (unsigned char *)&seed, sizeof(seed));
177 for (i = 0; i < max; i++) {
178 if (hash_get(&hash, isaac64_next_uint64(&isaac))->v != &i)
182 printf("%zu,hash successful lookup (nsec),%"PRIu64"\n", max,
183 time_to_nsec(time_divide(time_between(end, start), max)));
186 for (i = 0; i < max; i++) {
187 if (hash_get(&hash, isaac64_next_uint64(&isaac)))
191 printf("%zu,hash failed lookup (nsec),%"PRIu64"\n", max,
192 time_to_nsec(time_divide(time_between(end, start), max)));
194 isaac64_init(&isaac, (unsigned char *)&seed, sizeof(seed));
196 for (e = hash_first(&hash, &it); e; e = hash_next(&hash, &it)) {
201 printf("%zu,hash iteration (nsec),%"PRIu64"\n", max,
202 time_to_nsec(time_divide(time_between(end, start), max)));
206 for (e = hash_first(&hash, &it); e; e = hash_next(&hash, &it))
207 update_span(e, sizeof(*e), &span_min, &span_max);
208 /* table itself tends to be allocated in separate memory. */
209 span_max += (sizeof(uintptr_t) << hash.raw.bits);
210 printf("%zu,hash memory (bytes),%zu\n",
211 max, (size_t)(span_max - span_min + max / 2) / max);
213 isaac64_init(&isaac, (unsigned char *)&seed, sizeof(seed));
215 for (i = 0; i < max; i++) {
216 e = hash_get(&hash, isaac64_next_uint64(&isaac));
217 if (!hash_del(&hash, e))
222 printf("%zu,hash delete (nsec),%"PRIu64"\n", max,
223 time_to_nsec(time_divide(time_between(end, start), max)));
225 /* Use total, but "never happens". */