| 1 |
/* |
| 2 |
* Tests to compare against POSIX RB-Trees |
| 3 |
* POSIX provides balanced binary trees via the tsearch(3p) API. glibc |
| 4 |
* implements them as RB-Trees. This file compares the performance of both. |
| 5 |
* |
| 6 |
* The semantic differences are: |
| 7 |
* |
| 8 |
* o The tsearch(3p) API does memory allocation of node structures itself, |
| 9 |
* rather than allowing the caller to embed it. |
| 10 |
* |
| 11 |
* o The c-rbtree API exposes the tree structure, allowing efficient tree |
| 12 |
* operations. Furthermore, it allows tree creation/deletion without taking |
| 13 |
* the expensive insert/remove paths. For instance, imagine you want to |
| 14 |
* create an rb-tree from a set of objects you have. With c-rbtree you can |
| 15 |
* do that without a single rotation or tree-restructuring in O(n), while |
| 16 |
* tsearch(3p) requires O(n log n). |
| 17 |
* |
| 18 |
* o The tsearch(3p) API requires one pointer-chase on each node access. This |
| 19 |
* is inherent to the design as it does not allow embedding the node in the |
| 20 |
* parent object. This slows down the API considerably. |
| 21 |
* |
| 22 |
* o The tsearch(3p) API does not allow multiple entries with the same key. |
| 23 |
* |
| 24 |
* o The tsearch(3p) API requires node lookup during removal. This does not |
| 25 |
* affect the worst-case runtime, but does reduce absolute performance. |
| 26 |
* |
| 27 |
* o The tsearch(3p) API does not allow O(1) tests whether a node is linked |
| 28 |
* or not. It requires a separate state variable per node. |
| 29 |
* |
| 30 |
* o The tsearch(3p) API does not allow walking the tree with context. The |
| 31 |
* only accessor twalk(3p) provides no tree context nor caller context to |
| 32 |
* the callback function. |
| 33 |
* |
| 34 |
* o The glibc implementation of tsearch(3p) uses RB-Trees without parent |
| 35 |
* pointers. Hence, tree traversal requires back-tracking. Performance is |
| 36 |
* similar, but it reduces memory consumption (though, at the same time it |
| 37 |
* stores the key pointer, and allocates the node on the heap, so overall |
| 38 |
* the memory consumption is higher still). |
| 39 |
* But the more important issue is, a node itself is not enough context as |
| 40 |
* tree iterator, but the full depth parent pointers are needed as well. |
| 41 |
*/ |
| 42 |
|
| 43 |
#undef NDEBUG |
| 44 |
#include <assert.h> |
| 45 |
#include <c-stdaux.h> |
| 46 |
#include <inttypes.h> |
| 47 |
#include <limits.h> |
| 48 |
#include <search.h> |
| 49 |
#include <stdio.h> |
| 50 |
#include <stdlib.h> |
| 51 |
#include <string.h> |
| 52 |
#include <time.h> |
| 53 |
#include "c-rbtree.h" |
| 54 |
#include "c-rbtree-private.h" |
| 55 |
|
| 56 |
typedef struct { |
| 57 |
int key; |
| 58 |
CRBNode rb; |
| 59 |
} Node; |
| 60 |
|
| 61 |
#define node_from_rb(_rb) ((Node *)((char *)(_rb) - offsetof(Node, rb))) |
| 62 |
#define node_from_key(_key) ((Node *)((char *)(_key) - offsetof(Node, key))) |
| 63 |
|
| 64 |
static void shuffle(Node **nodes, size_t n_memb) { |
| 65 |
unsigned int i, j; |
| 66 |
Node *t; |
| 67 |
|
| 68 |
for (i = 0; i < n_memb; ++i) { |
| 69 |
j = rand() % n_memb; |
| 70 |
t = nodes[j]; |
| 71 |
nodes[j] = nodes[i]; |
| 72 |
nodes[i] = t; |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
static int compare(CRBTree *t, void *k, CRBNode *n) { |
| 77 |
int key = (int)(unsigned long)k; |
| 78 |
Node *node = node_from_rb(n); |
| 79 |
|
| 80 |
return key - node->key; |
| 81 |
} |
| 82 |
|
| 83 |
static uint64_t now(void) { |
| 84 |
struct timespec ts; |
| 85 |
int r; |
| 86 |
|
| 87 |
r = clock_gettime(CLOCK_THREAD_CPUTIME_ID, &ts); |
| 88 |
c_assert(r >= 0); |
| 89 |
return ts.tv_sec * UINT64_C(1000000000) + ts.tv_nsec; |
| 90 |
} |
| 91 |
|
| 92 |
/* |
| 93 |
* POSIX tsearch(3p) based RB-Tree API |
| 94 |
* |
| 95 |
* This implements a small rb-tree API alongside c-rbtree but based on |
| 96 |
* tsearch(3p) and friends. |
| 97 |
* |
| 98 |
* Note that we don't care for OOM here, nor do we implement all the same |
| 99 |
* features as c-rbtree. This just does basic insertion, removal, and lookup |
| 100 |
* without any conflict detection. |
| 101 |
* |
| 102 |
* This also hard-codes 'Node' as object type that can be stored in the tree. |
| 103 |
*/ |
| 104 |
|
| 105 |
typedef struct PosixRBTree PosixRBTree; |
| 106 |
|
| 107 |
struct PosixRBTree { |
| 108 |
void *root; |
| 109 |
}; |
| 110 |
|
| 111 |
static int posix_rbtree_compare(const void *a, const void *b) { |
| 112 |
return *(const int *)a - *(const int *)b; |
| 113 |
} |
| 114 |
|
| 115 |
static void posix_rbtree_add(PosixRBTree *t, const Node *node) { |
| 116 |
void *res; |
| 117 |
|
| 118 |
res = tsearch(&node->key, &t->root, posix_rbtree_compare); |
| 119 |
c_assert(*(int **)res == &node->key); |
| 120 |
} |
| 121 |
|
| 122 |
static void posix_rbtree_remove(PosixRBTree *t, const Node *node) { |
| 123 |
void *res; |
| 124 |
|
| 125 |
res = tdelete(&node->key, &t->root, posix_rbtree_compare); |
| 126 |
c_assert(res); |
| 127 |
} |
| 128 |
|
| 129 |
static Node *posix_rbtree_find(PosixRBTree *t, int key) { |
| 130 |
void *res; |
| 131 |
|
| 132 |
res = tfind(&key, &t->root, posix_rbtree_compare); |
| 133 |
return res ? node_from_key(*(int **)res) : NULL; |
| 134 |
} |
| 135 |
|
| 136 |
static void posix_rbtree_visit(const void *n, const VISIT o, const int depth) { |
| 137 |
static int v; |
| 138 |
|
| 139 |
/* HACK: twalk() has no context; use static context; reset on root */ |
| 140 |
if (depth == 0 && (o == preorder || o == leaf)) |
| 141 |
v = 0; |
| 142 |
|
| 143 |
switch (o) { |
| 144 |
case postorder: |
| 145 |
case leaf: |
| 146 |
c_assert(v <= node_from_key(*(int **)n)->key); |
| 147 |
v = node_from_key(*(int **)n)->key; |
| 148 |
break; |
| 149 |
default: |
| 150 |
break; |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
static void posix_rbtree_traverse(PosixRBTree *t) { |
| 155 |
twalk(t->root, posix_rbtree_visit); |
| 156 |
} |
| 157 |
|
| 158 |
/* |
| 159 |
* Comparison between c-rbtree and tsearch(3p) |
| 160 |
* |
| 161 |
* Based on the tsearch(3p) API above, this now implements some comparisons |
| 162 |
* between c-rbtree and the POSIX API. |
| 163 |
* |
| 164 |
* The semantic differences are explained above. This does mostly performance |
| 165 |
* comparisons. |
| 166 |
*/ |
| 167 |
|
| 168 |
static void test_posix(void) { |
| 169 |
uint64_t ts, ts_c1, ts_c2, ts_c3, ts_c4; |
| 170 |
uint64_t ts_p1, ts_p2, ts_p3, ts_p4; |
| 171 |
PosixRBTree pt = {}; |
| 172 |
CRBNode **slot, *p; |
| 173 |
CRBTree t = {}; |
| 174 |
Node *nodes[2048]; |
| 175 |
unsigned long i; |
| 176 |
int v; |
| 177 |
|
| 178 |
/* allocate and initialize all nodes */ |
| 179 |
for (i = 0; i < sizeof(nodes) / sizeof(*nodes); ++i) { |
| 180 |
nodes[i] = malloc(sizeof(*nodes[i])); |
| 181 |
c_assert(nodes[i]); |
| 182 |
nodes[i]->key = i; |
| 183 |
c_rbnode_init(&nodes[i]->rb); |
| 184 |
} |
| 185 |
|
| 186 |
/* shuffle nodes */ |
| 187 |
shuffle(nodes, sizeof(nodes) / sizeof(*nodes)); |
| 188 |
|
| 189 |
/* add all nodes, and verify that each node is linked */ |
| 190 |
ts = now(); |
| 191 |
for (i = 0; i < sizeof(nodes) / sizeof(*nodes); ++i) { |
| 192 |
slot = c_rbtree_find_slot(&t, compare, (void *)(unsigned long)nodes[i]->key, &p); |
| 193 |
c_assert(slot); |
| 194 |
c_rbtree_add(&t, p, slot, &nodes[i]->rb); |
| 195 |
} |
| 196 |
ts_c1 = now() - ts; |
| 197 |
|
| 198 |
ts = now(); |
| 199 |
for (i = 0; i < sizeof(nodes) / sizeof(*nodes); ++i) |
| 200 |
posix_rbtree_add(&pt, nodes[i]); |
| 201 |
ts_p1 = now() - ts; |
| 202 |
|
| 203 |
/* shuffle nodes again */ |
| 204 |
shuffle(nodes, sizeof(nodes) / sizeof(*nodes)); |
| 205 |
|
| 206 |
/* traverse tree in-order */ |
| 207 |
ts = now(); |
| 208 |
i = 0; |
| 209 |
v = 0; |
| 210 |
for (p = c_rbtree_first(&t); p; p = c_rbnode_next(p)) { |
| 211 |
++i; |
| 212 |
|
| 213 |
c_assert(v <= node_from_rb(p)->key); |
| 214 |
v = node_from_rb(p)->key; |
| 215 |
} |
| 216 |
c_assert(i == sizeof(nodes) / sizeof(*nodes)); |
| 217 |
ts_c2 = now() - ts; |
| 218 |
|
| 219 |
ts = now(); |
| 220 |
posix_rbtree_traverse(&pt); |
| 221 |
ts_p2 = now() - ts; |
| 222 |
|
| 223 |
/* shuffle nodes again */ |
| 224 |
shuffle(nodes, sizeof(nodes) / sizeof(*nodes)); |
| 225 |
|
| 226 |
/* lookup all nodes (in different order) */ |
| 227 |
ts = now(); |
| 228 |
for (i = 0; i < sizeof(nodes) / sizeof(*nodes); ++i) |
| 229 |
c_assert(nodes[i] == c_rbtree_find_entry(&t, compare, |
| 230 |
(void *)(unsigned long)nodes[i]->key, |
| 231 |
Node, rb)); |
| 232 |
ts_c3 = now() - ts; |
| 233 |
|
| 234 |
ts = now(); |
| 235 |
for (i = 0; i < sizeof(nodes) / sizeof(*nodes); ++i) |
| 236 |
c_assert(nodes[i] == posix_rbtree_find(&pt, nodes[i]->key)); |
| 237 |
ts_p3 = now() - ts; |
| 238 |
|
| 239 |
/* shuffle nodes again */ |
| 240 |
shuffle(nodes, sizeof(nodes) / sizeof(*nodes)); |
| 241 |
|
| 242 |
/* remove all nodes (in different order) */ |
| 243 |
ts = now(); |
| 244 |
for (i = 0; i < sizeof(nodes) / sizeof(*nodes); ++i) |
| 245 |
c_rbnode_unlink(&nodes[i]->rb); |
| 246 |
ts_c4 = now() - ts; |
| 247 |
|
| 248 |
ts = now(); |
| 249 |
for (i = 0; i < sizeof(nodes) / sizeof(*nodes); ++i) |
| 250 |
posix_rbtree_remove(&pt, nodes[i]); |
| 251 |
ts_p4 = now() - ts; |
| 252 |
|
| 253 |
/* free nodes again */ |
| 254 |
for (i = 0; i < sizeof(nodes) / sizeof(*nodes); ++i) |
| 255 |
free(nodes[i]); |
| 256 |
|
| 257 |
fprintf(stderr, " insertion traversal lookup removal\n"); |
| 258 |
fprintf(stderr, " c-rbtree: %8"PRIu64"ns %8"PRIu64"ns %8"PRIu64"ns %8"PRIu64"ns\n", |
| 259 |
ts_c1, ts_c2, ts_c3, ts_c4); |
| 260 |
fprintf(stderr, "tsearch(3p): %8"PRIu64"ns %8"PRIu64"ns %8"PRIu64"ns %8"PRIu64"ns\n", |
| 261 |
ts_p1, ts_p2, ts_p3, ts_p4); |
| 262 |
} |
| 263 |
|
| 264 |
int main(int argc, char **argv) { |
| 265 |
/* we want stable tests, so use fixed seed */ |
| 266 |
srand(0xdeadbeef); |
| 267 |
|
| 268 |
test_posix(); |
| 269 |
return 0; |
| 270 |
} |