Highly reduce the number of allocations required to build a trie.
[apps/pfixtools.git] / common / trie.c
1 /******************************************************************************/
2 /*          pfixtools: a collection of postfix related tools                  */
3 /*          ~~~~~~~~~                                                         */
4 /*  ________________________________________________________________________  */
5 /*                                                                            */
6 /*  Redistribution and use in source and binary forms, with or without        */
7 /*  modification, are permitted provided that the following conditions        */
8 /*  are met:                                                                  */
9 /*                                                                            */
10 /*  1. Redistributions of source code must retain the above copyright         */
11 /*     notice, this list of conditions and the following disclaimer.          */
12 /*  2. Redistributions in binary form must reproduce the above copyright      */
13 /*     notice, this list of conditions and the following disclaimer in the    */
14 /*     documentation and/or other materials provided with the distribution.   */
15 /*  3. The names of its contributors may not be used to endorse or promote    */
16 /*     products derived from this software without specific prior written     */
17 /*     permission.                                                            */
18 /*                                                                            */
19 /*  THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND   */
20 /*  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE     */
21 /*  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR        */
22 /*  PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS    */
23 /*  BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR    */
24 /*  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF      */
25 /*  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS  */
26 /*  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN   */
27 /*  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)   */
28 /*  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF    */
29 /*  THE POSSIBILITY OF SUCH DAMAGE.                                           */
30 /******************************************************************************/
31
32 /*
33  * Copyright © 2008 Florent Bruneau
34  */
35
36 #include <sys/mman.h>
37
38 #include "str.h"
39 #include "trie.h"
40
41 typedef struct trie_entry_t trie_entry_t;
42
43 struct trie_entry_t {
44     int32_t  c_offset;
45     int32_t  c_len;
46
47     int32_t children_offset;
48     int32_t children_len;
49 };
50
51 struct trie_t {
52     trie_entry_t *entries;
53     int32_t entries_len;
54     int32_t entries_size;
55
56     char *c;
57     int32_t  c_len;
58     int32_t  c_size;
59
60     char     *keys;
61     int32_t  keys_len;
62     int32_t  keys_size;
63
64     int      *keys_offset;
65     int32_t  keys_offset_len;
66     int32_t  keys_offset_size;
67
68     bool locked;
69 };
70
71 trie_t *trie_new(void)
72 {
73     return p_new(trie_t, 1);
74 }
75
76 static inline void trie_cleanup_build_data(trie_t *trie)
77 {
78     p_delete(&trie->keys);
79     p_delete(&trie->keys_offset);
80     trie->keys_len = trie->keys_size = 0;
81     trie->keys_offset_len = trie->keys_offset_size = 0;
82 }
83
84 void trie_delete(trie_t **trie)
85 {
86     if (*trie) {
87         trie_cleanup_build_data(*trie);
88         trie_unlock(*trie);
89         p_delete(&(*trie)->entries);
90         p_delete(&(*trie)->c);
91         p_delete(trie);
92     }
93 }
94
95 /** Check that the given entry is a prefix for the given key.
96  */
97 static inline bool trie_entry_c_match(const trie_t *trie,
98                                       const trie_entry_t *entry,
99                                       const char *key, int *pos)
100 {
101     const char *c = trie->c + entry->c_offset;
102     int i = 0;
103     for (i = 0 ; i < entry->c_len ; ++i) {
104         if (key[i] != c[i]) {
105             if (pos) {
106                 *pos = i;
107             }
108             return false;
109         }
110     }
111     if (pos) {
112         *pos = i;
113     }
114     return true;
115 }
116
117 static inline bool trie_entry_match(const trie_t *trie,
118                                     const trie_entry_t *entry, const char *key)
119 {
120     return !!(strcmp(trie->c + entry->c_offset, key) == 0);
121 }
122
123 static inline bool trie_entry_is_leaf(const trie_entry_t *entry)
124 {
125     return entry->children_len == 0;
126 }
127
128 /** Lookup for a child of entry matching the given entry at the given pos.
129  * Only the first character of the children is taken into account in the
130  * lookup. The current entry is assumed to match the key.
131  */
132 static inline trie_entry_t* trie_entry_child(const trie_t *trie,
133                                              const trie_entry_t* entry,
134                                              const char *key)
135 {
136     int start = entry->children_offset;
137     int end   = start + entry->children_len;
138     const char c = *key;
139
140     while (start < end) {
141         int mid = (start + end) / 2;
142         trie_entry_t* child = &trie->entries[mid];
143         const char c2 = trie->c[child->c_offset];
144
145         if (child->c_len) {
146             if (c2 == c) {
147                 return child;
148             }
149             if (c < c2) {
150                 end = mid;
151             } else {
152                 start = mid + 1;
153             }
154         } else {
155             abort();
156         }
157     }
158     return NULL;
159 }
160
161 #define GROW(Buffer, Added, Len, Size)                  \
162   do {                                                  \
163       const int required_size = (Len) + (Added);        \
164       int next_size = (Size);                           \
165       if (next_size >= required_size) {                 \
166           break;                                        \
167       }                                                 \
168       do {                                              \
169           next_size = p_alloc_nr(next_size);            \
170       } while (next_size < required_size);              \
171       p_allocgrow(&(Buffer), next_size, &(Size));       \
172   } while(0)
173
174 static inline void trie_grow(trie_t *trie, int delta)
175 {
176     GROW(trie->entries, delta, trie->entries_len, trie->entries_size);
177 }
178
179 static inline int trie_entry_new(trie_t *trie)
180 {
181     memset(trie->entries + trie->entries_len, 0, sizeof(trie_entry_t));
182     return trie->entries_len++;
183 }
184
185 static inline int trie_add_leaf(trie_t *trie, const char *key)
186 {
187     trie_entry_t *entry;
188     int len = m_strlen(key) + 1;
189     entry = &trie->entries[trie_entry_new(trie)];
190     GROW(trie->c, len, trie->c_len, trie->c_size);
191     memcpy(trie->c + trie->c_len, key, len);
192     entry->c_offset = trie->c_len;
193     entry->c_len    = len;
194     trie->c_len    += len;
195     return trie->entries_len - 1;
196 }
197
198 static inline void trie_entry_insert_child(trie_t *trie, trie_entry_t *entry,
199                                            int pchild)
200 {
201     if (entry->children_len == 0) {
202         entry->children_offset = pchild;
203         entry->children_len    = 1;
204     } else {
205         if (entry->children_offset + entry->children_len != pchild) {
206             printf("Inserting child %d while offset is %d[%d]\n",
207                    pchild, entry->children_offset, entry->children_len);
208             abort();
209         }
210         ++entry->children_len;
211     }
212 }
213
214 static inline void trie_entry_split(trie_t *trie, trie_entry_t *entry, int pos)
215 {
216     trie_entry_t *child;
217     child    = &trie->entries[trie_entry_new(trie)];
218     if (pos == 0) {
219         child->c_offset = entry->c_offset;
220         child->c_len    = entry->c_len;
221         entry->c_offset = 0;
222         entry->c_len    = 0;
223     } else {
224         child->c_offset = entry->c_offset + pos;
225         child->c_len    = entry->c_len - pos;
226         entry->c_len    = pos;
227     }
228     child->children_offset = entry->children_offset;
229     child->children_len    = entry->children_len;
230     entry->children_offset = trie->entries_len - 1;
231     entry->children_len    = 1;
232 }
233
234 void trie_insert(trie_t *trie, const char* key)
235 {
236     assert(trie->entries == NULL && "Trie already compiled");
237
238     int len = m_strlen(key) + 1;
239     GROW(trie->keys, len, trie->keys_len, trie->keys_size);
240     memcpy(trie->keys + trie->keys_len, key, len);
241
242     GROW(trie->keys_offset, 1, trie->keys_offset_len, trie->keys_offset_size);
243     trie->keys_offset[trie->keys_offset_len++] = trie->keys_len;
244     trie->keys_len += len;
245 }
246
247
248 static inline void trie_compile_aux(trie_t *trie, int id,
249                                     int first_key, int last_key, int offset,
250                                     int initial_diff)
251 {
252     int forks[256];
253     int fork_pos = 0;
254     char current = '\0';
255
256     for (int off_diff = initial_diff ; fork_pos == 0 ; ++off_diff, ++offset) {
257         current = trie->keys[trie->keys_offset[first_key] + offset];
258         for (int i = first_key + 1 ; i < last_key ; ++i) {
259             const char *str = trie->keys + trie->keys_offset[i];
260             const char c = str[offset];
261             if (c != current) {
262                 trie_grow(trie, 2);
263                 if (fork_pos == 0) {
264                     trie_entry_split(trie, &trie->entries[id], off_diff);
265                 }
266                 trie_entry_insert_child(trie, &trie->entries[id],
267                                         trie_add_leaf(trie, str + offset));
268                 forks[fork_pos++] = i;
269                 current = c;
270             }
271         }
272         if (fork_pos == 0 && current == '\0') {
273             return;
274         }
275     }
276     forks[fork_pos] = last_key;
277
278     const int children_len = trie->entries[id].children_len;
279     for (int i = 0 ; i < children_len ; ++i) {
280         int child = trie->entries[id].children_offset + i;
281         if (forks[i] - 1 > first_key) {
282             trie_compile_aux(trie, child, first_key, forks[i], offset, 1);
283         }
284         first_key = forks[i];
285     }
286 }
287
288 void trie_compile(trie_t *trie, bool memlock)
289 {
290     assert(trie->entries == NULL && "Trie already compiled");
291     assert(trie->keys != NULL && "Trying to compile an empty trie");
292     {
293 #       define QSORT_TYPE int
294 #       define QSORT_BASE trie->keys_offset
295 #       define QSORT_NELT trie->keys_offset_len
296 #       define QSORT_LT(a,b) strcmp(trie->keys + *a, trie->keys + *b) < 0
297 #       include "qsort.c"
298     }
299
300     trie_grow(trie, trie->keys_len);
301     trie_compile_aux(trie, trie_add_leaf(trie, trie->keys),
302                      0, trie->keys_offset_len, 0, 0);
303     trie_cleanup_build_data(trie);
304     p_shrink(&trie->entries, trie->entries_len, &trie->entries_size);
305     p_shrink(&trie->c, trie->c_len, &trie->c_size);
306     if (memlock) {
307         trie_lock(trie);
308     }
309 }
310
311 bool trie_lookup(const trie_t *trie, const char *key)
312 {
313     assert(trie->keys == NULL && "Can't lookup: trie not compiled");
314     if (trie->entries_len == 0) {
315         return false;
316     } else {
317         trie_entry_t *current = trie->entries;
318         while (true) {
319             int pos = 0;
320             if (trie_entry_is_leaf(current)) {
321                 return trie_entry_match(trie, current, key);
322             } else if (trie_entry_c_match(trie, current, key, &pos)) {
323                 key += pos;
324                 current = trie_entry_child(trie, current, key);
325                 if (current == NULL) {
326                     return false;
327                 }
328             } else {
329                 return false;
330             }
331         }
332     }
333 }
334
335 void trie_lock(trie_t *trie)
336 {
337     if (trie->locked) {
338         return;
339     }
340     if (mlock(trie->entries, sizeof(trie_entry_t) * trie->entries_len) != 0) {
341         UNIXERR("mlock");
342         return;
343     }
344     if (mlock(trie->c, trie->c_len) != 0) {
345         UNIXERR("mlock");
346         munlock(trie->entries, sizeof(trie_entry_t) * trie->entries_len);
347         return;
348     }
349     trie->locked = true;
350 }
351
352 void trie_unlock(trie_t *trie)
353 {
354     if (!trie->locked) {
355         return;
356     }
357     munlock(trie->entries, sizeof(trie_entry_t) * trie->entries_len);
358     munlock(trie->c, trie->c_len);
359     trie->locked = false;
360 }
361
362 /* Debug {{{1
363  */
364
365 static inline void trie_entry_inspect(const trie_t *trie, bool show_content,
366                                       const trie_entry_t *entry, int level)
367 {
368     static int max_depth = 0;
369     static int leaves    = 0;
370     static int depth_sum = 0;
371
372     if (trie_entry_is_leaf(entry)) {
373         if (level > max_depth) {
374             max_depth = level;
375         }
376         ++leaves;
377         depth_sum += level;
378     }
379     if (show_content) {
380         for (int i = 0 ; i < level ; ++i) {
381             fputs("  ", stdout);
382         }
383         if (entry->c_len == 0) {
384             fputs("(nil)", stdout);
385         } else {
386             const char *c = trie->c + entry->c_offset;
387             printf("(%d) ", entry->c_len);
388             for (int i = 0 ; i < entry->c_len ; ++i) {
389                 if (c[i]) {
390                     printf("%c ", c[i]);
391                 } else {
392                     fputs("\\0 ", stdout);
393                 }
394             }
395         }
396         fputs("\n", stdout);
397     }
398     for (int i = entry->children_offset ;
399           i < entry->children_offset + entry->children_len ; ++i) {
400         trie_entry_inspect(trie, show_content, &trie->entries[i], level + 1);
401     }
402     if (level == 0) {
403         printf("Average char per node: %d\n", trie->c_len / trie->entries_len);
404         printf("Number of nodes: %d\n", trie->entries_len);
405         printf("Number of leaves: %d\n", leaves);
406         printf("Max depth: %d\n", max_depth);
407         printf("Average leaf depth: %d\n", depth_sum / leaves);
408         printf("Memory used: %d\n", (trie->entries_size * sizeof(trie_entry_t))
409                                   + (trie->c_size) + sizeof(trie_t));
410     }
411 }
412
413 void trie_inspect(const trie_t *trie, bool show_content)
414 {
415     trie_entry_inspect(trie, show_content, trie->entries, 0);
416 }