From: Florent Bruneau Date: Sun, 5 Oct 2008 18:55:09 +0000 (+0200) Subject: Speed-up lookup. X-Git-Url: http://git.madism.org/?a=commitdiff_plain;h=ab82546953101224d09dcf2e44ff170e454e7282;p=apps%2Fpfixtools.git Speed-up lookup. Signed-off-by: Florent Bruneau --- diff --git a/common/trie.c b/common/trie.c index f58bd30..f16f0a6 100644 --- a/common/trie.c +++ b/common/trie.c @@ -84,21 +84,15 @@ void trie_delete(trie_t **trie) */ static inline bool trie_entry_c_match(const trie_t *trie, const trie_entry_t *entry, - const char *key, int *pos) + const char *key) { const char *c = array_ptr(trie->c, entry->c_offset); int i = 0; for (i = 0 ; i < entry->c_len ; ++i) { if (key[i] != c[i]) { - if (pos) { - *pos = i; - } return false; } } - if (pos) { - *pos = i; - } return true; } @@ -127,30 +121,26 @@ static inline bool trie_entry_is_leaf(const trie_entry_t *entry) * Only the first character of the children is taken into account in the * lookup. The current entry is assumed to match the key. */ -static inline trie_entry_t* trie_entry_child(const trie_t *trie, - const trie_entry_t* entry, - const char *key) +static inline const trie_entry_t* trie_entry_child(const trie_t *trie, + const trie_entry_t* entry, + const char *key) { - int start = entry->children_offset; - int end = start + entry->children_len; + uint32_t start = entry->children_offset; + uint32_t end = start + entry->children_len; const char c = *key; while (start < end) { - int mid = (start + end) / 2; - trie_entry_t* child = array_ptr(trie->entries, mid); + uint32_t mid = (start + end) >> 1; + const trie_entry_t* child = array_ptr(trie->entries, mid); const char c2 = array_elt(trie->c, child->c_offset); - if (child->c_len) { - if (c2 == c) { - return child; - } - if (c < c2) { - end = mid; - } else { - start = mid + 1; - } + if (c2 == c) { + return child; + } + if (c < c2) { + end = mid; } else { - abort(); + start = mid + 1; } } return NULL; @@ -289,13 +279,12 @@ bool trie_lookup(const trie_t *trie, const char *key) if (trie->entries.len == 0) { return false; } else { - trie_entry_t *current = array_ptr(trie->entries, 0); + const trie_entry_t *current = array_ptr(trie->entries, 0); while (true) { - int pos = 0; if (trie_entry_is_leaf(current)) { return trie_entry_match(trie, current, key); - } else if (trie_entry_c_match(trie, current, key, &pos)) { - key += pos; + } else if (trie_entry_c_match(trie, current, key)) { + key += current->c_len; current = trie_entry_child(trie, current, key); if (current == NULL) { return false; @@ -313,13 +302,12 @@ bool trie_prefix(const trie_t *trie, const char *key) if (trie->entries.len == 0) { return false; } else { - trie_entry_t *current = array_ptr(trie->entries, 0); + const trie_entry_t *current = array_ptr(trie->entries, 0); while (true) { - int pos = 0; if (trie_entry_is_leaf(current)) { return trie_entry_prefix(trie, current, key); - } else if (trie_entry_c_match(trie, current, key, &pos)) { - key += pos; + } else if (trie_entry_c_match(trie, current, key)) { + key += current->c_len; current = trie_entry_child(trie, current, key); if (current == NULL) { return false; diff --git a/common/tst-trie.c b/common/tst-trie.c index 9f69b1e..739bd41 100644 --- a/common/tst-trie.c +++ b/common/tst-trie.c @@ -129,6 +129,13 @@ int main(int argc, char *argv[]) if (argc > 1) { trie = create_trie_from_file(argv[1]); trie_inspect(trie, false); + if (argc > 2) { + time_t now = time(NULL); + for (uint32_t i = 0 ; i < 1000000000 ; ++i) { + trie_lookup(trie, argv[2]); + } + printf("%lu lookups per second\n", 1000000000 / (time(NULL) - now)); + } trie_delete(&trie); } return 0;