1 /******************************************************************************/
2 /* pfixtools: a collection of postfix related tools */
4 /* ________________________________________________________________________ */
6 /* Redistribution and use in source and binary forms, with or without */
7 /* modification, are permitted provided that the following conditions */
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 */
19 /* THIS SOFTWARE IS PROVIDED BY THE CONTRIBUTORS ``AS IS'' AND ANY EXPRESS */
20 /* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED */
21 /* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE */
22 /* DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY */
23 /* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL */
24 /* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS */
25 /* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) */
26 /* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, */
27 /* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN */
28 /* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE */
29 /* POSSIBILITY OF SUCH DAMAGE. */
31 /* Copyright (c) 2006-2008 the Authors */
32 /* see AUTHORS and source files for details */
33 /******************************************************************************/
36 * Copyright © 2008 Florent Bruneau
46 static trie_t *create_trie_from_file(const char *file)
53 if (!file_map_open(&map, file, false)) {
58 while (end > p && end[-1] != '\n') {
62 warn("file %s miss a final \\n, ignoring last line", file);
66 while (p < end && p != NULL) {
67 const char *eol = (char *)memchr(p, '\n', end - p);
71 if (eol - p > BUFSIZ) {
76 for (const char *s = eol - 1 ; s >= p ; --s) {
77 line[i++] = ascii_tolower(*s);
80 memcpy(line, p, eol - p);
84 trie_insert(db, line);
88 trie_compile(db, false);
94 static void check_trie_with_file(const trie_t *db, const char *file)
100 if (!file_map_open(&map, file, false)) {
105 while (end > p && end[-1] != '\n') {
108 if (end != map.end) {
109 warn("file %s miss a final \\n, ignoring last line", file);
112 while (p < end && p != NULL) {
113 const char *eol = (char *)memchr(p, '\n', end - p);
117 if (eol - p > BUFSIZ) {
122 for (const char *s = eol - 1 ; s >= p ; --s) {
123 line[i++] = ascii_tolower(*s);
126 memcpy(line, p, eol - p);
130 if (!trie_lookup(db, line)) {
131 warn("'%s' not found in the trie", line);
135 file_map_close(&map);
139 static bool test_linear(const uint8_t *start, uint32_t len, uint8_t data) {
140 const uint8_t *end = start + len;
141 while (start < end) {
142 const uint8_t val = *start;
145 } else if (val > data) {
153 static bool test_dicho(const uint8_t *start, uint32_t len, uint8_t data) {
154 const uint8_t *end = start + len;
156 while (start < end) {
157 const uint8_t *mid = start + ((end - start) >> 1);
158 const uint8_t val = *mid;
162 } else if (data < val) {
171 __attribute__((used))
172 static void test_lookup(void) {
176 printf("size,dicho,linear\n");
177 for (int i = 1 ; i < 64 ; ++i) {
180 memset(set, 1, 64 * sizeof(bool));
181 while (selected > i) {
182 int val = rand() % 64;
190 memset(set, 0, 64 * sizeof(bool));
191 while (selected < i) {
192 int val = rand() % 64;
200 for (int j = 0 ; j < 64 ; ++j) {
207 struct timeval start, end;
208 double diff_dicho, diff_linear;
209 const int iterations = 50000000;
211 gettimeofday(&start, NULL);
212 for (int k = 0 ; k < iterations ; ++k) {
213 for (int j = 0 ; j < 64 ; ++j) {
214 test_dicho(data, i, j);
217 gettimeofday(&end, NULL);
218 diff_dicho = ((end.tv_sec - start.tv_sec) * 10.0)
219 + (double)(end.tv_usec - start.tv_usec) / 10e5;
221 gettimeofday(&start, NULL);
222 for (int k = 0 ; k < iterations ; ++k) {
223 for (int j = 0 ; j < 64 ; ++j) {
224 test_linear(data, i, j);
227 gettimeofday(&end, NULL);
228 diff_linear = ((end.tv_sec - start.tv_sec) * 10.0)
229 + (double)(end.tv_usec - start.tv_usec) / 10e5;
230 printf("%d,%d,%d\n", i, (int)diff_dicho, (int)diff_linear);
235 int main(int argc, char *argv[])
241 trie_t *trie = trie_new();
242 trie_insert(trie, "abcde123456789");
243 trie_insert(trie, "abcde123654789");
244 trie_insert(trie, "abcdefghi");
245 trie_insert(trie, "coucou");
246 trie_insert(trie, "coucou chez vous");
247 trie_insert(trie, "debout !");
248 trie_compile(trie, false);
249 trie_inspect(trie, true);
251 #define ASSERT_TRUE(str) \
252 if (!trie_lookup(trie, str)) { \
253 printf("\"%s\" not found in trie\n", str); \
256 #define ASSERT_FALSE(str) \
257 if (trie_lookup(trie, str)) { \
258 printf("\"%s\" found in trie\n", str); \
262 ASSERT_FALSE("coucou ");
263 ASSERT_FALSE("abcde123");
264 ASSERT_FALSE("abcde");
265 ASSERT_FALSE("coucou chez vous tous");
266 ASSERT_TRUE("abcde123456789");
267 ASSERT_TRUE("abcde123456789");
268 ASSERT_TRUE("abcde123654789");
269 ASSERT_TRUE("abcdefghi");
270 ASSERT_TRUE("coucou");
271 ASSERT_TRUE("coucou chez vous");
272 ASSERT_TRUE("debout !");
279 trie = create_trie_from_file(argv[1]);
280 trie_inspect(trie, false);
281 check_trie_with_file(trie, argv[1]);
283 const uint32_t how_many = 8 * 1000 * 1000;
284 struct timeval start, end;
287 gettimeofday(&start, NULL);
288 for (uint32_t i = 0 ; i < how_many ; ++i) {
289 trie_lookup(trie, argv[2]);
291 gettimeofday(&end, NULL);
292 diff = (end.tv_sec - start.tv_sec) + (double)(end.tv_usec - start.tv_usec) / 10e6;
293 printf("%u lookups per second\n", (int)(how_many / diff));