Browse code

Fix infinite loop in lookup.

This happens due to deleted keys filling the hash,
workaround this issue for now, if this is a performance hog
we'll need to periodically clean up the deleted slots by rehashing
elements.

Török Edvin authored on 2010/01/11 19:43:21
Showing 1 changed files
... ...
@@ -35,8 +35,8 @@
35 35
 
36 36
 static mpool_t *mempool = NULL;
37 37
 
38
-//#define USE_LRUHASHCACHE
39
-#define USE_SPLAY
38
+#define USE_LRUHASHCACHE
39
+//#define USE_SPLAY
40 40
 
41 41
 #ifdef USE_LRUHASHCACHE
42 42
 struct cache_key {
... ...
@@ -92,6 +92,7 @@ static void cacheset_lru_remove(struct cache_set *map, size_t howmany)
92 92
 	 * we remove due to LRU! */
93 93
 	if (old == map->lru_tail)
94 94
 	    map->lru_tail = 0;
95
+	map->elements--;
95 96
     }
96 97
 }
97 98
 
... ...
@@ -100,7 +101,7 @@ int cacheset_lookup_internal(struct cache_set *map, unsigned char *md5, size_t s
100 100
     uint32_t idx = cli_readint32(md5+8) & (map->capacity -1);
101 101
     uint32_t tries = 0;
102 102
     struct cache_key *k = &map->data[idx];
103
-    while (k->size != CACHE_KEY_EMPTY) {
103
+    while (k->size != CACHE_KEY_EMPTY && tries < map->capacity) {
104 104
 	if (k->size == size &&
105 105
 	    !memcmp(k->digest, md5, 16)) {
106 106
 	    /* found key */